Skip to content

Commit

Permalink
add handling for next-codemod-ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 11, 2024
1 parent ae38de9 commit 4f0b070
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ export function POST(req, ctx) {
{ ...req }
)
}

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

export function PATCH(req, ctx) {
console.log(
{ /* @next-codemod-ignore */ ...ctx }
)
}
25 changes: 22 additions & 3 deletions packages/next-codemod/transforms/lib/async-request-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type FunctionScope =
| ArrowFunctionExpression

export const NEXT_CODEMOD_ERROR_PREFIX = '@next-codemod-error'
const NEXT_CODEMOD_IGNORE_ERROR_PREFIX = '@next-codemod-ignore'

export const TARGET_ROUTE_EXPORTS = new Set([
'GET',
Expand Down Expand Up @@ -429,10 +430,28 @@ export function insertCommentOnce(
j: API['j'],
comment: string
): boolean {
const isCodemodErrorComment = comment
.trim()
.startsWith(NEXT_CODEMOD_ERROR_PREFIX)

let hasIgnoreComment = false
let hasComment = false
if (node.comments) {
const hasComment = node.comments.some(
(commentNode) => commentNode.value === comment
)
node.comments.forEach((commentNode) => {
const currentComment = commentNode.value
if (currentComment.trim().startsWith(NEXT_CODEMOD_IGNORE_ERROR_PREFIX)) {
hasIgnoreComment = true
}
if (currentComment === comment) {
hasComment = true
}
})
// If it's inserting codemod error comment,
// check if there's already a @next-codemod-ignore comment.
// if ignore comment exists, bypass the comment insertion.
if (hasIgnoreComment && isCodemodErrorComment) {
return false
}
if (hasComment) {
return false
}
Expand Down

0 comments on commit 4f0b070

Please sign in to comment.