diff --git a/.changeset/lovely-mirrors-exercise.md b/.changeset/lovely-mirrors-exercise.md new file mode 100644 index 000000000000..6774d3eb6977 --- /dev/null +++ b/.changeset/lovely-mirrors-exercise.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: preserve correct parent pointers when stripping types from AST 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 36a25f512445..785d8275c7b2 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 @@ -131,10 +131,12 @@ const visitors = { }; /** + * Removes all Typescript nodes from the AST that were added by the Acorn-TS-Plugin. + * Note that this MUTATES the AST, in order to correctly preserve the parent pointers. * @template T * @param {T} ast * @returns {T} */ export function remove_typescript_nodes(ast) { - return walk(ast, null, visitors); + return walk(ast, null, visitors, { mutate: true }); } diff --git a/packages/svelte/tests/parser-modern/test.ts b/packages/svelte/tests/parser-modern/test.ts index 04af8b0780e3..b30d30843fe1 100644 --- a/packages/svelte/tests/parser-modern/test.ts +++ b/packages/svelte/tests/parser-modern/test.ts @@ -1,8 +1,10 @@ import * as fs from 'node:fs'; import { assert, it } from 'vitest'; import { parse } from 'svelte/compiler'; +import { parse as internal_parse } from '../../src/compiler/phases/1-parse/index.js'; import { try_load_json } from '../helpers.js'; import { suite, type BaseTest } from '../suite.js'; +import { remove_typescript_nodes } from '../../src/compiler/phases/1-parse/remove_typescript_nodes.js'; interface ParserTest extends BaseTest {} @@ -55,3 +57,16 @@ it('Strips BOM from the input', () => { ] }); }); + +it('Preserves correct parent pointers after stripping TypeScript nodes', () => { + const input = ` + +
hi
+