Skip to content

Commit 412094b

Browse files
authored
Fix crash for trailing inline comment (#74)
1 parent 32650bd commit 412094b

7 files changed

+1572
-8
lines changed

src/context/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class ScriptsSourceCode {
1515

1616
private _appendScriptLets: string | null = null
1717

18-
public separateSemiIndex: number
18+
public separateIndexes: number[] = []
1919

2020
public constructor(
2121
script: string,
@@ -24,7 +24,7 @@ export class ScriptsSourceCode {
2424
this.raw = script
2525
this.trimmedRaw = script.trimEnd()
2626
this.attrs = attrs
27-
this.separateSemiIndex = script.length
27+
this.separateIndexes = [script.length]
2828
}
2929

3030
public get vcode(): string {
@@ -37,8 +37,8 @@ export class ScriptsSourceCode {
3737
public addLet(letCode: string): { start: number; end: number } {
3838
if (this._appendScriptLets == null) {
3939
this._appendScriptLets = ""
40-
this.separateSemiIndex = this.vcode.length
41-
this._appendScriptLets += ";"
40+
this.separateIndexes = [this.vcode.length, this.vcode.length + 1]
41+
this._appendScriptLets += "\n;"
4242
const after = this.raw.slice(this.vcode.length)
4343
this._appendScriptLets += after
4444
}

src/context/script-let.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ export class ScriptLetContext {
482482
if (!orderedRestoreCallback) {
483483
return
484484
}
485-
const separateSemiIndex = this.script.separateSemiIndex
485+
const separateIndexes = this.script.separateIndexes
486486
const tokens = result.ast.tokens
487487
const processedTokens = []
488488
const comments = result.ast.comments
@@ -491,7 +491,7 @@ export class ScriptLetContext {
491491

492492
let tok
493493
while ((tok = tokens.shift())) {
494-
if (separateSemiIndex === tok.range[0] && tok.value === ";") {
494+
if (separateIndexes.includes(tok.range[0]) && tok.value === ";") {
495495
break
496496
}
497497
if (orderedRestoreCallback.start <= tok.range[0]) {
@@ -514,13 +514,19 @@ export class ScriptLetContext {
514514
traverseNodes(result.ast, {
515515
visitorKeys: result.visitorKeys,
516516
enterNode: (node) => {
517-
if (node.range?.[1] === separateSemiIndex + 1) {
517+
while (
518+
node.range &&
519+
separateIndexes.includes(node.range[1] - 1)
520+
) {
518521
node.range[1]--
519522
node.loc!.end.column--
520523
}
524+
if (node.loc!.end.column < 0) {
525+
node.loc!.end = this.ctx.getLocFromIndex(node.range![1])
526+
}
521527
if (
522528
(node as any).parent === result.ast &&
523-
separateSemiIndex <= node.range![0]
529+
separateIndexes[0] <= node.range![0]
524530
) {
525531
removeStatements.push(node as any)
526532
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let a = 1;
3+
let b = /a/
4+
//
5+
</script>
6+
7+
<input type="number" bind:value={a}>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"ruleId": "no-unused-vars",
4+
"code": "b",
5+
"line": 3,
6+
"column": 7
7+
}
8+
]

0 commit comments

Comments
 (0)