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: bump esrap, prevent malformed AST #14742

Merged
merged 8 commits into from
Dec 18, 2024
Merged
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/purple-donuts-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: bump esrap, prevent malformed AST
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"aria-query": "^5.3.1",
"axobject-query": "^4.1.0",
"esm-env": "^1.2.1",
"esrap": "^1.2.3",
"esrap": "^1.3.1",
"is-reference": "^3.0.3",
"locate-character": "^3.0.0",
"magic-string": "^0.30.11",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ function remove_this_param(node, context) {

/** @type {Visitors<any, null>} */
const visitors = {
_(node, context) {
context.next();

// TODO there may come a time when we decide to preserve type annotations.
// until that day comes, we just delete them so they don't confuse esrap
delete node.typeAnnotation;
delete node.typeParameters;
delete node.returnType;
delete node.accessibility;
},
Decorator(node) {
e.typescript_invalid_feature(node, 'decorators (related TSC proposal is not stage 4 yet)');
},
Expand Down Expand Up @@ -78,23 +88,12 @@ 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;
},
TSTypeAliasDeclaration() {
return b.empty;
},
TSTypeParameterDeclaration() {
return b.empty;
},
TSTypeParameterInstantiation() {
return b.empty;
},
TSEnumDeclaration(node) {
e.typescript_invalid_feature(node, 'enums');
},
Expand Down
10 changes: 9 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,16 @@ export function analyze_component(root, source, options) {
}

for (const node of analysis.module.ast.body) {
if (node.type === 'ExportNamedDeclaration' && node.specifiers !== null && node.source == null) {
if (
node.type === 'ExportNamedDeclaration' &&
// @ts-expect-error
node.exportKind !== 'type' &&
node.specifiers !== null &&
node.source == null
) {
for (const specifier of node.specifiers) {
// @ts-expect-error
if (specifier.exportKind === 'type') continue;
if (specifier.local.type !== 'Identifier') continue;

const binding = analysis.module.scope.get(specifier.local.name);
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/src/compiler/phases/3-transform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ export function transform_component(analysis, source, options) {
: client_component(analysis, options);

const js_source_name = get_source_name(options.filename, options.outputFilename, 'input.svelte');

// @ts-expect-error
const js = print(program, {
// include source content; makes it easier/more robust looking up the source map code
// (else esrap does return null for source and sourceMapContent which may trip up tooling)
sourceMapContent: source,
sourceMapSource: js_source_name
});

merge_with_preprocessor_map(js, options, js_source_name);

const css =
Expand Down Expand Up @@ -92,6 +95,7 @@ export function transform_module(analysis, source, options) {
}

return {
// @ts-expect-error
js: print(program, {
// include source content; makes it easier/more robust looking up the source map code
// (else esrap does return null for source and sourceMapContent which may trip up tooling)
Expand Down
6 changes: 6 additions & 0 deletions packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,13 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
},

ImportDeclaration(node, { state }) {
// @ts-expect-error
if (node.importKind === 'type') return;

for (const specifier of node.specifiers) {
// @ts-expect-error
if (specifier.importKind === 'type') continue;

state.scope.declare(specifier.local, 'normal', 'import', node);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
declare module 'foobar' {}
namespace SomeNamespace {
export type Foo = true
export type Foo = true;
}
export function overload(a: boolean): boolean;
Expand Down
6 changes: 5 additions & 1 deletion packages/svelte/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
"./tests/runtime-browser/test-ssr.ts",
"./tests/*/samples/*/_config.js"
],
"exclude": ["./scripts/process-messages/templates/", "./src/compiler/optimizer/"]
"exclude": [
"./scripts/process-messages/templates/",
"./scripts/_bundle.js",
"./src/compiler/optimizer/"
]
}
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading