Skip to content

feat: include script and svelte:options attributes in ast #11241

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

Merged
merged 5 commits into from
Apr 20, 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/serious-crabs-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: include `script` and `svelte:options` attributes in ast
4 changes: 4 additions & 0 deletions packages/svelte/src/compiler/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/read/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default function read_options(node) {
/** @type {import('#compiler').SvelteOptions} */
const component_options = {
start: node.start,
end: node.end
end: node.end,
// @ts-ignore
attributes: node.attributes
};

if (!node) {
Expand Down
4 changes: 3 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/read/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export function read_script(parser, start, attributes) {
end: parser.index,
context: get_context(attributes),
content: ast,
parent: null
parent: null,
// @ts-ignore
attributes: attributes
};
}
2 changes: 2 additions & 0 deletions packages/svelte/src/compiler/types/template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface SvelteOptions {
*/
extend?: ArrowFunctionExpression | Identifier;
};
attributes: Attribute[];
}

/** Static text */
Expand Down Expand Up @@ -467,6 +468,7 @@ export interface Script extends BaseNode {
type: 'Script';
context: string;
content: Program;
attributes: Attribute[];
}

declare module 'estree' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<svelte:options customElement="my-custom-element" runes={true} />

<script context="module" lang="ts">
</script>

<script lang="ts" generics="T extends { foo: number }">
</script>
208 changes: 208 additions & 0 deletions packages/svelte/tests/parser-modern/samples/options/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"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": 179,
"context": "default",
"content": {
"type": "Program",
"start": 169,
"end": 170,
"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"
}
]
},
{
"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 }"
}
]
}
]
}
}
19 changes: 18 additions & 1 deletion packages/svelte/tests/parser-modern/samples/snippets/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
}
2 changes: 2 additions & 0 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@ declare module 'svelte/compiler' {
*/
extend?: ArrowFunctionExpression | Identifier;
};
attributes: Attribute[];
}

/** Static text */
Expand Down Expand Up @@ -1690,6 +1691,7 @@ declare module 'svelte/compiler' {
type: 'Script';
context: string;
content: Program;
attributes: Attribute[];
}
/**
* The result of a preprocessor run. If the preprocessor does not return a result, it is assumed that the code is unchanged.
Expand Down
Loading