Skip to content

Commit

Permalink
feat: replace declaration property of SvelteConstTag with `declarat…
Browse files Browse the repository at this point in the history
…ions` property
  • Loading branch information
ota-meshi committed Jan 14, 2025
1 parent d1ac7f0 commit 6280a13
Show file tree
Hide file tree
Showing 10 changed files with 537 additions and 507 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/ast/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Check warning on line 268 in src/ast/html.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Remove in v2 and later.'
declarations: [ESTree.VariableDeclarator];
parent:
| SvelteProgram
| SvelteElement
Expand Down
16 changes: 15 additions & 1 deletion src/parser/converts/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Check warning on line 22 in src/parser/converts/const.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: '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]);
Expand Down
2 changes: 1 addition & 1 deletion src/visitor-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const svelteKeys: SvelteKeysType = {
SvelteLiteral: [],
SvelteMustacheTag: ["expression"],
SvelteDebugTag: ["identifiers"],
SvelteConstTag: ["declaration"],
SvelteConstTag: ["declarations"],
SvelteRenderTag: ["expression"],
SvelteIfBlock: ["expression", "children", "else"],
SvelteElseBlock: ["children"],
Expand Down
106 changes: 54 additions & 52 deletions tests/fixtures/parser/ast/at-const01-within-component-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Loading

0 comments on commit 6280a13

Please sign in to comment.