diff --git a/src/context/index.ts b/src/context/index.ts index 6f740654..db1f7085 100644 --- a/src/context/index.ts +++ b/src/context/index.ts @@ -15,7 +15,7 @@ export class ScriptsSourceCode { private _appendScriptLets: string | null = null - public separateSemiIndex: number + public separateIndexes: number[] = [] public constructor( script: string, @@ -24,7 +24,7 @@ export class ScriptsSourceCode { this.raw = script this.trimmedRaw = script.trimEnd() this.attrs = attrs - this.separateSemiIndex = script.length + this.separateIndexes = [script.length] } public get vcode(): string { @@ -37,8 +37,8 @@ export class ScriptsSourceCode { public addLet(letCode: string): { start: number; end: number } { if (this._appendScriptLets == null) { this._appendScriptLets = "" - this.separateSemiIndex = this.vcode.length - this._appendScriptLets += ";" + this.separateIndexes = [this.vcode.length, this.vcode.length + 1] + this._appendScriptLets += "\n;" const after = this.raw.slice(this.vcode.length) this._appendScriptLets += after } diff --git a/src/context/script-let.ts b/src/context/script-let.ts index 2c5f4c1e..5b0c03a6 100644 --- a/src/context/script-let.ts +++ b/src/context/script-let.ts @@ -482,7 +482,7 @@ export class ScriptLetContext { if (!orderedRestoreCallback) { return } - const separateSemiIndex = this.script.separateSemiIndex + const separateIndexes = this.script.separateIndexes const tokens = result.ast.tokens const processedTokens = [] const comments = result.ast.comments @@ -491,7 +491,7 @@ export class ScriptLetContext { let tok while ((tok = tokens.shift())) { - if (separateSemiIndex === tok.range[0] && tok.value === ";") { + if (separateIndexes.includes(tok.range[0]) && tok.value === ";") { break } if (orderedRestoreCallback.start <= tok.range[0]) { @@ -514,13 +514,19 @@ export class ScriptLetContext { traverseNodes(result.ast, { visitorKeys: result.visitorKeys, enterNode: (node) => { - if (node.range?.[1] === separateSemiIndex + 1) { + while ( + node.range && + separateIndexes.includes(node.range[1] - 1) + ) { node.range[1]-- node.loc!.end.column-- } + if (node.loc!.end.column < 0) { + node.loc!.end = this.ctx.getLocFromIndex(node.range![1]) + } if ( (node as any).parent === result.ast && - separateSemiIndex <= node.range![0] + separateIndexes[0] <= node.range![0] ) { removeStatements.push(node as any) } diff --git a/tests/fixtures/parser/ast/trailing-comment01-input.svelte b/tests/fixtures/parser/ast/trailing-comment01-input.svelte new file mode 100644 index 00000000..7041982c --- /dev/null +++ b/tests/fixtures/parser/ast/trailing-comment01-input.svelte @@ -0,0 +1,7 @@ + + + diff --git a/tests/fixtures/parser/ast/trailing-comment01-no-unused-vars-result.json b/tests/fixtures/parser/ast/trailing-comment01-no-unused-vars-result.json new file mode 100644 index 00000000..1d81cd48 --- /dev/null +++ b/tests/fixtures/parser/ast/trailing-comment01-no-unused-vars-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "no-unused-vars", + "code": "b", + "line": 3, + "column": 7 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/trailing-comment01-output.json b/tests/fixtures/parser/ast/trailing-comment01-output.json new file mode 100644 index 00000000..9fb94934 --- /dev/null +++ b/tests/fixtures/parser/ast/trailing-comment01-output.json @@ -0,0 +1,1051 @@ +{ + "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": "let", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + "init": { + "type": "Literal", + "raw": "1", + "value": 1, + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + "range": [ + 15, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 11 + } + } + } + ], + "range": [ + 11, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + { + "type": "VariableDeclaration", + "kind": "let", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "init": { + "type": "Literal", + "raw": "/a/", + "regex": { + "flags": "", + "pattern": "a" + }, + "value": "/a/", + "range": [ + 32, + 35 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "range": [ + 28, + 35 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + ], + "range": [ + 24, + 40 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 4, + "column": 4 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 41, + 50 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 9 + } + } + }, + "range": [ + 0, + 50 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 9 + } + } + }, + { + "type": "SvelteText", + "value": "\n\n", + "range": [ + 50, + 52 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 7, + "column": 0 + } + } + }, + { + "type": "SvelteElement", + "kind": "html", + "name": { + "type": "SvelteName", + "name": "input", + "range": [ + 53, + 58 + ], + "loc": { + "start": { + "line": 7, + "column": 1 + }, + "end": { + "line": 7, + "column": 6 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [ + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "type", + "range": [ + 59, + 63 + ], + "loc": { + "start": { + "line": 7, + "column": 7 + }, + "end": { + "line": 7, + "column": 11 + } + } + }, + "boolean": false, + "value": [ + { + "type": "SvelteLiteral", + "value": "number", + "range": [ + 65, + 71 + ], + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 19 + } + } + } + ], + "range": [ + 59, + 72 + ], + "loc": { + "start": { + "line": 7, + "column": 7 + }, + "end": { + "line": 7, + "column": 20 + } + } + }, + { + "type": "SvelteDirective", + "kind": "Binding", + "key": { + "type": "SvelteDirectiveKey", + "name": { + "type": "SvelteName", + "name": "value", + "range": [ + 78, + 83 + ], + "loc": { + "start": { + "line": 7, + "column": 26 + }, + "end": { + "line": 7, + "column": 31 + } + } + }, + "modifiers": [], + "range": [ + 73, + 83 + ], + "loc": { + "start": { + "line": 7, + "column": 21 + }, + "end": { + "line": 7, + "column": 31 + } + } + }, + "expression": { + "type": "Identifier", + "name": "a", + "range": [ + 85, + 86 + ], + "loc": { + "start": { + "line": 7, + "column": 33 + }, + "end": { + "line": 7, + "column": 34 + } + } + }, + "range": [ + 73, + 87 + ], + "loc": { + "start": { + "line": 7, + "column": 21 + }, + "end": { + "line": 7, + "column": 35 + } + } + } + ], + "selfClosing": false, + "range": [ + 52, + 88 + ], + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 36 + } + } + }, + "children": [], + "endTag": null, + "range": [ + 52, + 88 + ], + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 36 + } + } + } + ], + "sourceType": "module", + "comments": [ + { + "type": "Line", + "value": "", + "range": [ + 38, + 40 + ], + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 4 + } + } + } + ], + "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": "let", + "range": [ + 11, + 14 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + } + }, + { + "type": "Numeric", + "value": "1", + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + { + "type": "Keyword", + "value": "let", + "range": [ + 24, + 27 + ], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 30, + 31 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "RegularExpression", + "regex": { + "flags": "", + "pattern": "a" + }, + "value": "/a/", + "range": [ + 32, + 35 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 41, + 42 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 42, + 43 + ], + "loc": { + "start": { + "line": 5, + "column": 1 + }, + "end": { + "line": 5, + "column": 2 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 43, + 49 + ], + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 49, + 50 + ], + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 9 + } + } + }, + { + "type": "HTMLText", + "value": "\n\n", + "range": [ + 50, + 52 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 7, + "column": 0 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "input", + "range": [ + 53, + 58 + ], + "loc": { + "start": { + "line": 7, + "column": 1 + }, + "end": { + "line": 7, + "column": 6 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "type", + "range": [ + 59, + 63 + ], + "loc": { + "start": { + "line": 7, + "column": 7 + }, + "end": { + "line": 7, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 63, + 64 + ], + "loc": { + "start": { + "line": 7, + "column": 11 + }, + "end": { + "line": 7, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 64, + 65 + ], + "loc": { + "start": { + "line": 7, + "column": 12 + }, + "end": { + "line": 7, + "column": 13 + } + } + }, + { + "type": "HTMLText", + "value": "number", + "range": [ + 65, + 71 + ], + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 71, + 72 + ], + "loc": { + "start": { + "line": 7, + "column": 19 + }, + "end": { + "line": 7, + "column": 20 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "bind", + "range": [ + 73, + 77 + ], + "loc": { + "start": { + "line": 7, + "column": 21 + }, + "end": { + "line": 7, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 77, + 78 + ], + "loc": { + "start": { + "line": 7, + "column": 25 + }, + "end": { + "line": 7, + "column": 26 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "value", + "range": [ + 78, + 83 + ], + "loc": { + "start": { + "line": 7, + "column": 26 + }, + "end": { + "line": 7, + "column": 31 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 83, + 84 + ], + "loc": { + "start": { + "line": 7, + "column": 31 + }, + "end": { + "line": 7, + "column": 32 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 84, + 85 + ], + "loc": { + "start": { + "line": 7, + "column": 32 + }, + "end": { + "line": 7, + "column": 33 + } + } + }, + { + "type": "Identifier", + "value": "a", + "range": [ + 85, + 86 + ], + "loc": { + "start": { + "line": 7, + "column": 33 + }, + "end": { + "line": 7, + "column": 34 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 86, + 87 + ], + "loc": { + "start": { + "line": 7, + "column": 34 + }, + "end": { + "line": 7, + "column": 35 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 87, + 88 + ], + "loc": { + "start": { + "line": 7, + "column": 35 + }, + "end": { + "line": 7, + "column": 36 + } + } + } + ], + "range": [ + 0, + 89 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 8, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/trailing-comment01-prefer-const-result.json b/tests/fixtures/parser/ast/trailing-comment01-prefer-const-result.json new file mode 100644 index 00000000..059794b8 --- /dev/null +++ b/tests/fixtures/parser/ast/trailing-comment01-prefer-const-result.json @@ -0,0 +1,8 @@ +[ + { + "ruleId": "prefer-const", + "code": "b", + "line": 3, + "column": 7 + } +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/trailing-comment01-scope-output.json b/tests/fixtures/parser/ast/trailing-comment01-scope-output.json new file mode 100644 index 00000000..a7744f2c --- /dev/null +++ b/tests/fixtures/parser/ast/trailing-comment01-scope-output.json @@ -0,0 +1,484 @@ +{ + "type": "global", + "variables": [ + { + "name": "$$slots", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$props", + "identifiers": [], + "defs": [], + "references": [] + }, + { + "name": "$$restProps", + "identifiers": [], + "defs": [], + "references": [] + } + ], + "references": [], + "childScopes": [ + { + "type": "module", + "variables": [ + { + "name": "a", + "identifiers": [ + { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + } + ], + "defs": [ + { + "type": "Variable", + "name": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + "node": { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + "init": { + "type": "Literal", + "raw": "1", + "value": 1, + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + "range": [ + 15, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 11 + } + } + } + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + "from": "module", + "init": true, + "resolved": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + } + }, + { + "identifier": { + "type": "Identifier", + "name": "a", + "range": [ + 85, + 86 + ], + "loc": { + "start": { + "line": 7, + "column": 33 + }, + "end": { + "line": 7, + "column": 34 + } + } + }, + "from": "module", + "init": null, + "resolved": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + } + } + ] + }, + { + "name": "b", + "identifiers": [ + { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + } + ], + "defs": [ + { + "type": "Variable", + "name": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "node": { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "init": { + "type": "Literal", + "raw": "/a/", + "regex": { + "flags": "", + "pattern": "a" + }, + "value": "/a/", + "range": [ + 32, + 35 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "range": [ + 28, + 35 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "from": "module", + "init": true, + "resolved": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + } + } + ] + } + ], + "references": [ + { + "identifier": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + "from": "module", + "init": true, + "resolved": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + } + }, + { + "identifier": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "from": "module", + "init": true, + "resolved": { + "type": "Identifier", + "name": "b", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + } + } + }, + { + "identifier": { + "type": "Identifier", + "name": "a", + "range": [ + 85, + 86 + ], + "loc": { + "start": { + "line": 7, + "column": 33 + }, + "end": { + "line": 7, + "column": 34 + } + } + }, + "from": "module", + "init": null, + "resolved": { + "type": "Identifier", + "name": "a", + "range": [ + 15, + 16 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + } + } + ], + "childScopes": [], + "through": [] + } + ], + "through": [] +} \ No newline at end of file