Skip to content

Commit

Permalink
codemod: add separator to the parenthenese expr (#71993)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Oct 29, 2024
1 parent 89ba33a commit 97153cc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { headers, type UnsafeUnwrappedHeaders } from 'next/headers';

export function MyComp() {
(headers() as unknown as UnsafeUnwrappedHeaders)
void (headers() as unknown as UnsafeUnwrappedHeaders)
}

export function generateContentfulMetadata() {
(headers() as unknown as UnsafeUnwrappedHeaders)
void (headers() as unknown as UnsafeUnwrappedHeaders)
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export function myFun() {

export function myFun2() {
return function () {
(headers() as unknown as UnsafeUnwrappedHeaders)
void (headers() as unknown as UnsafeUnwrappedHeaders)
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { cookies } from 'next/headers'

export function myFunc() {
const c = cookies()
cookies()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { cookies, type UnsafeUnwrappedCookies } from 'next/headers';

export function myFunc() {
const c = (cookies() as unknown as UnsafeUnwrappedCookies)
void (cookies() as unknown as UnsafeUnwrappedCookies)
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,26 @@ function castTypesOrAddComment(
)
// Replace the original expression with the new cast expression,
// also wrap () around the new cast expression.
j(path).replaceWith(j.parenthesizedExpression(newCastExpression))
const parent = path.parent.value
const wrappedExpression = j.parenthesizedExpression(newCastExpression)
path.replace(wrappedExpression)

// If the wrapped expression `(<expression>)` is the beginning of an expression statement,
// add a void operator to separate the statement, to avoid syntax error that being treated as part of previous statement.
// example:
// input:
// <expression>
// <expression>
// output:
// (<expression> as ...)
// void (<expression> as ...)
if (
j.ExpressionStatement.check(parent) &&
parent.expression === path.node
) {
// append a semicolon to the start of the expression statement
parent.expression = j.unaryExpression('void', parent.expression)
}
modified = true

// If cast types are not imported, add them to the import list
Expand Down

0 comments on commit 97153cc

Please sign in to comment.