From 1c67ebea590a7c5279bf8b6e14826b867c0f2d89 Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 19 Apr 2024 14:30:42 +0200 Subject: [PATCH 1/5] include attributes of script tags in ast --- packages/svelte/src/compiler/legacy.js | 4 ++++ .../compiler/phases/1-parse/read/script.js | 3 ++- .../svelte/src/compiler/types/template.d.ts | 1 + .../samples/comment-before-script/output.json | 19 ++++++++++++++++++- .../samples/snippets/output.json | 19 ++++++++++++++++++- .../typescript-in-event-handler/output.json | 19 ++++++++++++++++++- packages/svelte/types/index.d.ts | 1 + 7 files changed, 62 insertions(+), 4 deletions(-) diff --git a/packages/svelte/src/compiler/legacy.js b/packages/svelte/src/compiler/legacy.js index d5c51d0d57a5..49d2f78260c8 100644 --- a/packages/svelte/src/compiler/legacy.js +++ b/packages/svelte/src/compiler/legacy.js @@ -86,11 +86,15 @@ export function convert(source, ast) { if (instance) { // @ts-ignore delete instance.parent; + // @ts-ignore + delete instance.attributes; } if (module) { // @ts-ignore delete module.parent; + // @ts-ignore + delete module.attributes; } return { diff --git a/packages/svelte/src/compiler/phases/1-parse/read/script.js b/packages/svelte/src/compiler/phases/1-parse/read/script.js index 4761052e1c5e..cf67189ff9ed 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/script.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/script.js @@ -63,6 +63,7 @@ export function read_script(parser, start, attributes) { end: parser.index, context: get_context(attributes), content: ast, - parent: null + parent: null, + attributes: attributes }; } diff --git a/packages/svelte/src/compiler/types/template.d.ts b/packages/svelte/src/compiler/types/template.d.ts index e42a34c0aa52..cf9e9d9e826c 100644 --- a/packages/svelte/src/compiler/types/template.d.ts +++ b/packages/svelte/src/compiler/types/template.d.ts @@ -467,6 +467,7 @@ export interface Script extends BaseNode { type: 'Script'; context: string; content: Program; + attributes: Array; } declare module 'estree' { diff --git a/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json b/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json index 794e80f66f01..e7e5dfe2443c 100644 --- a/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json +++ b/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json @@ -133,6 +133,23 @@ "value": "should not error out" } ] - } + }, + "attributes": [ + { + "type": "Attribute", + "start": 36, + "end": 45, + "name": "lang", + "value": [ + { + "start": 42, + "end": 44, + "type": "Text", + "raw": "ts", + "data": "ts" + } + ] + } + ] } } diff --git a/packages/svelte/tests/parser-modern/samples/snippets/output.json b/packages/svelte/tests/parser-modern/samples/snippets/output.json index a1b96c588c07..ac9aa580678a 100644 --- a/packages/svelte/tests/parser-modern/samples/snippets/output.json +++ b/packages/svelte/tests/parser-modern/samples/snippets/output.json @@ -206,6 +206,23 @@ }, "body": [], "sourceType": "module" - } + }, + "attributes": [ + { + "type": "Attribute", + "start": 8, + "end": 17, + "name": "lang", + "value": [ + { + "start": 14, + "end": 16, + "type": "Text", + "raw": "ts", + "data": "ts" + } + ] + } + ] } } diff --git a/packages/svelte/tests/parser-modern/samples/typescript-in-event-handler/output.json b/packages/svelte/tests/parser-modern/samples/typescript-in-event-handler/output.json index a23219770ba6..6f219c7311f2 100644 --- a/packages/svelte/tests/parser-modern/samples/typescript-in-event-handler/output.json +++ b/packages/svelte/tests/parser-modern/samples/typescript-in-event-handler/output.json @@ -477,6 +477,23 @@ } ], "sourceType": "module" - } + }, + "attributes": [ + { + "type": "Attribute", + "start": 8, + "end": 17, + "name": "lang", + "value": [ + { + "start": 14, + "end": 16, + "type": "Text", + "raw": "ts", + "data": "ts" + } + ] + } + ] } } diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index d03a7b553d46..cd2bdc4fa749 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -1690,6 +1690,7 @@ declare module 'svelte/compiler' { type: 'Script'; context: string; content: Program; + attributes: Array; } /** * The result of a preprocessor run. If the preprocessor does not return a result, it is assumed that the code is unchanged. From b1411a1010719900c2d4727faba2a32bd040609b Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 19 Apr 2024 14:31:02 +0200 Subject: [PATCH 2/5] include attributes of svelte:options tag in ast --- .../compiler/phases/1-parse/read/options.js | 3 +- .../svelte/src/compiler/types/template.d.ts | 1 + .../samples/options/input.svelte | 7 + .../parser-modern/samples/options/output.json | 193 ++++++++++++++++++ packages/svelte/types/index.d.ts | 1 + 5 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 packages/svelte/tests/parser-modern/samples/options/input.svelte create mode 100644 packages/svelte/tests/parser-modern/samples/options/output.json diff --git a/packages/svelte/src/compiler/phases/1-parse/read/options.js b/packages/svelte/src/compiler/phases/1-parse/read/options.js index 5d4f58868ff1..0204fc805a4b 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/options.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/options.js @@ -11,7 +11,8 @@ export default function read_options(node) { /** @type {import('#compiler').SvelteOptions} */ const component_options = { start: node.start, - end: node.end + end: node.end, + attributes: node.attributes }; if (!node) { diff --git a/packages/svelte/src/compiler/types/template.d.ts b/packages/svelte/src/compiler/types/template.d.ts index cf9e9d9e826c..2e4373116b29 100644 --- a/packages/svelte/src/compiler/types/template.d.ts +++ b/packages/svelte/src/compiler/types/template.d.ts @@ -93,6 +93,7 @@ export interface SvelteOptions { */ extend?: ArrowFunctionExpression | Identifier; }; + attributes: Array; } /** Static text */ diff --git a/packages/svelte/tests/parser-modern/samples/options/input.svelte b/packages/svelte/tests/parser-modern/samples/options/input.svelte new file mode 100644 index 000000000000..062c11916314 --- /dev/null +++ b/packages/svelte/tests/parser-modern/samples/options/input.svelte @@ -0,0 +1,7 @@ + + + + + diff --git a/packages/svelte/tests/parser-modern/samples/options/output.json b/packages/svelte/tests/parser-modern/samples/options/output.json new file mode 100644 index 000000000000..af68829455b0 --- /dev/null +++ b/packages/svelte/tests/parser-modern/samples/options/output.json @@ -0,0 +1,193 @@ +{ + "css": null, + "js": [], + "start": 0, + "end": 112, + "type": "Root", + "fragment": { + "type": "Fragment", + "nodes": [ + { + "type": "Text", + "start": 65, + "end": 67, + "raw": "\n\n", + "data": "\n\n" + }, + { + "type": "Text", + "start": 112, + "end": 114, + "raw": "\n\n", + "data": "\n\n" + } + ], + "transparent": false + }, + "options": { + "start": 0, + "end": 65, + "attributes": [ + { + "type": "Attribute", + "start": 16, + "end": 49, + "name": "customElement", + "value": [ + { + "start": 31, + "end": 48, + "type": "Text", + "raw": "my-custom-element", + "data": "my-custom-element", + "parent": null + } + ], + "parent": null, + "metadata": { + "dynamic": false, + "delegated": null + } + }, + { + "type": "Attribute", + "start": 50, + "end": 62, + "name": "runes", + "value": [ + { + "type": "ExpressionTag", + "start": 56, + "end": 62, + "expression": { + "type": "Literal", + "start": 57, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 57 + }, + "end": { + "line": 1, + "column": 61 + } + }, + "value": true, + "raw": "true" + }, + "parent": null, + "metadata": { + "contains_call_expression": false, + "dynamic": false + } + } + ], + "parent": null, + "metadata": { + "dynamic": false, + "delegated": null + } + } + ], + "customElement": { + "tag": "my-custom-element" + }, + "runes": true + }, + "module": { + "type": "Script", + "start": 67, + "end": 112, + "context": "module", + "content": { + "type": "Program", + "start": 102, + "end": 103, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + }, + "body": [], + "sourceType": "module" + }, + "attributes": [ + { + "type": "Attribute", + "start": 75, + "end": 91, + "name": "context", + "value": [ + { + "start": 84, + "end": 90, + "type": "Text", + "raw": "module", + "data": "module" + } + ] + }, + { + "type": "Attribute", + "start": 92, + "end": 101, + "name": "lang", + "value": [ + { + "start": 98, + "end": 100, + "type": "Text", + "raw": "ts", + "data": "ts" + } + ] + } + ] + }, + "instance": { + "type": "Script", + "start": 114, + "end": 142, + "context": "default", + "content": { + "type": "Program", + "start": 132, + "end": 133, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 0 + } + }, + "body": [], + "sourceType": "module" + }, + "attributes": [ + { + "type": "Attribute", + "start": 122, + "end": 131, + "name": "lang", + "value": [ + { + "start": 128, + "end": 130, + "type": "Text", + "raw": "ts", + "data": "ts" + } + ] + } + ] + } +} diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index cd2bdc4fa749..c9b9b942f5db 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -1316,6 +1316,7 @@ declare module 'svelte/compiler' { */ extend?: ArrowFunctionExpression | Identifier; }; + attributes: Array; } /** Static text */ From a09d68dc436fbcf1676ecdb75b9ffd8c0667fbc4 Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 19 Apr 2024 14:59:52 +0200 Subject: [PATCH 3/5] add changeset --- .changeset/serious-crabs-punch.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/serious-crabs-punch.md diff --git a/.changeset/serious-crabs-punch.md b/.changeset/serious-crabs-punch.md new file mode 100644 index 000000000000..850f0d03a788 --- /dev/null +++ b/.changeset/serious-crabs-punch.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +feat: include `script` and `svelte:options` attributes in ast From 6890743dfb0d1db68fa0c9292d3781b6d30510e0 Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 19 Apr 2024 17:49:22 +0200 Subject: [PATCH 4/5] improve types --- packages/svelte/src/compiler/phases/1-parse/read/options.js | 1 + packages/svelte/src/compiler/phases/1-parse/read/script.js | 1 + packages/svelte/src/compiler/types/template.d.ts | 4 ++-- packages/svelte/types/index.d.ts | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/svelte/src/compiler/phases/1-parse/read/options.js b/packages/svelte/src/compiler/phases/1-parse/read/options.js index 0204fc805a4b..c9262c54a8f8 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/options.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/options.js @@ -12,6 +12,7 @@ export default function read_options(node) { const component_options = { start: node.start, end: node.end, + // @ts-ignore attributes: node.attributes }; diff --git a/packages/svelte/src/compiler/phases/1-parse/read/script.js b/packages/svelte/src/compiler/phases/1-parse/read/script.js index cf67189ff9ed..cd9129e8b594 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/script.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/script.js @@ -64,6 +64,7 @@ export function read_script(parser, start, attributes) { context: get_context(attributes), content: ast, parent: null, + // @ts-ignore attributes: attributes }; } diff --git a/packages/svelte/src/compiler/types/template.d.ts b/packages/svelte/src/compiler/types/template.d.ts index 2e4373116b29..4ddee3096f82 100644 --- a/packages/svelte/src/compiler/types/template.d.ts +++ b/packages/svelte/src/compiler/types/template.d.ts @@ -93,7 +93,7 @@ export interface SvelteOptions { */ extend?: ArrowFunctionExpression | Identifier; }; - attributes: Array; + attributes: Attribute[]; } /** Static text */ @@ -468,7 +468,7 @@ export interface Script extends BaseNode { type: 'Script'; context: string; content: Program; - attributes: Array; + attributes: Attribute[]; } declare module 'estree' { diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index c9b9b942f5db..900bee5a7695 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -1316,7 +1316,7 @@ declare module 'svelte/compiler' { */ extend?: ArrowFunctionExpression | Identifier; }; - attributes: Array; + attributes: Attribute[]; } /** Static text */ @@ -1691,7 +1691,7 @@ declare module 'svelte/compiler' { type: 'Script'; context: string; content: Program; - attributes: Array; + attributes: Attribute[]; } /** * The result of a preprocessor run. If the preprocessor does not return a result, it is assumed that the code is unchanged. From 4299721e3080b663a8c23904d0ee779fb54e96b1 Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 19 Apr 2024 17:53:02 +0200 Subject: [PATCH 5/5] improve test --- .../samples/options/input.svelte | 2 +- .../parser-modern/samples/options/output.json | 21 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/svelte/tests/parser-modern/samples/options/input.svelte b/packages/svelte/tests/parser-modern/samples/options/input.svelte index 062c11916314..9e8253c50c99 100644 --- a/packages/svelte/tests/parser-modern/samples/options/input.svelte +++ b/packages/svelte/tests/parser-modern/samples/options/input.svelte @@ -3,5 +3,5 @@ - diff --git a/packages/svelte/tests/parser-modern/samples/options/output.json b/packages/svelte/tests/parser-modern/samples/options/output.json index af68829455b0..398c178cb43d 100644 --- a/packages/svelte/tests/parser-modern/samples/options/output.json +++ b/packages/svelte/tests/parser-modern/samples/options/output.json @@ -153,12 +153,12 @@ "instance": { "type": "Script", "start": 114, - "end": 142, + "end": 179, "context": "default", "content": { "type": "Program", - "start": 132, - "end": 133, + "start": 169, + "end": 170, "loc": { "start": { "line": 1, @@ -187,6 +187,21 @@ "data": "ts" } ] + }, + { + "type": "Attribute", + "start": 132, + "end": 168, + "name": "generics", + "value": [ + { + "start": 142, + "end": 167, + "type": "Text", + "raw": "T extends { foo: number }", + "data": "T extends { foo: number }" + } + ] } ] }