diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-11.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-11.output.tsx index 8ca2f99d6eeb3..b317adc580307 100644 --- a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-11.output.tsx +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-11.output.tsx @@ -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) } diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-19.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-19.output.tsx index 3fd23bb439c89..39476c888899e 100644 --- a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-19.output.tsx +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-19.output.tsx @@ -8,6 +8,6 @@ export function myFun() { export function myFun2() { return function () { - (headers() as unknown as UnsafeUnwrappedHeaders) + void (headers() as unknown as UnsafeUnwrappedHeaders) }; } diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-25.input.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-25.input.tsx new file mode 100644 index 0000000000000..0a1aa920908f9 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-25.input.tsx @@ -0,0 +1,6 @@ +import { cookies } from 'next/headers' + +export function myFunc() { + const c = cookies() + cookies() +} diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-25.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-25.output.tsx new file mode 100644 index 0000000000000..bd162864fc3ae --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-apis/async-api-25.output.tsx @@ -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) +} diff --git a/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-api.ts b/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-api.ts index 2bfa7d6710dfa..5298052066f76 100644 --- a/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-api.ts +++ b/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-api.ts @@ -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 `()` 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: + // + // + // output: + // ( as ...) + // void ( 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