Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve correct parent pointers when stripping types from AST #14271

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-mirrors-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: preserve correct parent pointers when stripping types from AST
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
15 changes: 15 additions & 0 deletions packages/svelte/tests/parser-modern/test.ts
Original file line number Diff line number Diff line change
@@ -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 {}

Expand Down Expand Up @@ -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