Skip to content

Commit

Permalink
handle leading comments
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 11, 2024
1 parent 82d7e98 commit ff7c9a5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ export function PATCH(req, ctx) {
{ /* @next-codemod-ignore */ ...ctx }
)
}

export function PUT(req, ctx) {
console.log(
{
// @next-codemod-ignore
...ctx
}
)
}

export function OPTIONS(req, ctx) {
console.log(
{
// @next-codemod-incorrect-ignore
...ctx
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ export function PATCH(req, ctx) {
{ /* @next-codemod-ignore */ ...ctx }
)
}

export function PUT(req, ctx) {
console.log(
{
// @next-codemod-ignore
...ctx
}
)
}

export function OPTIONS(req, ctx) {
console.log(
{
/* @next-codemod-error 'ctx' is used with spread syntax (...). Any asynchronous properties of 'ctx' must be awaited when accessed. */
// @next-codemod-incorrect-ignore
...ctx
}
)
}
37 changes: 28 additions & 9 deletions packages/next-codemod/transforms/lib/async-request-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,8 @@ export function wrapParentheseIfNeeded(
return hasChainAccess ? j.parenthesizedExpression(expression) : expression
}

export function insertCommentOnce(
node: ASTPath<any>['node'],
j: API['j'],
function existsComment(
comments: ASTPath<any>['node']['comments'],
comment: string
): boolean {
const isCodemodErrorComment = comment
Expand All @@ -436,8 +435,9 @@ export function insertCommentOnce(

let hasIgnoreComment = false
let hasComment = false
if (node.comments) {
node.comments.forEach((commentNode) => {

if (comments) {
comments.forEach((commentNode) => {
const currentComment = commentNode.value
if (currentComment.trim().startsWith(NEXT_CODEMOD_IGNORE_ERROR_PREFIX)) {
hasIgnoreComment = true
Expand All @@ -450,14 +450,33 @@ export function insertCommentOnce(
// check if there's already a @next-codemod-ignore comment.
// if ignore comment exists, bypass the comment insertion.
if (hasIgnoreComment && isCodemodErrorComment) {
return false
return true
}
if (hasComment) {
return false
return true
}
}
node.comments = [j.commentBlock(comment), ...(node.comments || [])]
return true
return false
}

export function insertCommentOnce(
node: ASTPath<any>['node'],
j: API['j'],
comment: string
): boolean {
const hasCommentInInlineComments = existsComment(node.comments, comment)
const hasCommentInLeadingComments = existsComment(
node.leadingComments,
comment
)

if (!hasCommentInInlineComments && !hasCommentInLeadingComments) {
// Always insert into inline comment
node.comments = [j.commentBlock(comment), ...(node.comments || [])]
return true
}

return false
}

export function getVariableDeclaratorId(
Expand Down

0 comments on commit ff7c9a5

Please sign in to comment.