diff --git a/package.json b/package.json index b324c038..be1d1b6c 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "chai": "^5.0.0", "env-cmd": "^10.1.0", "esbuild": "^0.24.0", - "eslint": "~9.16.0", + "eslint": "~9.18.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-eslint-comments": "^3.2.0", "eslint-plugin-jsdoc": "^50.6.0", diff --git a/src/ast/html.ts b/src/ast/html.ts index 0b81b858..86e11f1d 100644 --- a/src/ast/html.ts +++ b/src/ast/html.ts @@ -262,7 +262,11 @@ export interface SvelteDebugTag extends BaseNode { /** Node of const tag. e.g. `{@const}` */ export interface SvelteConstTag extends BaseNode { type: "SvelteConstTag"; - declaration: ESTree.VariableDeclarator; + /** + * @deprecated Use `declarations` instead. + */ + declaration: ESTree.VariableDeclarator; // TODO Remove in v2 and later. + declarations: [ESTree.VariableDeclarator]; parent: | SvelteProgram | SvelteElement diff --git a/src/parser/converts/const.ts b/src/parser/converts/const.ts index d3c4a5d3..3d1ab55b 100644 --- a/src/parser/converts/const.ts +++ b/src/parser/converts/const.ts @@ -13,14 +13,28 @@ export function convertConstTag( const mustache: SvelteConstTag = { type: "SvelteConstTag", declaration: null as any, + declarations: [] as any, parent, ...ctx.getConvertLocation(node), }; + + // Link declaration and declarations for backward compatibility. + // TODO Remove in v2 and later. + Object.defineProperty(mustache, "declaration", { + get() { + return mustache.declarations[0]; + }, + set(value) { + mustache.declarations = [value]; + }, + enumerable: false, + }); + ctx.scriptLet.addVariableDeclarator( getDeclaratorFromConstTag(node), mustache, (declaration) => { - mustache.declaration = declaration; + mustache.declarations = [declaration]; }, ); const atConstStart = ctx.code.indexOf("@const", mustache.range[0]); diff --git a/src/visitor-keys.ts b/src/visitor-keys.ts index 3a7492c6..47eb204d 100644 --- a/src/visitor-keys.ts +++ b/src/visitor-keys.ts @@ -21,7 +21,7 @@ const svelteKeys: SvelteKeysType = { SvelteLiteral: [], SvelteMustacheTag: ["expression"], SvelteDebugTag: ["identifiers"], - SvelteConstTag: ["declaration"], + SvelteConstTag: ["declarations"], SvelteRenderTag: ["expression"], SvelteIfBlock: ["expression", "children", "else"], SvelteElseBlock: ["children"], diff --git a/tests/fixtures/parser/ast/at-const01-within-component-output.json b/tests/fixtures/parser/ast/at-const01-within-component-output.json index 19c0f35d..c5069790 100644 --- a/tests/fixtures/parser/ast/at-const01-within-component-output.json +++ b/tests/fixtures/parser/ast/at-const01-within-component-output.json @@ -300,59 +300,75 @@ }, { "type": "SvelteConstTag", - "declaration": { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "a", - "range": [ - 101, - 102 - ], - "loc": { - "start": { - "line": 6, - "column": 10 - }, - "end": { - "line": 6, - "column": 11 - } - } - }, - "init": { - "type": "BinaryExpression", - "left": { + "declarations": [ + { + "type": "VariableDeclarator", + "id": { "type": "Identifier", - "name": "b", + "name": "a", "range": [ - 105, - 106 + 101, + 102 ], "loc": { "start": { "line": 6, - "column": 14 + "column": 10 }, "end": { "line": 6, - "column": 15 + "column": 11 } } }, - "operator": "*", - "right": { - "type": "Literal", - "raw": "2", - "value": 2, + "init": { + "type": "BinaryExpression", + "left": { + "type": "Identifier", + "name": "b", + "range": [ + 105, + 106 + ], + "loc": { + "start": { + "line": 6, + "column": 14 + }, + "end": { + "line": 6, + "column": 15 + } + } + }, + "operator": "*", + "right": { + "type": "Literal", + "raw": "2", + "value": 2, + "range": [ + 109, + 110 + ], + "loc": { + "start": { + "line": 6, + "column": 18 + }, + "end": { + "line": 6, + "column": 19 + } + } + }, "range": [ - 109, + 105, 110 ], "loc": { "start": { "line": 6, - "column": 18 + "column": 14 }, "end": { "line": 6, @@ -361,35 +377,21 @@ } }, "range": [ - 105, + 101, 110 ], "loc": { "start": { "line": 6, - "column": 14 + "column": 10 }, "end": { "line": 6, "column": 19 } } - }, - "range": [ - 101, - 110 - ], - "loc": { - "start": { - "line": 6, - "column": 10 - }, - "end": { - "line": 6, - "column": 19 - } } - }, + ], "range": [ 93, 111 diff --git a/tests/fixtures/parser/ast/at-const02-output.json b/tests/fixtures/parser/ast/at-const02-output.json index 3895e5e3..29ab4248 100644 --- a/tests/fixtures/parser/ast/at-const02-output.json +++ b/tests/fixtures/parser/ast/at-const02-output.json @@ -210,117 +210,133 @@ "children": [ { "type": "SvelteConstTag", - "declaration": { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "area", - "range": [ - 68, - 72 - ], - "loc": { - "start": { - "line": 8, - "column": 0 - }, - "end": { - "line": 8, - "column": 4 - } - } - }, - "init": { - "type": "BinaryExpression", - "left": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", - "range": [ - 75, - 78 - ], - "loc": { - "start": { - "line": 10, - "column": 0 - }, - "end": { - "line": 10, - "column": 3 - } - } - }, - "optional": false, - "property": { - "type": "Identifier", - "name": "width", - "range": [ - 79, - 84 - ], - "loc": { - "start": { - "line": 10, - "column": 4 - }, - "end": { - "line": 10, - "column": 9 - } - } - }, + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "area", "range": [ - 75, - 84 + 68, + 72 ], "loc": { "start": { - "line": 10, + "line": 8, "column": 0 }, "end": { - "line": 10, - "column": 9 + "line": 8, + "column": 4 } } }, - "operator": "*", - "right": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", + "init": { + "type": "BinaryExpression", + "left": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 75, + 78 + ], + "loc": { + "start": { + "line": 10, + "column": 0 + }, + "end": { + "line": 10, + "column": 3 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "width", + "range": [ + 79, + 84 + ], + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 9 + } + } + }, "range": [ - 87, - 90 + 75, + 84 ], "loc": { "start": { - "line": 12, + "line": 10, "column": 0 }, "end": { - "line": 12, - "column": 3 + "line": 10, + "column": 9 } } }, - "optional": false, - "property": { - "type": "Identifier", - "name": "height", + "operator": "*", + "right": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 87, + 90 + ], + "loc": { + "start": { + "line": 12, + "column": 0 + }, + "end": { + "line": 12, + "column": 3 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "height", + "range": [ + 91, + 97 + ], + "loc": { + "start": { + "line": 12, + "column": 4 + }, + "end": { + "line": 12, + "column": 10 + } + } + }, "range": [ - 91, + 87, 97 ], "loc": { "start": { "line": 12, - "column": 4 + "column": 0 }, "end": { "line": 12, @@ -329,12 +345,12 @@ } }, "range": [ - 87, + 75, 97 ], "loc": { "start": { - "line": 12, + "line": 10, "column": 0 }, "end": { @@ -344,12 +360,12 @@ } }, "range": [ - 75, + 68, 97 ], "loc": { "start": { - "line": 10, + "line": 8, "column": 0 }, "end": { @@ -357,22 +373,8 @@ "column": 10 } } - }, - "range": [ - 68, - 97 - ], - "loc": { - "start": { - "line": 8, - "column": 0 - }, - "end": { - "line": 12, - "column": 10 - } } - }, + ], "range": [ 59, 99 diff --git a/tests/fixtures/parser/ast/at-const02-scope-output.json b/tests/fixtures/parser/ast/at-const02-scope-output.json index 5f9e06f8..d5921e79 100644 --- a/tests/fixtures/parser/ast/at-const02-scope-output.json +++ b/tests/fixtures/parser/ast/at-const02-scope-output.json @@ -322,117 +322,133 @@ "children": [ { "type": "SvelteConstTag", - "declaration": { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "area", - "range": [ - 68, - 72 - ], - "loc": { - "start": { - "line": 8, - "column": 0 - }, - "end": { - "line": 8, - "column": 4 - } - } - }, - "init": { - "type": "BinaryExpression", - "left": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", - "range": [ - 75, - 78 - ], - "loc": { - "start": { - "line": 10, - "column": 0 - }, - "end": { - "line": 10, - "column": 3 - } - } - }, - "optional": false, - "property": { - "type": "Identifier", - "name": "width", - "range": [ - 79, - 84 - ], - "loc": { - "start": { - "line": 10, - "column": 4 - }, - "end": { - "line": 10, - "column": 9 - } - } - }, + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "area", "range": [ - 75, - 84 + 68, + 72 ], "loc": { "start": { - "line": 10, + "line": 8, "column": 0 }, "end": { - "line": 10, - "column": 9 + "line": 8, + "column": 4 } } }, - "operator": "*", - "right": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", + "init": { + "type": "BinaryExpression", + "left": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 75, + 78 + ], + "loc": { + "start": { + "line": 10, + "column": 0 + }, + "end": { + "line": 10, + "column": 3 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "width", + "range": [ + 79, + 84 + ], + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 9 + } + } + }, "range": [ - 87, - 90 + 75, + 84 ], "loc": { "start": { - "line": 12, + "line": 10, "column": 0 }, "end": { - "line": 12, - "column": 3 + "line": 10, + "column": 9 } } }, - "optional": false, - "property": { - "type": "Identifier", - "name": "height", + "operator": "*", + "right": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 87, + 90 + ], + "loc": { + "start": { + "line": 12, + "column": 0 + }, + "end": { + "line": 12, + "column": 3 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "height", + "range": [ + 91, + 97 + ], + "loc": { + "start": { + "line": 12, + "column": 4 + }, + "end": { + "line": 12, + "column": 10 + } + } + }, "range": [ - 91, + 87, 97 ], "loc": { "start": { "line": 12, - "column": 4 + "column": 0 }, "end": { "line": 12, @@ -441,12 +457,12 @@ } }, "range": [ - 87, + 75, 97 ], "loc": { "start": { - "line": 12, + "line": 10, "column": 0 }, "end": { @@ -456,12 +472,12 @@ } }, "range": [ - 75, + 68, 97 ], "loc": { "start": { - "line": 10, + "line": 8, "column": 0 }, "end": { @@ -469,22 +485,8 @@ "column": 10 } } - }, - "range": [ - 68, - 97 - ], - "loc": { - "start": { - "line": 8, - "column": 0 - }, - "end": { - "line": 12, - "column": 10 - } } - }, + ], "range": [ 59, 99 diff --git a/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/01-output.json b/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/01-output.json index 3df0e787..777f0687 100644 --- a/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/01-output.json +++ b/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/01-output.json @@ -3,59 +3,61 @@ "body": [ { "type": "SvelteConstTag", - "declaration": { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "variable", - "range": [ - 8, - 16 - ], - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 16 + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "variable", + "range": [ + 8, + 16 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 16 + } } - } - }, - "init": { - "type": "Identifier", - "name": "assignment", + }, + "init": { + "type": "Identifier", + "name": "assignment", + "range": [ + 19, + 29 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 29 + } + } + }, "range": [ - 19, + 8, 29 ], "loc": { "start": { "line": 1, - "column": 19 + "column": 8 }, "end": { "line": 1, "column": 29 } } - }, - "range": [ - 8, - 29 - ], - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 29 - } } - }, + ], "range": [ 0, 30 diff --git a/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-output.json b/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-output.json index d235c719..4dbe7a6d 100644 --- a/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-output.json +++ b/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-output.json @@ -210,117 +210,133 @@ "children": [ { "type": "SvelteConstTag", - "declaration": { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "area", - "range": [ - 71, - 75 - ], - "loc": { - "start": { - "line": 6, - "column": 10 - }, - "end": { - "line": 6, - "column": 14 - } - } - }, - "init": { - "type": "BinaryExpression", - "left": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", - "range": [ - 78, - 81 - ], - "loc": { - "start": { - "line": 6, - "column": 17 - }, - "end": { - "line": 6, - "column": 20 - } - } - }, - "optional": false, - "property": { - "type": "Identifier", - "name": "width", - "range": [ - 82, - 87 - ], - "loc": { - "start": { - "line": 6, - "column": 21 - }, - "end": { - "line": 6, - "column": 26 - } - } - }, + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "area", "range": [ - 78, - 87 + 71, + 75 ], "loc": { "start": { "line": 6, - "column": 17 + "column": 10 }, "end": { "line": 6, - "column": 26 + "column": 14 } } }, - "operator": "*", - "right": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", + "init": { + "type": "BinaryExpression", + "left": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 78, + 81 + ], + "loc": { + "start": { + "line": 6, + "column": 17 + }, + "end": { + "line": 6, + "column": 20 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "width", + "range": [ + 82, + 87 + ], + "loc": { + "start": { + "line": 6, + "column": 21 + }, + "end": { + "line": 6, + "column": 26 + } + } + }, "range": [ - 90, - 93 + 78, + 87 ], "loc": { "start": { "line": 6, - "column": 29 + "column": 17 }, "end": { "line": 6, - "column": 32 + "column": 26 } } }, - "optional": false, - "property": { - "type": "Identifier", - "name": "height", + "operator": "*", + "right": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 90, + 93 + ], + "loc": { + "start": { + "line": 6, + "column": 29 + }, + "end": { + "line": 6, + "column": 32 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "height", + "range": [ + 94, + 100 + ], + "loc": { + "start": { + "line": 6, + "column": 33 + }, + "end": { + "line": 6, + "column": 39 + } + } + }, "range": [ - 94, + 90, 100 ], "loc": { "start": { "line": 6, - "column": 33 + "column": 29 }, "end": { "line": 6, @@ -329,13 +345,13 @@ } }, "range": [ - 90, + 78, 100 ], "loc": { "start": { "line": 6, - "column": 29 + "column": 17 }, "end": { "line": 6, @@ -344,35 +360,21 @@ } }, "range": [ - 78, + 71, 100 ], "loc": { "start": { "line": 6, - "column": 17 + "column": 10 }, "end": { "line": 6, "column": 39 } } - }, - "range": [ - 71, - 100 - ], - "loc": { - "start": { - "line": 6, - "column": 10 - }, - "end": { - "line": 6, - "column": 39 - } } - }, + ], "range": [ 63, 101 diff --git a/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-scope-output.json b/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-scope-output.json index 0b78d9c5..15de90f3 100644 --- a/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-scope-output.json +++ b/tests/fixtures/parser/ast/docs/template-syntax/10.1-at-const/02-scope-output.json @@ -322,117 +322,133 @@ "children": [ { "type": "SvelteConstTag", - "declaration": { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "area", - "range": [ - 71, - 75 - ], - "loc": { - "start": { - "line": 6, - "column": 10 - }, - "end": { - "line": 6, - "column": 14 - } - } - }, - "init": { - "type": "BinaryExpression", - "left": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", - "range": [ - 78, - 81 - ], - "loc": { - "start": { - "line": 6, - "column": 17 - }, - "end": { - "line": 6, - "column": 20 - } - } - }, - "optional": false, - "property": { - "type": "Identifier", - "name": "width", - "range": [ - 82, - 87 - ], - "loc": { - "start": { - "line": 6, - "column": 21 - }, - "end": { - "line": 6, - "column": 26 - } - } - }, + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "area", "range": [ - 78, - 87 + 71, + 75 ], "loc": { "start": { "line": 6, - "column": 17 + "column": 10 }, "end": { "line": 6, - "column": 26 + "column": 14 } } }, - "operator": "*", - "right": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "box", + "init": { + "type": "BinaryExpression", + "left": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 78, + 81 + ], + "loc": { + "start": { + "line": 6, + "column": 17 + }, + "end": { + "line": 6, + "column": 20 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "width", + "range": [ + 82, + 87 + ], + "loc": { + "start": { + "line": 6, + "column": 21 + }, + "end": { + "line": 6, + "column": 26 + } + } + }, "range": [ - 90, - 93 + 78, + 87 ], "loc": { "start": { "line": 6, - "column": 29 + "column": 17 }, "end": { "line": 6, - "column": 32 + "column": 26 } } }, - "optional": false, - "property": { - "type": "Identifier", - "name": "height", + "operator": "*", + "right": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "box", + "range": [ + 90, + 93 + ], + "loc": { + "start": { + "line": 6, + "column": 29 + }, + "end": { + "line": 6, + "column": 32 + } + } + }, + "optional": false, + "property": { + "type": "Identifier", + "name": "height", + "range": [ + 94, + 100 + ], + "loc": { + "start": { + "line": 6, + "column": 33 + }, + "end": { + "line": 6, + "column": 39 + } + } + }, "range": [ - 94, + 90, 100 ], "loc": { "start": { "line": 6, - "column": 33 + "column": 29 }, "end": { "line": 6, @@ -441,13 +457,13 @@ } }, "range": [ - 90, + 78, 100 ], "loc": { "start": { "line": 6, - "column": 29 + "column": 17 }, "end": { "line": 6, @@ -456,35 +472,21 @@ } }, "range": [ - 78, + 71, 100 ], "loc": { "start": { "line": 6, - "column": 17 + "column": 10 }, "end": { "line": 6, "column": 39 } } - }, - "range": [ - 71, - 100 - ], - "loc": { - "start": { - "line": 6, - "column": 10 - }, - "end": { - "line": 6, - "column": 39 - } } - }, + ], "range": [ 63, 101