From f722d7c047706b4a051999b28d482d0069667da5 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Thu, 29 Feb 2024 19:02:01 +0900 Subject: [PATCH] fix: for svelte v5.0.0-next.69 & support optional `{@render}` (#483) --- .changeset/four-ears-appear.md | 5 + package.json | 50 +- src/ast/html.ts | 5 +- src/parser/converts/render.ts | 29 +- src/parser/svelte-ast-types.ts | 5 +- src/visitor-keys.ts | 2 +- .../ast/svelte5/docs/snippets/01-output.json | 92 ++- .../docs/snippets/01-scope-output.json | 92 ++- .../snippets/03-snippet-scope-output.json | 100 ++- .../snippets/04-snippet-scope-output.json | 90 +- .../snippets/05-snippet-scope-output.json | 176 ++-- ...passing-snippets-to-components-output.json | 30 +- .../parser/ast/svelte5/render01-output.json | 62 +- .../svelte5/render02-optional-input.svelte | 5 + .../ast/svelte5/render02-optional-output.json | 767 ++++++++++++++++++ .../render02-optional-scope-output.json | 475 +++++++++++ .../ast/svelte5/ts-snippet01-output.json | 46 +- .../svelte5/ts-snippet01-type-output.svelte | 2 +- 18 files changed, 1776 insertions(+), 257 deletions(-) create mode 100644 .changeset/four-ears-appear.md create mode 100644 tests/fixtures/parser/ast/svelte5/render02-optional-input.svelte create mode 100644 tests/fixtures/parser/ast/svelte5/render02-optional-output.json create mode 100644 tests/fixtures/parser/ast/svelte5/render02-optional-scope-output.json diff --git a/.changeset/four-ears-appear.md b/.changeset/four-ears-appear.md new file mode 100644 index 00000000..03c79b53 --- /dev/null +++ b/.changeset/four-ears-appear.md @@ -0,0 +1,5 @@ +--- +"svelte-eslint-parser": patch +--- + +fix: for svelte v5.0.0-next.68 & support optional `{@render}` diff --git a/package.json b/package.json index e9dc691f..e8b146bf 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version" }, "peerDependencies": { - "svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.65" + "svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.69" }, "peerDependenciesMeta": { "svelte": { @@ -58,7 +58,7 @@ "eslint-scope": "^7.2.2", "eslint-visitor-keys": "^3.4.3", "espree": "^9.6.1", - "postcss": "^8.4.32", + "postcss": "^8.4.35", "postcss-scss": "^4.0.9" }, "devDependencies": { @@ -67,47 +67,47 @@ "@changesets/get-release-plan": "^4.0.0", "@ota-meshi/eslint-plugin": "^0.15.2", "@types/benchmark": "^2.1.5", - "@types/chai": "^4.3.11", - "@types/eslint": "^8.44.9", + "@types/chai": "^4.3.12", + "@types/eslint": "^8.56.5", "@types/eslint-scope": "^3.7.7", "@types/eslint-visitor-keys": "^1.0.0", "@types/estree": "^1.0.5", "@types/mocha": "^10.0.6", - "@types/node": "^20.10.4", - "@types/semver": "^7.5.6", - "@typescript-eslint/eslint-plugin": "^7.0.0", + "@types/node": "^20.11.22", + "@types/semver": "^7.5.8", + "@typescript-eslint/eslint-plugin": "^7.1.0", "@typescript-eslint/parser": "~7.1.0", "@typescript-eslint/types": "~7.1.0", "benchmark": "^2.1.4", - "chai": "^4.3.10", + "chai": "^4.4.1", "env-cmd": "^10.1.0", - "esbuild": "^0.20.0", + "esbuild": "^0.20.1", "esbuild-register": "^3.5.0", - "eslint": "^8.55.0", + "eslint": "^8.57.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-eslint-comments": "^3.2.0", - "eslint-plugin-json-schema-validator": "^4.7.4", - "eslint-plugin-jsonc": "^2.11.1", - "eslint-plugin-n": "^16.4.0", - "eslint-plugin-node-dependencies": "^0.11.1", - "eslint-plugin-prettier": "^5.0.1", - "eslint-plugin-regexp": "^2.1.2", + "eslint-plugin-json-schema-validator": "^4.8.3", + "eslint-plugin-jsonc": "^2.13.0", + "eslint-plugin-n": "^16.6.2", + "eslint-plugin-node-dependencies": "^0.11.2", + "eslint-plugin-prettier": "^5.1.3", + "eslint-plugin-regexp": "^2.2.0", "eslint-plugin-svelte": "^2.35.1", - "eslint-plugin-yml": "^1.11.0", + "eslint-plugin-yml": "^1.12.2", "estree-walker": "^3.0.3", "locate-character": "^3.0.0", - "magic-string": "^0.30.5", - "mocha": "^10.2.0", + "magic-string": "^0.30.7", + "mocha": "^10.3.0", "mocha-chai-jest-snapshot": "^1.1.4", "nyc": "^15.1.0", "prettier": "~3.2.5", - "prettier-plugin-pkg": "^0.18.0", - "prettier-plugin-svelte": "^3.1.2", + "prettier-plugin-pkg": "^0.18.1", + "prettier-plugin-svelte": "^3.2.2", "rimraf": "^5.0.5", - "semver": "^7.5.4", - "svelte": "^5.0.0-next.65", - "svelte2tsx": "^0.7.0", - "typescript": "~5.3.0", + "semver": "^7.6.0", + "svelte": "^5.0.0-next.69", + "svelte2tsx": "^0.7.3", + "typescript": "~5.3.3", "typescript-eslint-parser-for-extra-files": "^0.6.0" }, "publishConfig": { diff --git a/src/ast/html.ts b/src/ast/html.ts index 65fcda64..221c0276 100644 --- a/src/ast/html.ts +++ b/src/ast/html.ts @@ -276,8 +276,9 @@ export interface SvelteConstTag extends BaseNode { /** Node of render tag. e.g. `{@render}` */ export interface SvelteRenderTag extends BaseNode { type: "SvelteRenderTag"; - callee: ESTree.Identifier; - arguments: (ESTree.Expression | ESTree.SpreadElement)[]; + expression: + | ESTree.SimpleCallExpression + | (ESTree.ChainExpression & { expression: ESTree.SimpleCallExpression }); parent: | SvelteProgram | SvelteElement diff --git a/src/parser/converts/render.ts b/src/parser/converts/render.ts index d5ef6ab6..27aa62b1 100644 --- a/src/parser/converts/render.ts +++ b/src/parser/converts/render.ts @@ -12,29 +12,24 @@ export function convertRenderTag( ): SvelteRenderTag { const mustache: SvelteRenderTag = { type: "SvelteRenderTag", - callee: null as any, - arguments: [], + expression: null as any, parent, ...ctx.getConvertLocation(node), }; - const calleeRange = getWithLoc(node.expression); - const closeParenIndex = ctx.code.indexOf( - ")", - node.arguments.length - ? getWithLoc(node.arguments[node.arguments.length - 1]).end - : calleeRange.end, - ); + const callRange = getWithLoc(node.expression); ctx.scriptLet.addExpressionFromRange( - [calleeRange.start, closeParenIndex + 1], + [callRange.start, callRange.end], mustache, null, - (expression: ESTree.SimpleCallExpression) => { - mustache.callee = expression.callee as ESTree.Identifier; - (mustache.callee as any).parent = mustache; - for (const argument of expression.arguments) { - mustache.arguments.push(argument); - (argument as any).parent = mustache; - } + ( + expression: + | ESTree.SimpleCallExpression + | (ESTree.ChainExpression & { + expression: ESTree.SimpleCallExpression; + }), + ) => { + mustache.expression = expression; + (mustache.expression as any).parent = mustache; }, ); const atRenderStart = ctx.code.indexOf("@render", mustache.range[0]); diff --git a/src/parser/svelte-ast-types.ts b/src/parser/svelte-ast-types.ts index 78ec68c8..35ac239c 100644 --- a/src/parser/svelte-ast-types.ts +++ b/src/parser/svelte-ast-types.ts @@ -60,8 +60,9 @@ export interface ConstTag extends BaseNode { } export interface RenderTag extends BaseNode { type: "RenderTag"; - expression: ESTree.Identifier; - arguments: (ESTree.Expression | ESTree.SpreadElement)[]; + expression: + | ESTree.SimpleCallExpression + | (ESTree.ChainExpression & { expression: ESTree.SimpleCallExpression }); } export interface IfBlock extends BaseNode { type: "IfBlock"; diff --git a/src/visitor-keys.ts b/src/visitor-keys.ts index 99e5a532..83c3b076 100644 --- a/src/visitor-keys.ts +++ b/src/visitor-keys.ts @@ -22,7 +22,7 @@ const svelteKeys: SvelteKeysType = { SvelteMustacheTag: ["expression"], SvelteDebugTag: ["identifiers"], SvelteConstTag: ["declaration"], - SvelteRenderTag: ["callee", "arguments"], + SvelteRenderTag: ["expression"], SvelteIfBlock: ["expression", "children", "else"], SvelteElseBlock: ["children"], SvelteEachBlock: [ diff --git a/tests/fixtures/parser/ast/svelte5/docs/snippets/01-output.json b/tests/fixtures/parser/ast/svelte5/docs/snippets/01-output.json index 7124ed40..6a94355a 100644 --- a/tests/fixtures/parser/ast/svelte5/docs/snippets/01-output.json +++ b/tests/fixtures/parser/ast/svelte5/docs/snippets/01-output.json @@ -1126,32 +1126,50 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "image", + "range": [ + 288, + 293 + ], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 24 + } + } + } + ], + "callee": { "type": "Identifier", - "name": "image", + "name": "figure", "range": [ - 288, - 293 + 281, + 287 ], "loc": { "start": { "line": 16, - "column": 19 + "column": 12 }, "end": { "line": 16, - "column": 24 + "column": 18 } } - } - ], - "callee": { - "type": "Identifier", - "name": "figure", + }, + "optional": false, "range": [ 281, - 287 + 294 ], "loc": { "start": { @@ -1160,7 +1178,7 @@ }, "end": { "line": 16, - "column": 18 + "column": 25 } } }, @@ -1237,32 +1255,50 @@ "children": [ { "type": "SvelteRenderTag", - "arguments": [ - { + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "image", + "range": [ + 330, + 335 + ], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 23 + } + } + } + ], + "callee": { "type": "Identifier", - "name": "image", + "name": "figure", "range": [ - 330, - 335 + 323, + 329 ], "loc": { "start": { "line": 19, - "column": 18 + "column": 11 }, "end": { "line": 19, - "column": 23 + "column": 17 } } - } - ], - "callee": { - "type": "Identifier", - "name": "figure", + }, + "optional": false, "range": [ 323, - 329 + 336 ], "loc": { "start": { @@ -1271,7 +1307,7 @@ }, "end": { "line": 19, - "column": 17 + "column": 24 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/docs/snippets/01-scope-output.json b/tests/fixtures/parser/ast/svelte5/docs/snippets/01-scope-output.json index 79dbc140..fb168cad 100644 --- a/tests/fixtures/parser/ast/svelte5/docs/snippets/01-scope-output.json +++ b/tests/fixtures/parser/ast/svelte5/docs/snippets/01-scope-output.json @@ -1254,32 +1254,50 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "image", + "range": [ + 288, + 293 + ], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 24 + } + } + } + ], + "callee": { "type": "Identifier", - "name": "image", + "name": "figure", "range": [ - 288, - 293 + 281, + 287 ], "loc": { "start": { "line": 16, - "column": 19 + "column": 12 }, "end": { "line": 16, - "column": 24 + "column": 18 } } - } - ], - "callee": { - "type": "Identifier", - "name": "figure", + }, + "optional": false, "range": [ 281, - 287 + 294 ], "loc": { "start": { @@ -1288,7 +1306,7 @@ }, "end": { "line": 16, - "column": 18 + "column": 25 } } }, @@ -1365,32 +1383,50 @@ "children": [ { "type": "SvelteRenderTag", - "arguments": [ - { + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "image", + "range": [ + 330, + 335 + ], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 23 + } + } + } + ], + "callee": { "type": "Identifier", - "name": "image", + "name": "figure", "range": [ - 330, - 335 + 323, + 329 ], "loc": { "start": { "line": 19, - "column": 18 + "column": 11 }, "end": { "line": 19, - "column": 23 + "column": 17 } } - } - ], - "callee": { - "type": "Identifier", - "name": "figure", + }, + "optional": false, "range": [ 323, - 329 + 336 ], "loc": { "start": { @@ -1399,7 +1435,7 @@ }, "end": { "line": 19, - "column": 17 + "column": 24 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/docs/snippets/03-snippet-scope-output.json b/tests/fixtures/parser/ast/svelte5/docs/snippets/03-snippet-scope-output.json index 0ee78980..363e65cb 100644 --- a/tests/fixtures/parser/ast/svelte5/docs/snippets/03-snippet-scope-output.json +++ b/tests/fixtures/parser/ast/svelte5/docs/snippets/03-snippet-scope-output.json @@ -578,33 +578,51 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { - "type": "Literal", - "raw": "'alice'", - "value": "alice", + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Literal", + "raw": "'alice'", + "value": "alice", + "range": [ + 159, + 166 + ], + "loc": { + "start": { + "line": 9, + "column": 15 + }, + "end": { + "line": 9, + "column": 22 + } + } + } + ], + "callee": { + "type": "Identifier", + "name": "hello", "range": [ - 159, - 166 + 153, + 158 ], "loc": { "start": { "line": 9, - "column": 15 + "column": 9 }, "end": { "line": 9, - "column": 22 + "column": 14 } } - } - ], - "callee": { - "type": "Identifier", - "name": "hello", + }, + "optional": false, "range": [ 153, - 158 + 167 ], "loc": { "start": { @@ -613,7 +631,7 @@ }, "end": { "line": 9, - "column": 14 + "column": 23 } } }, @@ -652,33 +670,51 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { - "type": "Literal", - "raw": "'bob'", - "value": "bob", + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Literal", + "raw": "'bob'", + "value": "bob", + "range": [ + 184, + 189 + ], + "loc": { + "start": { + "line": 10, + "column": 15 + }, + "end": { + "line": 10, + "column": 20 + } + } + } + ], + "callee": { + "type": "Identifier", + "name": "hello", "range": [ - 184, - 189 + 178, + 183 ], "loc": { "start": { "line": 10, - "column": 15 + "column": 9 }, "end": { "line": 10, - "column": 20 + "column": 14 } } - } - ], - "callee": { - "type": "Identifier", - "name": "hello", + }, + "optional": false, "range": [ 178, - 183 + 190 ], "loc": { "start": { @@ -687,7 +723,7 @@ }, "end": { "line": 10, - "column": 14 + "column": 21 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/docs/snippets/04-snippet-scope-output.json b/tests/fixtures/parser/ast/svelte5/docs/snippets/04-snippet-scope-output.json index 6631c8db..cebeac78 100644 --- a/tests/fixtures/parser/ast/svelte5/docs/snippets/04-snippet-scope-output.json +++ b/tests/fixtures/parser/ast/svelte5/docs/snippets/04-snippet-scope-output.json @@ -175,13 +175,31 @@ }, { "type": "SvelteRenderTag", - "arguments": [], - "callee": { - "type": "Identifier", - "name": "y", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "y", + "range": [ + 88, + 89 + ], + "loc": { + "start": { + "line": 6, + "column": 11 + }, + "end": { + "line": 6, + "column": 12 + } + } + }, + "optional": false, "range": [ 88, - 89 + 91 ], "loc": { "start": { @@ -190,7 +208,7 @@ }, "end": { "line": 6, - "column": 12 + "column": 14 } } }, @@ -300,13 +318,31 @@ }, { "type": "SvelteRenderTag", - "arguments": [], - "callee": { - "type": "Identifier", - "name": "y", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "y", + "range": [ + 166, + 167 + ], + "loc": { + "start": { + "line": 10, + "column": 10 + }, + "end": { + "line": 10, + "column": 11 + } + } + }, + "optional": false, "range": [ 166, - 167 + 169 ], "loc": { "start": { @@ -315,7 +351,7 @@ }, "end": { "line": 10, - "column": 11 + "column": 13 } } }, @@ -441,13 +477,31 @@ }, { "type": "SvelteRenderTag", - "arguments": [], - "callee": { - "type": "Identifier", - "name": "x", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "x", + "range": [ + 242, + 243 + ], + "loc": { + "start": { + "line": 14, + "column": 9 + }, + "end": { + "line": 14, + "column": 10 + } + } + }, + "optional": false, "range": [ 242, - 243 + 245 ], "loc": { "start": { @@ -456,7 +510,7 @@ }, "end": { "line": 14, - "column": 10 + "column": 12 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/docs/snippets/05-snippet-scope-output.json b/tests/fixtures/parser/ast/svelte5/docs/snippets/05-snippet-scope-output.json index 7a35b3e2..fc4cc810 100644 --- a/tests/fixtures/parser/ast/svelte5/docs/snippets/05-snippet-scope-output.json +++ b/tests/fixtures/parser/ast/svelte5/docs/snippets/05-snippet-scope-output.json @@ -359,69 +359,87 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { - "type": "BinaryExpression", - "left": { - "type": "Identifier", - "name": "n", - "range": [ - 131, - 132 - ], - "loc": { - "start": { - "line": 8, - "column": 21 - }, - "end": { - "line": 8, - "column": 22 + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "BinaryExpression", + "left": { + "type": "Identifier", + "name": "n", + "range": [ + 131, + 132 + ], + "loc": { + "start": { + "line": 8, + "column": 21 + }, + "end": { + "line": 8, + "column": 22 + } } - } - }, - "operator": "-", - "right": { - "type": "Literal", - "raw": "1", - "value": 1, + }, + "operator": "-", + "right": { + "type": "Literal", + "raw": "1", + "value": 1, + "range": [ + 135, + 136 + ], + "loc": { + "start": { + "line": 8, + "column": 25 + }, + "end": { + "line": 8, + "column": 26 + } + } + }, "range": [ - 135, + 131, 136 ], "loc": { "start": { "line": 8, - "column": 25 + "column": 21 }, "end": { "line": 8, "column": 26 } } - }, + } + ], + "callee": { + "type": "Identifier", + "name": "countdown", "range": [ - 131, - 136 + 121, + 130 ], "loc": { "start": { "line": 8, - "column": 21 + "column": 11 }, "end": { "line": 8, - "column": 26 + "column": 20 } } - } - ], - "callee": { - "type": "Identifier", - "name": "countdown", + }, + "optional": false, "range": [ 121, - 130 + 137 ], "loc": { "start": { @@ -430,7 +448,7 @@ }, "end": { "line": 8, - "column": 20 + "column": 27 } } }, @@ -456,13 +474,31 @@ "children": [ { "type": "SvelteRenderTag", - "arguments": [], - "callee": { - "type": "Identifier", - "name": "blastoff", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "blastoff", + "range": [ + 159, + 167 + ], + "loc": { + "start": { + "line": 10, + "column": 11 + }, + "end": { + "line": 10, + "column": 19 + } + } + }, + "optional": false, "range": [ 159, - 167 + 169 ], "loc": { "start": { @@ -471,7 +507,7 @@ }, "end": { "line": 10, - "column": 19 + "column": 21 } } }, @@ -595,33 +631,51 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { - "type": "Literal", - "raw": "10", - "value": 10, + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Literal", + "raw": "10", + "value": 10, + "range": [ + 209, + 211 + ], + "loc": { + "start": { + "line": 14, + "column": 19 + }, + "end": { + "line": 14, + "column": 21 + } + } + } + ], + "callee": { + "type": "Identifier", + "name": "countdown", "range": [ - 209, - 211 + 199, + 208 ], "loc": { "start": { "line": 14, - "column": 19 + "column": 9 }, "end": { "line": 14, - "column": 21 + "column": 18 } } - } - ], - "callee": { - "type": "Identifier", - "name": "countdown", + }, + "optional": false, "range": [ 199, - 208 + 212 ], "loc": { "start": { @@ -630,7 +684,7 @@ }, "end": { "line": 14, - "column": 18 + "column": 22 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/docs/snippets/09-passing-snippets-to-components-output.json b/tests/fixtures/parser/ast/svelte5/docs/snippets/09-passing-snippets-to-components-output.json index 190ffa13..4364306d 100644 --- a/tests/fixtures/parser/ast/svelte5/docs/snippets/09-passing-snippets-to-components-output.json +++ b/tests/fixtures/parser/ast/svelte5/docs/snippets/09-passing-snippets-to-components-output.json @@ -539,13 +539,31 @@ "children": [ { "type": "SvelteRenderTag", - "arguments": [], - "callee": { - "type": "Identifier", - "name": "children", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "children", + "range": [ + 111, + 119 + ], + "loc": { + "start": { + "line": 8, + "column": 16 + }, + "end": { + "line": 8, + "column": 24 + } + } + }, + "optional": false, "range": [ 111, - 119 + 121 ], "loc": { "start": { @@ -554,7 +572,7 @@ }, "end": { "line": 8, - "column": 24 + "column": 26 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/render01-output.json b/tests/fixtures/parser/ast/svelte5/render01-output.json index 9cdcf70c..fd1a2433 100644 --- a/tests/fixtures/parser/ast/svelte5/render01-output.json +++ b/tests/fixtures/parser/ast/svelte5/render01-output.json @@ -338,16 +338,34 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { - "type": "CallExpression", - "arguments": [], - "callee": { - "type": "Identifier", - "name": "bar", + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "bar", + "range": [ + 98, + 101 + ], + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 16 + } + } + }, + "optional": false, "range": [ 98, - 101 + 103 ], "loc": { "start": { @@ -356,33 +374,33 @@ }, "end": { "line": 9, - "column": 16 + "column": 18 } } - }, - "optional": false, + } + ], + "callee": { + "type": "Identifier", + "name": "foo", "range": [ - 98, - 103 + 94, + 97 ], "loc": { "start": { "line": 9, - "column": 13 + "column": 9 }, "end": { "line": 9, - "column": 18 + "column": 12 } } - } - ], - "callee": { - "type": "Identifier", - "name": "foo", + }, + "optional": false, "range": [ 94, - 97 + 104 ], "loc": { "start": { @@ -391,7 +409,7 @@ }, "end": { "line": 9, - "column": 12 + "column": 19 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/render02-optional-input.svelte b/tests/fixtures/parser/ast/svelte5/render02-optional-input.svelte new file mode 100644 index 00000000..cfc350b5 --- /dev/null +++ b/tests/fixtures/parser/ast/svelte5/render02-optional-input.svelte @@ -0,0 +1,5 @@ + + +{@render foo?.()} diff --git a/tests/fixtures/parser/ast/svelte5/render02-optional-output.json b/tests/fixtures/parser/ast/svelte5/render02-optional-output.json new file mode 100644 index 00000000..9c7d0f02 --- /dev/null +++ b/tests/fixtures/parser/ast/svelte5/render02-optional-output.json @@ -0,0 +1,767 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteScriptElement", + "name": { + "type": "SvelteName", + "name": "script", + "range": [ + 1, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 0, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "body": [ + { + "type": "VariableDeclaration", + "kind": "const", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "ObjectPattern", + "properties": [ + { + "type": "Property", + "kind": "init", + "computed": false, + "key": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "method": false, + "shorthand": true, + "value": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + ], + "range": [ + 16, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + "init": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "$props", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + "optional": false, + "range": [ + 26, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 25 + } + } + }, + "range": [ + 16, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + ], + "range": [ + 10, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 26 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 36, + 45 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "range": [ + 0, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "SvelteText", + "value": "\n\n", + "range": [ + 45, + 47 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 5, + "column": 0 + } + } + }, + { + "type": "SvelteRenderTag", + "expression": { + "type": "ChainExpression", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "foo", + "range": [ + 56, + 59 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 12 + } + } + }, + "optional": true, + "range": [ + 56, + 63 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 16 + } + } + }, + "range": [ + 56, + 63 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 16 + } + } + }, + "range": [ + 47, + 64 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 17 + } + } + } + ], + "sourceType": "module", + "comments": [], + "tokens": [ + { + "type": "Punctuator", + "value": "<", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 1, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Keyword", + "value": "const", + "range": [ + 10, + 15 + ], + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 6 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Identifier", + "value": "$props", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 34, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 36, + 37 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 37, + 38 + ], + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 38, + 44 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 44, + 45 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "HTMLText", + "value": "\n\n", + "range": [ + 45, + 47 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 5, + "column": 0 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 47, + 48 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + }, + { + "type": "MustacheKeyword", + "value": "@render", + "range": [ + 48, + 55 + ], + "loc": { + "start": { + "line": 5, + "column": 1 + }, + "end": { + "line": 5, + "column": 8 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 56, + 59 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "?.", + "range": [ + 59, + 61 + ], + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 61, + 62 + ], + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 62, + 63 + ], + "loc": { + "start": { + "line": 5, + "column": 15 + }, + "end": { + "line": 5, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 63, + 64 + ], + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 17 + } + } + } + ], + "range": [ + 0, + 65 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/svelte5/render02-optional-scope-output.json b/tests/fixtures/parser/ast/svelte5/render02-optional-scope-output.json new file mode 100644 index 00000000..e4cc516f --- /dev/null +++ b/tests/fixtures/parser/ast/svelte5/render02-optional-scope-output.json @@ -0,0 +1,475 @@ +{ + "type": "global", + "variables": [ + { + "name": "$$slots", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$props", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$restProps", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$state", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$derived", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$effect", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$props", + "identifiers": [], + "defs": [], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "$props", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ] + }, + { + "name": "$inspect", + "identifiers": [], + "defs": [], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "module", + "variables": [ + { + "name": "foo", + "identifiers": [ + { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + ], + "defs": [ + { + "type": "Variable", + "name": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "node": { + "type": "VariableDeclarator", + "id": { + "type": "ObjectPattern", + "properties": [ + { + "type": "Property", + "kind": "init", + "computed": false, + "key": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "method": false, + "shorthand": true, + "value": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + ], + "range": [ + 16, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + "init": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "$props", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + "optional": false, + "range": [ + 26, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 25 + } + } + }, + "range": [ + 16, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "from": "module", + "init": true, + "resolved": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + }, + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 56, + 59 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 12 + } + } + }, + "from": "module", + "init": null, + "resolved": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + } + ] + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + "from": "module", + "init": true, + "resolved": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + }, + { + "identifier": { + "type": "Identifier", + "name": "$props", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + "from": "module", + "init": null, + "resolved": null + }, + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 56, + 59 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 12 + } + } + }, + "from": "module", + "init": null, + "resolved": { + "type": "Identifier", + "name": "foo", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + } + ], + "childScopes": [], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "$props", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ] + } + ], + "through": [] +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/svelte5/ts-snippet01-output.json b/tests/fixtures/parser/ast/svelte5/ts-snippet01-output.json index e13eebb3..d743739f 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-snippet01-output.json +++ b/tests/fixtures/parser/ast/svelte5/ts-snippet01-output.json @@ -444,32 +444,50 @@ }, { "type": "SvelteRenderTag", - "arguments": [ - { + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "msg", + "range": [ + 114, + 117 + ], + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 16 + } + } + } + ], + "callee": { "type": "Identifier", - "name": "msg", + "name": "foo", "range": [ - 114, - 117 + 110, + 113 ], "loc": { "start": { "line": 9, - "column": 13 + "column": 9 }, "end": { "line": 9, - "column": 16 + "column": 12 } } - } - ], - "callee": { - "type": "Identifier", - "name": "foo", + }, + "optional": false, "range": [ 110, - 113 + 118 ], "loc": { "start": { @@ -478,7 +496,7 @@ }, "end": { "line": 9, - "column": 12 + "column": 17 } } }, diff --git a/tests/fixtures/parser/ast/svelte5/ts-snippet01-type-output.svelte b/tests/fixtures/parser/ast/svelte5/ts-snippet01-type-output.svelte index b1e7053e..55ea0d0a 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-snippet01-type-output.svelte +++ b/tests/fixtures/parser/ast/svelte5/ts-snippet01-type-output.svelte @@ -6,4 +6,4 @@

{msg}

{/snippet} -{@render foo(msg)} +{@render foo(msg)}