Skip to content

Commit c033dae

Browse files
authored
fix: bump esrap, prevent malformed AST (#14742)
* bump esrap, start fixing stuff * ignore type exports when looking for undefineds * sanitize stuff * changeset * fix * oops * try this? * prettier
1 parent 36a437c commit c033dae

File tree

9 files changed

+47
-21
lines changed

9 files changed

+47
-21
lines changed

.changeset/purple-donuts-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: bump esrap, prevent malformed AST

packages/svelte/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"aria-query": "^5.3.1",
148148
"axobject-query": "^4.1.0",
149149
"esm-env": "^1.2.1",
150-
"esrap": "^1.2.3",
150+
"esrap": "^1.3.1",
151151
"is-reference": "^3.0.3",
152152
"locate-character": "^3.0.0",
153153
"magic-string": "^0.30.11",

packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ function remove_this_param(node, context) {
1717

1818
/** @type {Visitors<any, null>} */
1919
const visitors = {
20+
_(node, context) {
21+
context.next();
22+
23+
// TODO there may come a time when we decide to preserve type annotations.
24+
// until that day comes, we just delete them so they don't confuse esrap
25+
delete node.typeAnnotation;
26+
delete node.typeParameters;
27+
delete node.returnType;
28+
delete node.accessibility;
29+
},
2030
Decorator(node) {
2131
e.typescript_invalid_feature(node, 'decorators (related TSC proposal is not stage 4 yet)');
2232
},
@@ -78,23 +88,12 @@ const visitors = {
7888
TSNonNullExpression(node, context) {
7989
return context.visit(node.expression);
8090
},
81-
TSTypeAnnotation() {
82-
// This isn't correct, strictly speaking, and could result in invalid ASTs (like an empty statement within function parameters),
83-
// but esrap, our printing tool, just ignores these AST nodes at invalid positions, so it's fine
84-
return b.empty;
85-
},
8691
TSInterfaceDeclaration() {
8792
return b.empty;
8893
},
8994
TSTypeAliasDeclaration() {
9095
return b.empty;
9196
},
92-
TSTypeParameterDeclaration() {
93-
return b.empty;
94-
},
95-
TSTypeParameterInstantiation() {
96-
return b.empty;
97-
},
9897
TSEnumDeclaration(node) {
9998
e.typescript_invalid_feature(node, 'enums');
10099
},

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,16 @@ export function analyze_component(root, source, options) {
698698
}
699699

700700
for (const node of analysis.module.ast.body) {
701-
if (node.type === 'ExportNamedDeclaration' && node.specifiers !== null && node.source == null) {
701+
if (
702+
node.type === 'ExportNamedDeclaration' &&
703+
// @ts-expect-error
704+
node.exportKind !== 'type' &&
705+
node.specifiers !== null &&
706+
node.source == null
707+
) {
702708
for (const specifier of node.specifiers) {
709+
// @ts-expect-error
710+
if (specifier.exportKind === 'type') continue;
703711
if (specifier.local.type !== 'Identifier') continue;
704712

705713
const binding = analysis.module.scope.get(specifier.local.name);

packages/svelte/src/compiler/phases/3-transform/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ export function transform_component(analysis, source, options) {
3333
: client_component(analysis, options);
3434

3535
const js_source_name = get_source_name(options.filename, options.outputFilename, 'input.svelte');
36+
37+
// @ts-expect-error
3638
const js = print(program, {
3739
// include source content; makes it easier/more robust looking up the source map code
3840
// (else esrap does return null for source and sourceMapContent which may trip up tooling)
3941
sourceMapContent: source,
4042
sourceMapSource: js_source_name
4143
});
44+
4245
merge_with_preprocessor_map(js, options, js_source_name);
4346

4447
const css =
@@ -92,6 +95,7 @@ export function transform_module(analysis, source, options) {
9295
}
9396

9497
return {
98+
// @ts-expect-error
9599
js: print(program, {
96100
// include source content; makes it easier/more robust looking up the source map code
97101
// (else esrap does return null for source and sourceMapContent which may trip up tooling)

packages/svelte/src/compiler/phases/scope.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,13 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
433433
},
434434

435435
ImportDeclaration(node, { state }) {
436+
// @ts-expect-error
437+
if (node.importKind === 'type') return;
438+
436439
for (const specifier of node.specifiers) {
440+
// @ts-expect-error
441+
if (specifier.importKind === 'type') continue;
442+
437443
state.scope.declare(specifier.local, 'normal', 'import', node);
438444
}
439445
},

packages/svelte/tests/runtime-runes/samples/typescript/main.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
declare module 'foobar' {}
2626
namespace SomeNamespace {
27-
export type Foo = true
27+
export type Foo = true;
2828
}
2929
3030
export function overload(a: boolean): boolean;

packages/svelte/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@
4040
"./tests/runtime-browser/test-ssr.ts",
4141
"./tests/*/samples/*/_config.js"
4242
],
43-
"exclude": ["./scripts/process-messages/templates/", "./src/compiler/optimizer/"]
43+
"exclude": [
44+
"./scripts/process-messages/templates/",
45+
"./scripts/_bundle.js",
46+
"./src/compiler/optimizer/"
47+
]
4448
}

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)