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
pull/14740/head
Rich Harris 9 months ago committed by GitHub
parent 36a437c2f9
commit c033dae049
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: bump esrap, prevent malformed AST

@ -147,7 +147,7 @@
"aria-query": "^5.3.1", "aria-query": "^5.3.1",
"axobject-query": "^4.1.0", "axobject-query": "^4.1.0",
"esm-env": "^1.2.1", "esm-env": "^1.2.1",
"esrap": "^1.2.3", "esrap": "^1.3.1",
"is-reference": "^3.0.3", "is-reference": "^3.0.3",
"locate-character": "^3.0.0", "locate-character": "^3.0.0",
"magic-string": "^0.30.11", "magic-string": "^0.30.11",

@ -17,6 +17,16 @@ function remove_this_param(node, context) {
/** @type {Visitors<any, null>} */ /** @type {Visitors<any, null>} */
const visitors = { 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) { Decorator(node) {
e.typescript_invalid_feature(node, 'decorators (related TSC proposal is not stage 4 yet)'); e.typescript_invalid_feature(node, 'decorators (related TSC proposal is not stage 4 yet)');
}, },
@ -78,23 +88,12 @@ const visitors = {
TSNonNullExpression(node, context) { TSNonNullExpression(node, context) {
return context.visit(node.expression); 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() { TSInterfaceDeclaration() {
return b.empty; return b.empty;
}, },
TSTypeAliasDeclaration() { TSTypeAliasDeclaration() {
return b.empty; return b.empty;
}, },
TSTypeParameterDeclaration() {
return b.empty;
},
TSTypeParameterInstantiation() {
return b.empty;
},
TSEnumDeclaration(node) { TSEnumDeclaration(node) {
e.typescript_invalid_feature(node, 'enums'); e.typescript_invalid_feature(node, 'enums');
}, },

@ -698,8 +698,16 @@ export function analyze_component(root, source, options) {
} }
for (const node of analysis.module.ast.body) { 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) { for (const specifier of node.specifiers) {
// @ts-expect-error
if (specifier.exportKind === 'type') continue;
if (specifier.local.type !== 'Identifier') continue; if (specifier.local.type !== 'Identifier') continue;
const binding = analysis.module.scope.get(specifier.local.name); const binding = analysis.module.scope.get(specifier.local.name);

@ -33,12 +33,15 @@ export function transform_component(analysis, source, options) {
: client_component(analysis, options); : client_component(analysis, options);
const js_source_name = get_source_name(options.filename, options.outputFilename, 'input.svelte'); const js_source_name = get_source_name(options.filename, options.outputFilename, 'input.svelte');
// @ts-expect-error
const js = print(program, { const js = print(program, {
// include source content; makes it easier/more robust looking up the source map code // 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) // (else esrap does return null for source and sourceMapContent which may trip up tooling)
sourceMapContent: source, sourceMapContent: source,
sourceMapSource: js_source_name sourceMapSource: js_source_name
}); });
merge_with_preprocessor_map(js, options, js_source_name); merge_with_preprocessor_map(js, options, js_source_name);
const css = const css =
@ -92,6 +95,7 @@ export function transform_module(analysis, source, options) {
} }
return { return {
// @ts-expect-error
js: print(program, { js: print(program, {
// include source content; makes it easier/more robust looking up the source map code // 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) // (else esrap does return null for source and sourceMapContent which may trip up tooling)

@ -433,7 +433,13 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
}, },
ImportDeclaration(node, { state }) { ImportDeclaration(node, { state }) {
// @ts-expect-error
if (node.importKind === 'type') return;
for (const specifier of node.specifiers) { for (const specifier of node.specifiers) {
// @ts-expect-error
if (specifier.importKind === 'type') continue;
state.scope.declare(specifier.local, 'normal', 'import', node); state.scope.declare(specifier.local, 'normal', 'import', node);
} }
}, },

@ -24,7 +24,7 @@
declare module 'foobar' {} declare module 'foobar' {}
namespace SomeNamespace { namespace SomeNamespace {
export type Foo = true export type Foo = true;
} }
export function overload(a: boolean): boolean; export function overload(a: boolean): boolean;

@ -40,5 +40,9 @@
"./tests/runtime-browser/test-ssr.ts", "./tests/runtime-browser/test-ssr.ts",
"./tests/*/samples/*/_config.js" "./tests/*/samples/*/_config.js"
], ],
"exclude": ["./scripts/process-messages/templates/", "./src/compiler/optimizer/"] "exclude": [
"./scripts/process-messages/templates/",
"./scripts/_bundle.js",
"./src/compiler/optimizer/"
]
} }

@ -84,8 +84,8 @@ importers:
specifier: ^1.2.1 specifier: ^1.2.1
version: 1.2.1 version: 1.2.1
esrap: esrap:
specifier: ^1.2.3 specifier: ^1.3.1
version: 1.2.3 version: 1.3.1
is-reference: is-reference:
specifier: ^3.0.3 specifier: ^3.0.3
version: 3.0.3 version: 3.0.3
@ -1111,8 +1111,8 @@ packages:
resolution: {integrity: sha512-YQLXUplAwJgCydQ78IMJywZCceoqk1oH01OERdSAJc/7U2AylwjhSCLDEtqwg811idIS/9fIU5GjG73IgjKMVg==} resolution: {integrity: sha512-YQLXUplAwJgCydQ78IMJywZCceoqk1oH01OERdSAJc/7U2AylwjhSCLDEtqwg811idIS/9fIU5GjG73IgjKMVg==}
engines: {node: '>=0.10'} engines: {node: '>=0.10'}
esrap@1.2.3: esrap@1.3.1:
resolution: {integrity: sha512-ZlQmCCK+n7SGoqo7DnfKaP1sJZa49P01/dXzmjCASSo04p72w8EksT2NMK8CEX8DhKsfJXANioIw8VyHNsBfvQ==} resolution: {integrity: sha512-KpAH3+QsDmtOP1KOW04CbD1PgzWsIHjB8tOCk3PCb8xzNGn8XkjI8zl80i09fmXdzQyaS8tcsKCCDzHF7AcowA==}
esrecurse@4.3.0: esrecurse@4.3.0:
resolution: {integrity: sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==} resolution: {integrity: sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==}
@ -3315,10 +3315,10 @@ snapshots:
dependencies: dependencies:
estraverse: 5.3.0 estraverse: 5.3.0
esrap@1.2.3: esrap@1.3.1:
dependencies: dependencies:
'@jridgewell/sourcemap-codec': 1.5.0 '@jridgewell/sourcemap-codec': 1.5.0
'@types/estree': 1.0.6 '@typescript-eslint/types': 8.2.0
esrecurse@4.3.0: esrecurse@4.3.0:
dependencies: dependencies:

Loading…
Cancel
Save