From 077eea9f450d7887333846f1f507be2c4b6279a2 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 22 Oct 2024 23:10:02 +0200 Subject: [PATCH] codemod: should not transform when param is not used --- .../access-props-39.input.tsx | 7 +++++++ .../access-props-39.output.tsx | 7 +++++++ .../lib/async-request-api/next-async-dynamic-prop.ts | 7 +++---- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.input.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.output.tsx diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.input.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.input.tsx new file mode 100644 index 0000000000000..bad3318722e60 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.input.tsx @@ -0,0 +1,7 @@ +export default function Page({ params, children }) { + return children +} + +export function generateMetadata(props) { + return {} +} diff --git a/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.output.tsx b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.output.tsx new file mode 100644 index 0000000000000..bad3318722e60 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/next-async-request-api-dynamic-props/access-props-39.output.tsx @@ -0,0 +1,7 @@ +export default function Page({ params, children }) { + return children +} + +export function generateMetadata(props) { + return {} +} 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 3207ec5729d9c..7bcb08df06b01 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 @@ -695,9 +695,8 @@ export function transformDynamicProps( : null const paramPropertyName = paramsPropertyName || matchedPropName - // if propName is not used in lower scope, and it stars with unused prefix `_`, - // also skip the transformation - + // If propName is an identifier and not used in lower scope, + // also skip the transformation. const hasUsedInBody = j(functionBodyPath) .find(j.Identifier, { @@ -705,7 +704,7 @@ export function transformDynamicProps( }) .size() > 0 - if (!hasUsedInBody && paramPropertyName.startsWith('_')) continue + if (!hasUsedInBody && j.Identifier.check(paramsProperty)) continue // Search the usage of propName in the function body, // if they're all awaited or wrapped with use(), skip the transformation