From 1c67ebea590a7c5279bf8b6e14826b867c0f2d89 Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Fri, 19 Apr 2024 14:30:42 +0200 Subject: [PATCH] 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 d5c51d0d57..49d2f78260 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 4761052e1c..cf67189ff9 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 e42a34c0aa..cf9e9d9e82 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 794e80f66f..e7e5dfe244 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 a1b96c588c..ac9aa58067 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 a23219770b..6f219c7311 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 d03a7b553d..cd2bdc4fa7 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.