From d9a9a5d6ecf097c3658be68fbc6e00ba7fdff7dc Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 10 Oct 2024 17:51:05 +0200 Subject: [PATCH] codemod: do not await on invalid prop (#71089) --- .../access-props-32.input.tsx | 6 ++++++ .../access-props-32.output.tsx | 6 ++++++ .../lib/async-request-api/next-async-dynamic-prop.ts | 9 +++++++++ 3 files changed, 21 insertions(+) create mode 100644 packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.input.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.output.tsx diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.input.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.input.tsx new file mode 100644 index 0000000000000..bed7e00cf5623 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.input.tsx @@ -0,0 +1,6 @@ +export async function GET( + req: NextRequest, + ctx: any, +): Promise { + callback(ctx.propDoesNotExist); +} diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.output.tsx new file mode 100644 index 0000000000000..bed7e00cf5623 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-32.output.tsx @@ -0,0 +1,6 @@ +export async function GET( + req: NextRequest, + ctx: any, +): Promise { + callback(ctx.propDoesNotExist); +} diff --git a/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts b/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts index df62c81754629..5bb61d7bbf174 100644 --- a/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts +++ b/packages/next-codemod/transforms/lib/async-request-api/next-async-dynamic-prop.ts @@ -51,6 +51,15 @@ function awaitMemberAccessOfProp( memberAccess.forEach((memberAccessPath) => { const member = memberAccessPath.value + const memberProperty = member.property + const isAccessingMatchedProperty = + j.Identifier.check(memberProperty) && + TARGET_PROP_NAMES.has(memberProperty.name) + + if (!isAccessingMatchedProperty) { + return + } + if (isParentPromiseAllCallExpression(memberAccessPath, j)) { return }