From 7e24e988dc34b656b501f1c7562f16214e03cca2 Mon Sep 17 00:00:00 2001 From: Mohab Sameh Date: Wed, 16 Jul 2025 14:22:17 +0300 Subject: [PATCH] fix(nuxt/4/shallow-function-reactivity): only add key to useAsyncData if useRoute variable is found; add tests and docs --- .../__testfixtures__/no_route_var.input.ts | 6 +++++ .../__testfixtures__/no_route_var.output.ts | 6 +++++ .../__testfixtures__/with_route_var.input.ts | 7 ++++++ .../__testfixtures__/with_route_var.output.ts | 7 ++++++ .../shallow-function-reactivity/src/index.ts | 24 +++++++++---------- 5 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.input.ts create mode 100644 codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.output.ts create mode 100644 codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.input.ts create mode 100644 codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.output.ts diff --git a/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.input.ts b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.input.ts new file mode 100644 index 00000000..75f5d9a5 --- /dev/null +++ b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.input.ts @@ -0,0 +1,6 @@ +// biome-ignore lint/correctness/useHookAtTopLevel: +const { data } = useAsyncData(() => getContinuousKLines(symbol.value, interval.value), { + immediate: false, + default: () => [] satisfies Array, + watch: [symbol, interval], +}); \ No newline at end of file diff --git a/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.output.ts b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.output.ts new file mode 100644 index 00000000..75f5d9a5 --- /dev/null +++ b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/no_route_var.output.ts @@ -0,0 +1,6 @@ +// biome-ignore lint/correctness/useHookAtTopLevel: +const { data } = useAsyncData(() => getContinuousKLines(symbol.value, interval.value), { + immediate: false, + default: () => [] satisfies Array, + watch: [symbol, interval], +}); \ No newline at end of file diff --git a/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.input.ts b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.input.ts new file mode 100644 index 00000000..70020026 --- /dev/null +++ b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.input.ts @@ -0,0 +1,7 @@ +// biome-ignore lint/correctness/useHookAtTopLevel: +const route = useRoute(); +const { data } = useAsyncData(() => getContinuousKLines(route.params.slug, interval.value), { + immediate: false, + default: () => [] satisfies Array, + watch: [route, interval], +}); \ No newline at end of file diff --git a/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.output.ts b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.output.ts new file mode 100644 index 00000000..29d0e580 --- /dev/null +++ b/codemods/nuxt/4/shallow-function-reactivity/__testfixtures__/with_route_var.output.ts @@ -0,0 +1,7 @@ +// biome-ignore lint/correctness/useHookAtTopLevel: +const route = useRoute(); +const { data } = useAsyncData(route.params.slug, () => getContinuousKLines(route.params.slug, interval.value), { + immediate: false, + default: () => [] satisfies Array, + watch: [route, interval], +}); \ No newline at end of file diff --git a/codemods/nuxt/4/shallow-function-reactivity/src/index.ts b/codemods/nuxt/4/shallow-function-reactivity/src/index.ts index 9826c08b..b8bea140 100644 --- a/codemods/nuxt/4/shallow-function-reactivity/src/index.ts +++ b/codemods/nuxt/4/shallow-function-reactivity/src/index.ts @@ -63,7 +63,7 @@ export default function transform( .forEach((path) => { const args = path.node.arguments; if (args[0].type === "ArrowFunctionExpression") { - let slug = ""; + let slugVar = ""; root .find(j.VariableDeclarator, { init: { @@ -74,18 +74,18 @@ export default function transform( }, }) .forEach((path) => { - slug = path.node.id.name; + slugVar = path.node.id.name; }); - slug += ".params.slug"; - - // Create the new arguments - const newArg = j.identifier(slug); - - path.node.arguments.unshift(newArg); - - // Replace the node with the new node, preserving comments - replaceWithComments(path, path.node); - isDirty = true; + if (slugVar) { + const slug = slugVar + ".params.slug"; + // Create the new arguments + const newArg = j.identifier(slug); + path.node.arguments.unshift(newArg); + // Replace the node with the new node, preserving comments + replaceWithComments(path, path.node); + isDirty = true; + } + // If no slugVar found, do nothing (skip transformation) } });