From 941f83b5a84daf859d7da623687298ece0307cc5 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Tue, 10 Sep 2024 16:35:28 +0200 Subject: [PATCH] fix: don't show `state_referenced_locally` warning on types (#13177) Add a new visitor to the "strip TS AST" logic. Strictly speaking this results in incorrect ASTs but esrap, our printer, just ignores them at those positions, so we can do that. An alternative would be to adjust zimmerframe to be able to return something to say "delete this node" Fixes #13173 --- .changeset/five-suns-roll.md | 5 +++++ .../phases/1-parse/remove_typescript_nodes.js | 14 +++++--------- .../state-referenced-locally-types/input.svelte | 6 ++++++ .../state-referenced-locally-types/warnings.json | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 .changeset/five-suns-roll.md create mode 100644 packages/svelte/tests/validator/samples/state-referenced-locally-types/input.svelte create mode 100644 packages/svelte/tests/validator/samples/state-referenced-locally-types/warnings.json diff --git a/.changeset/five-suns-roll.md b/.changeset/five-suns-roll.md new file mode 100644 index 0000000000..ffd0ca44cf --- /dev/null +++ b/.changeset/five-suns-roll.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't show `state_referenced_locally` warning on types diff --git a/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js b/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js index 872b71a4df..edbb673bd5 100644 --- a/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js +++ b/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js @@ -73,6 +73,11 @@ const visitors = { TSNonNullExpression(node, context) { return context.visit(node.expression); }, + TSTypeAnnotation() { + // This isn't correct, strictly speaking, and could result in invalid ASTs (like an empty statement within function parameters), + // but esrap, our printing tool, just ignores these AST nodes at invalid positions, so it's fine + return b.empty; + }, TSInterfaceDeclaration() { return b.empty; }, @@ -94,15 +99,6 @@ const visitors = { } return node.parameter; }, - Identifier(node) { - if (node.typeAnnotation) { - return { - ...node, - typeAnnotation: null - }; - } - return node; - }, FunctionExpression: remove_this_param, FunctionDeclaration: remove_this_param, TSDeclareFunction() { diff --git a/packages/svelte/tests/validator/samples/state-referenced-locally-types/input.svelte b/packages/svelte/tests/validator/samples/state-referenced-locally-types/input.svelte new file mode 100644 index 0000000000..30d282d7e4 --- /dev/null +++ b/packages/svelte/tests/validator/samples/state-referenced-locally-types/input.svelte @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/packages/svelte/tests/validator/samples/state-referenced-locally-types/warnings.json b/packages/svelte/tests/validator/samples/state-referenced-locally-types/warnings.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/packages/svelte/tests/validator/samples/state-referenced-locally-types/warnings.json @@ -0,0 +1 @@ +[]