Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codemod: add separator to the parenthenese expr #71993

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading