From a3b2bdfdbd239920253782a0043a5fc6b811a61c Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 25 Dec 2022 16:10:02 +0900 Subject: [PATCH 1/3] fix: parsing errors for blocks and js comments --- src/context/script-let.ts | 21 +- .../parser/ast/each-with-comment-input.svelte | 3 + .../parser/ast/each-with-comment-output.json | 407 +++++++++++++ .../ast/each-with-comment-scope-output.json | 357 +++++++++++ ...ch-with-comment-spaced-comment-result.json | 8 + ...issue327-eslint-plugin-svelte-input.svelte | 3 + ...-eslint-plugin-svelte-no-undef-result.json | 20 + ...t-plugin-svelte-no-unused-vars-result.json | 8 + .../issue327-eslint-plugin-svelte-output.json | 553 ++++++++++++++++++ ...327-eslint-plugin-svelte-scope-output.json | 494 ++++++++++++++++ ...t-plugin-svelte-spaced-comment-result.json | 8 + 11 files changed, 1873 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/parser/ast/each-with-comment-input.svelte create mode 100644 tests/fixtures/parser/ast/each-with-comment-output.json create mode 100644 tests/fixtures/parser/ast/each-with-comment-scope-output.json create mode 100644 tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json create mode 100644 tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-input.svelte create mode 100644 tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-undef-result.json create mode 100644 tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-unused-vars-result.json create mode 100644 tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-output.json create mode 100644 tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-scope-output.json create mode 100644 tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-spaced-comment-result.json diff --git a/src/context/script-let.ts b/src/context/script-let.ts index 916b3ef1..f57ef1b5 100644 --- a/src/context/script-let.ts +++ b/src/context/script-let.ts @@ -88,15 +88,16 @@ function getNodeRange( node.trailingComments[node.trailingComments.length - 1] ).end; } - if (start != null && end != null) { - return [start, end]; - } - if ("range" in node) { - return [start ?? node.range![0], end ?? node.range![1]]; - } - const loc = getWithLoc(node); - return [start ?? loc.start, end ?? loc.end]; + const loc = + "range" in node + ? { start: node.range![0], end: node.range![1] } + : getWithLoc(node); + + return [ + start ? Math.min(start, loc.start) : loc.start, + end ? Math.max(end, loc.end) : loc.end, + ]; } type RestoreCallback = { @@ -702,7 +703,9 @@ export class ScriptLetContext { (t) => restoreCallback.start <= t.range[0] ), }; - + if (startIndex.comment === -1) { + startIndex.comment = comments.length; + } const endIndex = { token: tokens.findIndex( (t) => restoreCallback.end < t.range[1], diff --git a/tests/fixtures/parser/ast/each-with-comment-input.svelte b/tests/fixtures/parser/ast/each-with-comment-input.svelte new file mode 100644 index 00000000..7986174e --- /dev/null +++ b/tests/fixtures/parser/ast/each-with-comment-input.svelte @@ -0,0 +1,3 @@ +{#each Array( +//comment +) as i}{i}{/each} diff --git a/tests/fixtures/parser/ast/each-with-comment-output.json b/tests/fixtures/parser/ast/each-with-comment-output.json new file mode 100644 index 00000000..e3789ddd --- /dev/null +++ b/tests/fixtures/parser/ast/each-with-comment-output.json @@ -0,0 +1,407 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteEachBlock", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "Array", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "optional": false, + "range": [ + 7, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "context": { + "type": "Identifier", + "name": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + }, + "index": null, + "key": null, + "children": [ + { + "type": "SvelteMustacheTag", + "kind": "text", + "expression": { + "type": "Identifier", + "name": "i", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "range": [ + 31, + 34 + ], + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 10 + } + } + } + ], + "else": null, + "range": [ + 0, + 41 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "sourceType": "module", + "comments": [ + { + "type": "Line", + "value": "comment", + "range": [ + 14, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 9 + } + } + } + ], + "tokens": [ + { + "type": "Punctuator", + "value": "{", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MustacheKeyword", + "value": "#each", + "range": [ + 1, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Identifier", + "value": "Array", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + { + "type": "Keyword", + "value": "as", + "range": [ + 26, + 28 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 4 + } + } + }, + { + "type": "Identifier", + "value": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 30, + 31 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 31, + 32 + ], + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + { + "type": "Identifier", + "value": "i", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 34, + 35 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 11 + } + } + }, + { + "type": "MustacheKeyword", + "value": "/each", + "range": [ + 35, + 40 + ], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 40, + 41 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "range": [ + 0, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/each-with-comment-scope-output.json b/tests/fixtures/parser/ast/each-with-comment-scope-output.json new file mode 100644 index 00000000..ff8a4ac7 --- /dev/null +++ b/tests/fixtures/parser/ast/each-with-comment-scope-output.json @@ -0,0 +1,357 @@ +{ + "type": "global", + "variables": [ + { + "name": "$$slots", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$props", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$restProps", + "identifiers": [], + "defs": [], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "module", + "variables": [], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "Array", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ], + "childScopes": [ + { + "type": "function", + "variables": [ + { + "name": "i", + "identifiers": [ + { + "type": "Identifier", + "name": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + } + ], + "defs": [ + { + "type": "Parameter", + "name": { + "type": "Identifier", + "name": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + }, + "node": { + "type": "SvelteEachBlock", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "Array", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "optional": false, + "range": [ + 7, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "context": { + "type": "Identifier", + "name": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + }, + "index": null, + "key": null, + "children": [ + { + "type": "SvelteMustacheTag", + "kind": "text", + "expression": { + "type": "Identifier", + "name": "i", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "range": [ + 31, + 34 + ], + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 10 + } + } + } + ], + "else": null, + "range": [ + 0, + 41 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "i", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + } + } + ] + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "i", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "i", + "range": [ + 29, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + } + } + ], + "childScopes": [], + "through": [] + } + ], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "Array", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ] + } + ], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "Array", + "range": [ + 7, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json b/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json new file mode 100644 index 00000000..d87243da --- /dev/null +++ b/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "spaced-comment", + "code": "//comment", + "line": 2, + "column": 1 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-input.svelte b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-input.svelte new file mode 100644 index 00000000..fecb45d8 --- /dev/null +++ b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-input.svelte @@ -0,0 +1,3 @@ +{#each f( +//asdf +d) as k}{/each} diff --git a/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-undef-result.json b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-undef-result.json new file mode 100644 index 00000000..0285d636 --- /dev/null +++ b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-undef-result.json @@ -0,0 +1,20 @@ +[ + { + "ruleId": "no-undef", + "code": "f", + "line": 1, + "column": 8 + }, + { + "ruleId": "no-undef", + "code": "d", + "line": 3, + "column": 1 + }, + { + "ruleId": "no-undef", + "code": "Csdf", + "line": 3, + "column": 10 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-unused-vars-result.json b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-unused-vars-result.json new file mode 100644 index 00000000..91017a94 --- /dev/null +++ b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-no-unused-vars-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "no-unused-vars", + "code": "k", + "line": 3, + "column": 7 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-output.json b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-output.json new file mode 100644 index 00000000..e34fc9a3 --- /dev/null +++ b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-output.json @@ -0,0 +1,553 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteEachBlock", + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "d", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ], + "callee": { + "type": "Identifier", + "name": "f", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "optional": false, + "range": [ + 7, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + "context": { + "type": "Identifier", + "name": "k", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "index": null, + "key": null, + "children": [ + { + "type": "SvelteElement", + "kind": "component", + "name": { + "type": "Identifier", + "name": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 25, + 31 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + "children": [], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 31, + 38 + ], + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 21 + } + } + }, + "range": [ + 25, + 38 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 21 + } + } + } + ], + "else": null, + "range": [ + 0, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 28 + } + } + } + ], + "sourceType": "module", + "comments": [ + { + "type": "Line", + "value": "asdf", + "range": [ + 10, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + } + } + ], + "tokens": [ + { + "type": "Punctuator", + "value": "{", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MustacheKeyword", + "value": "#each", + "range": [ + 1, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Identifier", + "value": "f", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 8, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "d", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 18, + 19 + ], + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + { + "type": "Keyword", + "value": "as", + "range": [ + 20, + 22 + ], + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "k", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 30, + 31 + ], + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 31, + 32 + ], + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 16 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "Csdf", + "range": [ + 33, + 37 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 37, + 38 + ], + "loc": { + "start": { + "line": 3, + "column": 20 + }, + "end": { + "line": 3, + "column": 21 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 38, + 39 + ], + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + } + } + }, + { + "type": "MustacheKeyword", + "value": "/each", + "range": [ + 39, + 44 + ], + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 27 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 44, + 45 + ], + "loc": { + "start": { + "line": 3, + "column": 27 + }, + "end": { + "line": 3, + "column": 28 + } + } + } + ], + "range": [ + 0, + 46 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-scope-output.json b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-scope-output.json new file mode 100644 index 00000000..6d635a13 --- /dev/null +++ b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-scope-output.json @@ -0,0 +1,494 @@ +{ + "type": "global", + "variables": [ + { + "name": "$$slots", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$props", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$restProps", + "identifiers": [], + "defs": [], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "module", + "variables": [], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "f", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "from": "module", + "init": null, + "resolved": null + }, + { + "identifier": { + "type": "Identifier", + "name": "d", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ], + "childScopes": [ + { + "type": "function", + "variables": [ + { + "name": "k", + "identifiers": [ + { + "type": "Identifier", + "name": "k", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + } + ], + "defs": [ + { + "type": "Parameter", + "name": { + "type": "Identifier", + "name": "k", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "node": { + "type": "SvelteEachBlock", + "expression": { + "type": "CallExpression", + "arguments": [ + { + "type": "Identifier", + "name": "d", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ], + "callee": { + "type": "Identifier", + "name": "f", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "optional": false, + "range": [ + 7, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + "context": { + "type": "Identifier", + "name": "k", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "index": null, + "key": null, + "children": [ + { + "type": "SvelteElement", + "kind": "component", + "name": { + "type": "Identifier", + "name": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 25, + 31 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + "children": [], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 31, + 38 + ], + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 21 + } + } + }, + "range": [ + 25, + 38 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 21 + } + } + } + ], + "else": null, + "range": [ + 0, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 28 + } + } + } + } + ], + "references": [] + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "from": "function", + "init": null, + "resolved": null + } + ], + "childScopes": [], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "from": "function", + "init": null, + "resolved": null + } + ] + } + ], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "f", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "from": "module", + "init": null, + "resolved": null + }, + { + "identifier": { + "type": "Identifier", + "name": "d", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "from": "module", + "init": null, + "resolved": null + }, + { + "identifier": { + "type": "Identifier", + "name": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "from": "function", + "init": null, + "resolved": null + } + ] + } + ], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "f", + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "from": "module", + "init": null, + "resolved": null + }, + { + "identifier": { + "type": "Identifier", + "name": "d", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "from": "module", + "init": null, + "resolved": null + }, + { + "identifier": { + "type": "Identifier", + "name": "Csdf", + "range": [ + 26, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "from": "function", + "init": null, + "resolved": null + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-spaced-comment-result.json b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-spaced-comment-result.json new file mode 100644 index 00000000..ca91e4ff --- /dev/null +++ b/tests/fixtures/parser/ast/issue327-eslint-plugin-svelte-spaced-comment-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "spaced-comment", + "code": "//asdf", + "line": 2, + "column": 1 + } +] \ No newline at end of file From 3a0e1285f7cf5392b02774d4dabb54e306c0e4f4 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sun, 25 Dec 2022 16:10:51 +0900 Subject: [PATCH 2/3] Create rotten-moons-glow.md --- .changeset/rotten-moons-glow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rotten-moons-glow.md diff --git a/.changeset/rotten-moons-glow.md b/.changeset/rotten-moons-glow.md new file mode 100644 index 00000000..4b0d61c7 --- /dev/null +++ b/.changeset/rotten-moons-glow.md @@ -0,0 +1,5 @@ +--- +"svelte-eslint-parser": patch +--- + +fix: parsing errors for blocks and js comments From 922869e630c47547c9484515aa6f42fbb20f3f94 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 25 Dec 2022 16:18:03 +0900 Subject: [PATCH 3/3] add test --- .changeset/rotten-moons-glow.md | 2 +- .../parser/ast/each-with-comment-input.svelte | 5 +- .../parser/ast/each-with-comment-output.json | 780 +++++++++++++++--- .../ast/each-with-comment-scope-output.json | 425 ++++++++-- ...ch-with-comment-spaced-comment-result.json | 2 +- .../ast/mustache-with-comment-input.svelte | 1 + ...mustache-with-comment-no-undef-result.json | 8 + .../ast/mustache-with-comment-output.json | 187 +++++ .../mustache-with-comment-scope-output.json | 106 +++ 9 files changed, 1340 insertions(+), 176 deletions(-) create mode 100644 tests/fixtures/parser/ast/mustache-with-comment-input.svelte create mode 100644 tests/fixtures/parser/ast/mustache-with-comment-no-undef-result.json create mode 100644 tests/fixtures/parser/ast/mustache-with-comment-output.json create mode 100644 tests/fixtures/parser/ast/mustache-with-comment-scope-output.json diff --git a/.changeset/rotten-moons-glow.md b/.changeset/rotten-moons-glow.md index 4b0d61c7..827dd5f0 100644 --- a/.changeset/rotten-moons-glow.md +++ b/.changeset/rotten-moons-glow.md @@ -2,4 +2,4 @@ "svelte-eslint-parser": patch --- -fix: parsing errors for blocks and js comments +fix: parsing errors (or wrong AST) for js comments in template diff --git a/tests/fixtures/parser/ast/each-with-comment-input.svelte b/tests/fixtures/parser/ast/each-with-comment-input.svelte index 7986174e..f4a1332c 100644 --- a/tests/fixtures/parser/ast/each-with-comment-input.svelte +++ b/tests/fixtures/parser/ast/each-with-comment-input.svelte @@ -1,3 +1,6 @@ + {#each Array( //comment -) as i}{i}{/each} +) as i}{i}{/each} diff --git a/tests/fixtures/parser/ast/each-with-comment-output.json b/tests/fixtures/parser/ast/each-with-comment-output.json index e3789ddd..1fb3b02c 100644 --- a/tests/fixtures/parser/ast/each-with-comment-output.json +++ b/tests/fixtures/parser/ast/each-with-comment-output.json @@ -1,6 +1,170 @@ { "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": "ImportDeclaration", + "source": { + "type": "Literal", + "raw": "'./foo.svelte'", + "value": "./foo.svelte", + "range": [ + 25, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 30 + } + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "local": { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + ], + "range": [ + 9, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 30 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 40, + 49 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "range": [ + 0, + 49 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "SvelteText", + "value": "\n", + "range": [ + 49, + 50 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 4, + "column": 0 + } + } + }, { "type": "SvelteEachBlock", "expression": { @@ -10,32 +174,32 @@ "type": "Identifier", "name": "Array", "range": [ - 7, - 12 + 57, + 62 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 1, + "line": 4, "column": 12 } } }, "optional": false, "range": [ - 7, - 25 + 57, + 75 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 3, + "line": 6, "column": 1 } } @@ -44,16 +208,16 @@ "type": "Identifier", "name": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } @@ -62,55 +226,129 @@ "key": null, "children": [ { - "type": "SvelteMustacheTag", - "kind": "text", - "expression": { + "type": "SvelteElement", + "kind": "component", + "name": { "type": "Identifier", - "name": "i", + "name": "Foo", "range": [ - 32, - 33 + 82, + 85 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 8 }, "end": { - "line": 3, - "column": 9 + "line": 6, + "column": 11 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 81, + 86 + ], + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 12 + } + } + }, + "children": [ + { + "type": "SvelteMustacheTag", + "kind": "text", + "expression": { + "type": "Identifier", + "name": "i", + "range": [ + 87, + 88 + ], + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 14 + } + } + }, + "range": [ + 86, + 89 + ], + "loc": { + "start": { + "line": 6, + "column": 12 + }, + "end": { + "line": 6, + "column": 15 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 89, + 95 + ], + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 21 } } }, "range": [ - 31, - 34 + 81, + 95 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 7 }, "end": { - "line": 3, - "column": 10 + "line": 6, + "column": 21 } } } ], "else": null, "range": [ - 0, - 41 + 50, + 102 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 0 }, "end": { - "line": 3, - "column": 17 + "line": 6, + "column": 28 } } } @@ -121,16 +359,16 @@ "type": "Line", "value": "comment", "range": [ - 14, - 23 + 64, + 73 ], "loc": { "start": { - "line": 2, + "line": 5, "column": 0 }, "end": { - "line": 2, + "line": 5, "column": 9 } } @@ -139,7 +377,7 @@ "tokens": [ { "type": "Punctuator", - "value": "{", + "value": "<", "range": [ 0, 1 @@ -156,11 +394,11 @@ } }, { - "type": "MustacheKeyword", - "value": "#each", + "type": "HTMLIdentifier", + "value": "script", "range": [ 1, - 6 + 7 ], "loc": { "start": { @@ -169,16 +407,16 @@ }, "end": { "line": 1, - "column": 6 + "column": 7 } } }, { - "type": "Identifier", - "value": "Array", + "type": "Punctuator", + "value": ">", "range": [ 7, - 12 + 8 ], "loc": { "start": { @@ -187,6 +425,222 @@ }, "end": { "line": 1, + "column": 8 + } + } + }, + { + "type": "Keyword", + "value": "import", + "range": [ + 9, + 15 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "from", + "range": [ + 20, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + { + "type": "String", + "value": "'./foo.svelte'", + "range": [ + 25, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 30 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 40, + 41 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 41, + 42 + ], + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 42, + 48 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 48, + 49 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "HTMLText", + "value": "\n", + "range": [ + 49, + 50 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 4, + "column": 0 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 50, + 51 + ], + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + } + }, + { + "type": "MustacheKeyword", + "value": "#each", + "range": [ + 51, + 56 + ], + "loc": { + "start": { + "line": 4, + "column": 1 + }, + "end": { + "line": 4, + "column": 6 + } + } + }, + { + "type": "Identifier", + "value": "Array", + "range": [ + 57, + 62 + ], + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, "column": 12 } } @@ -195,16 +649,16 @@ "type": "Punctuator", "value": "(", "range": [ - 12, - 13 + 62, + 63 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 12 }, "end": { - "line": 1, + "line": 4, "column": 13 } } @@ -213,16 +667,16 @@ "type": "Punctuator", "value": ")", "range": [ - 24, - 25 + 74, + 75 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 0 }, "end": { - "line": 3, + "line": 6, "column": 1 } } @@ -231,16 +685,16 @@ "type": "Keyword", "value": "as", "range": [ - 26, - 28 + 76, + 78 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 2 }, "end": { - "line": 3, + "line": 6, "column": 4 } } @@ -249,16 +703,16 @@ "type": "Identifier", "value": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } @@ -267,53 +721,107 @@ "type": "Punctuator", "value": "}", "range": [ - 30, - 31 + 80, + 81 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 6 }, "end": { - "line": 3, + "line": 6, "column": 7 } } }, { "type": "Punctuator", - "value": "{", + "value": "<", "range": [ - 31, - 32 + 81, + 82 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 7 }, "end": { - "line": 3, + "line": 6, "column": 8 } } }, { "type": "Identifier", - "value": "i", + "value": "Foo", "range": [ - 32, - 33 + 82, + 85 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 8 }, "end": { - "line": 3, - "column": 9 + "line": 6, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 85, + 86 + ], + "loc": { + "start": { + "line": 6, + "column": 11 + }, + "end": { + "line": 6, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 86, + 87 + ], + "loc": { + "start": { + "line": 6, + "column": 12 + }, + "end": { + "line": 6, + "column": 13 + } + } + }, + { + "type": "Identifier", + "value": "i", + "range": [ + 87, + 88 + ], + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 14 } } }, @@ -321,17 +829,89 @@ "type": "Punctuator", "value": "}", "range": [ - 33, - 34 + 88, + 89 ], "loc": { "start": { - "line": 3, - "column": 9 + "line": 6, + "column": 14 }, "end": { - "line": 3, - "column": 10 + "line": 6, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 89, + 90 + ], + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 90, + 91 + ], + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 17 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "Foo", + "range": [ + 91, + 94 + ], + "loc": { + "start": { + "line": 6, + "column": 17 + }, + "end": { + "line": 6, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 94, + 95 + ], + "loc": { + "start": { + "line": 6, + "column": 20 + }, + "end": { + "line": 6, + "column": 21 } } }, @@ -339,17 +919,17 @@ "type": "Punctuator", "value": "{", "range": [ - 34, - 35 + 95, + 96 ], "loc": { "start": { - "line": 3, - "column": 10 + "line": 6, + "column": 21 }, "end": { - "line": 3, - "column": 11 + "line": 6, + "column": 22 } } }, @@ -357,17 +937,17 @@ "type": "MustacheKeyword", "value": "/each", "range": [ - 35, - 40 + 96, + 101 ], "loc": { "start": { - "line": 3, - "column": 11 + "line": 6, + "column": 22 }, "end": { - "line": 3, - "column": 16 + "line": 6, + "column": 27 } } }, @@ -375,24 +955,24 @@ "type": "Punctuator", "value": "}", "range": [ - 40, - 41 + 101, + 102 ], "loc": { "start": { - "line": 3, - "column": 16 + "line": 6, + "column": 27 }, "end": { - "line": 3, - "column": 17 + "line": 6, + "column": 28 } } } ], "range": [ 0, - 42 + 103 ], "loc": { "start": { @@ -400,7 +980,7 @@ "column": 0 }, "end": { - "line": 4, + "line": 7, "column": 0 } } diff --git a/tests/fixtures/parser/ast/each-with-comment-scope-output.json b/tests/fixtures/parser/ast/each-with-comment-scope-output.json index ff8a4ac7..6cf870a5 100644 --- a/tests/fixtures/parser/ast/each-with-comment-scope-output.json +++ b/tests/fixtures/parser/ast/each-with-comment-scope-output.json @@ -24,23 +24,147 @@ "childScopes": [ { "type": "module", - "variables": [], + "variables": [ + { + "name": "Foo", + "identifiers": [ + { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + ], + "defs": [ + { + "type": "ImportBinding", + "name": { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + "node": { + "type": "ImportDefaultSpecifier", + "local": { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "Foo", + "range": [ + 82, + 85 + ], + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 11 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + } + ] + } + ], "references": [ { "identifier": { "type": "Identifier", "name": "Array", "range": [ - 7, - 12 + 57, + 62 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 1, + "line": 4, "column": 12 } } @@ -61,16 +185,16 @@ "type": "Identifier", "name": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } @@ -83,16 +207,16 @@ "type": "Identifier", "name": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } @@ -106,32 +230,32 @@ "type": "Identifier", "name": "Array", "range": [ - 7, - 12 + 57, + 62 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 1, + "line": 4, "column": 12 } } }, "optional": false, "range": [ - 7, - 25 + 57, + 75 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 3, + "line": 6, "column": 1 } } @@ -140,16 +264,16 @@ "type": "Identifier", "name": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } @@ -158,55 +282,129 @@ "key": null, "children": [ { - "type": "SvelteMustacheTag", - "kind": "text", - "expression": { + "type": "SvelteElement", + "kind": "component", + "name": { "type": "Identifier", - "name": "i", + "name": "Foo", "range": [ - 32, - 33 + 82, + 85 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 8 }, "end": { - "line": 3, - "column": 9 + "line": 6, + "column": 11 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 81, + 86 + ], + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 12 + } + } + }, + "children": [ + { + "type": "SvelteMustacheTag", + "kind": "text", + "expression": { + "type": "Identifier", + "name": "i", + "range": [ + 87, + 88 + ], + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 14 + } + } + }, + "range": [ + 86, + 89 + ], + "loc": { + "start": { + "line": 6, + "column": 12 + }, + "end": { + "line": 6, + "column": 15 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 89, + 95 + ], + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 21 } } }, "range": [ - 31, - 34 + 81, + 95 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 7 }, "end": { - "line": 3, - "column": 10 + "line": 6, + "column": 21 } } } ], "else": null, "range": [ - 0, - 41 + 50, + 102 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 0 }, "end": { - "line": 3, - "column": 17 + "line": 6, + "column": 28 } } } @@ -218,17 +416,17 @@ "type": "Identifier", "name": "i", "range": [ - 32, - 33 + 87, + 88 ], "loc": { "start": { - "line": 3, - "column": 8 + "line": 6, + "column": 13 }, "end": { - "line": 3, - "column": 9 + "line": 6, + "column": 14 } } }, @@ -238,16 +436,16 @@ "type": "Identifier", "name": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } @@ -262,17 +460,17 @@ "type": "Identifier", "name": "i", "range": [ - 32, - 33 + 87, + 88 ], "loc": { "start": { - "line": 3, - "column": 8 + "line": 6, + "column": 13 }, "end": { - "line": 3, - "column": 9 + "line": 6, + "column": 14 } } }, @@ -282,24 +480,105 @@ "type": "Identifier", "name": "i", "range": [ - 29, - 30 + 79, + 80 ], "loc": { "start": { - "line": 3, + "line": 6, "column": 5 }, "end": { - "line": 3, + "line": 6, "column": 6 } } } + }, + { + "identifier": { + "type": "Identifier", + "name": "Foo", + "range": [ + 82, + 85 + ], + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 11 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + } } ], "childScopes": [], - "through": [] + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "Foo", + "range": [ + 82, + 85 + ], + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 11 + } + } + }, + "from": "function", + "init": null, + "resolved": { + "type": "Identifier", + "name": "Foo", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + } + } + ] } ], "through": [ @@ -308,16 +587,16 @@ "type": "Identifier", "name": "Array", "range": [ - 7, - 12 + 57, + 62 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 1, + "line": 4, "column": 12 } } @@ -335,16 +614,16 @@ "type": "Identifier", "name": "Array", "range": [ - 7, - 12 + 57, + 62 ], "loc": { "start": { - "line": 1, + "line": 4, "column": 7 }, "end": { - "line": 1, + "line": 4, "column": 12 } } diff --git a/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json b/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json index d87243da..6be0d232 100644 --- a/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json +++ b/tests/fixtures/parser/ast/each-with-comment-spaced-comment-result.json @@ -2,7 +2,7 @@ { "ruleId": "spaced-comment", "code": "//comment", - "line": 2, + "line": 5, "column": 1 } ] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/mustache-with-comment-input.svelte b/tests/fixtures/parser/ast/mustache-with-comment-input.svelte new file mode 100644 index 00000000..0455da9f --- /dev/null +++ b/tests/fixtures/parser/ast/mustache-with-comment-input.svelte @@ -0,0 +1 @@ +{foo(/**/)} diff --git a/tests/fixtures/parser/ast/mustache-with-comment-no-undef-result.json b/tests/fixtures/parser/ast/mustache-with-comment-no-undef-result.json new file mode 100644 index 00000000..b8e43e3e --- /dev/null +++ b/tests/fixtures/parser/ast/mustache-with-comment-no-undef-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "no-undef", + "code": "foo", + "line": 1, + "column": 2 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/mustache-with-comment-output.json b/tests/fixtures/parser/ast/mustache-with-comment-output.json new file mode 100644 index 00000000..38c2bb3d --- /dev/null +++ b/tests/fixtures/parser/ast/mustache-with-comment-output.json @@ -0,0 +1,187 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteMustacheTag", + "kind": "text", + "expression": { + "type": "CallExpression", + "arguments": [], + "callee": { + "type": "Identifier", + "name": "foo", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + "optional": false, + "range": [ + 1, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + "range": [ + 0, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + } + } + ], + "sourceType": "module", + "comments": [ + { + "type": "Block", + "value": "", + "range": [ + 5, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 9 + } + } + } + ], + "tokens": [ + { + "type": "Punctuator", + "value": "{", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 4, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 9, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + } + ], + "range": [ + 0, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/mustache-with-comment-scope-output.json b/tests/fixtures/parser/ast/mustache-with-comment-scope-output.json new file mode 100644 index 00000000..cfa04a30 --- /dev/null +++ b/tests/fixtures/parser/ast/mustache-with-comment-scope-output.json @@ -0,0 +1,106 @@ +{ + "type": "global", + "variables": [ + { + "name": "$$slots", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$props", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$restProps", + "identifiers": [], + "defs": [], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "module", + "variables": [], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ], + "childScopes": [], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ] + } + ], + "through": [ + { + "identifier": { + "type": "Identifier", + "name": "foo", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + "from": "module", + "init": null, + "resolved": null + } + ] +} \ No newline at end of file