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: type cast async api calls in non entry file exports #71040

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,6 @@
import { cookies } from 'next/headers'

export default function Foo(): string {
const name = cookies().get('name')
return name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { cookies, type UnsafeUnwrappedCookies } from 'next/headers';

export default function Foo(): string {
const name = (cookies() as unknown as UnsafeUnwrappedCookies).get('name')
return name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { cookies } from 'next/headers'

export default function Foo(): string {
const name = cookies().get('name')
return name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { cookies } from 'next/headers'

export default function Foo(): string {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
const name = /* Next.js Dynamic Async API Codemod: Manually await this call, if it's a Server Component */
cookies().get('name')
huozhi marked this conversation as resolved.
Show resolved Hide resolved
return name
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,26 @@ describe('next-async-request-api - dynamic-apis', () => {
const prefix = `${fixtureDir}/${fixture}`;
const [inputPath, input] = getSourceByInputPath(path.join(`${__dirname}`, `../__testfixtures__/${prefix}.input`))
const [outputPath, expectedOutput] = getSourceByInputPath(path.join(`${__dirname}`, `../__testfixtures__/${prefix}.output`))
const extension = path.extname(inputPath)

const transformPath = `${__dirname}/../${transformName}`
const transform = require(transformPath).default

// Override test fixture input filename with `page.tsx` to always match the expected output,
// otherwise fallback to the original filename.
const overrideFilename = /[\\/]origin-name-\d{2}-/.test(inputPath)
// extract the <name> from `origin-name-<name>-<number>.input.js`
? inputPath
.replace(/origin-name-(\d{2})-/, '')
.replace(/\.input\./, '.')
: 'page' + extension

it(`transforms correctly ${prefix}`, () => {
runInlineTest(
transform,
null,
{
path: inputPath,
path: overrideFilename,
source: input,
},
expectedOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('next-async-request-api - dynamic-props', () => {
const prefix = `${fixtureDir}/${fixture}`;
const [inputPath, input] = getSourceByInputPath(path.join(`${__dirname}`, `../__testfixtures__/${prefix}.input`))
const [outputPath, expectedOutput] = getSourceByInputPath(path.join(`${__dirname}`, `../__testfixtures__/${prefix}.output`))
const extension = path.extname(inputPath)

const transformPath = `${__dirname}/../${transformName}`
const transform = require(transformPath).default
Expand All @@ -48,7 +49,7 @@ describe('next-async-request-api - dynamic-props', () => {
? inputPath
.replace(/origin-name-(\d{2})-/, '')
.replace(/\.input\./, '.')
: 'page.tsx'
: 'page' + extension

it(`transforms correctly ${prefix}`, () => {
runInlineTest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
findClosetParentFunctionScope,
wrapParentheseIfNeeded,
insertCommentOnce,
NEXTJS_ENTRY_FILES,
} from './utils'

const DYNAMIC_IMPORT_WARN_COMMENT = ` Next.js Dynamic Async API Codemod: The APIs under 'next/headers' are async now, need to be manually awaited. `
Expand Down Expand Up @@ -44,6 +45,7 @@ export function transformDynamicAPI(
api: API,
filePath: string
) {
const isEntryFile = NEXTJS_ENTRY_FILES.test(filePath)
const j = api.jscodeshift.withParser('tsx')
const root = j(source)
let modified = false
Expand Down Expand Up @@ -119,7 +121,8 @@ export function transformDynamicAPI(
} else {
// Determine if the function is an export
const closetScopePath = closetScope.get()
const isFromExport = isMatchedFunctionExported(closetScopePath, j)
const isEntryFileExport =
isEntryFile && isMatchedFunctionExported(closetScopePath, j)
const closestFunctionNode = closetScope.size()
? closetScopePath.node
: null
Expand All @@ -130,7 +133,7 @@ export function transformDynamicAPI(
// e.g. export const MyComponent = function() {}
let exportFunctionNode

if (isFromExport) {
if (isEntryFileExport) {
if (
closestFunctionNode &&
isFunctionType(closestFunctionNode.type)
Expand All @@ -144,7 +147,7 @@ export function transformDynamicAPI(

let canConvertToAsync = false
// check if current path is under the default export function
if (isFromExport) {
if (isEntryFileExport) {
// if default export function is not async, convert it to async, and await the api call
if (!isCallAwaited) {
// If the scoped function is async function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
insertCommentOnce,
TARGET_ROUTE_EXPORTS,
getVariableDeclaratorId,
NEXTJS_ENTRY_FILES,
} from './utils'

const PAGE_PROPS = 'props'
const MATCHED_FILE_PATTERNS = /([\\/]|^)(page|layout|route)\.(t|j)sx?$/

function findFunctionBody(path: ASTPath<FunctionScope>) {
let functionBody = path.node.body
Expand Down Expand Up @@ -342,8 +342,8 @@ export function transformDynamicProps(
api: API,
filePath: string
) {
const isMatched = MATCHED_FILE_PATTERNS.test(filePath)
if (!isMatched) {
const isEntryFile = NEXTJS_ENTRY_FILES.test(filePath)
if (!isEntryFile) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import type {
FunctionExpression,
} from 'jscodeshift'

export const NEXTJS_ENTRY_FILES =
/([\\/]|^)(page|layout|route|default)\.(t|j)sx?$/

export type FunctionScope =
| FunctionDeclaration
| FunctionExpression
Expand Down