fix: preserve correct parent pointers when stripping types from AST

fixes #14204
ts-strip-fix
Simon Holthausen 10 months ago
parent 4a85c4157d
commit f82a7c765a

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: preserve correct parent pointers when stripping types from AST

@ -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 });
}

@ -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 = `<script lang="ts"></script>
<div>
<p class={x as y}>hi</p>
</div>
`;
const prev: any = internal_parse(input);
const post: any = remove_typescript_nodes(prev);
assert.equal(prev, post);
assert.equal(post.fragment.nodes[1].fragment.nodes[1].parent, post.fragment.nodes[1]);
});

Loading…
Cancel
Save