Skip to content

Fix crash for trailing inline comment #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ScriptsSourceCode {

private _appendScriptLets: string | null = null

public separateSemiIndex: number
public separateIndexes: number[] = []

public constructor(
script: string,
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions src/context/script-let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]) {
Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/parser/ast/trailing-comment01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let a = 1;
let b = /a/
//
</script>

<input type="number" bind:value={a}>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"ruleId": "no-unused-vars",
"code": "b",
"line": 3,
"column": 7
}
]
Loading