Skip to content

fix: incorrect location when there is whitespace at the beginning of block #434

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 5 commits into from
Nov 21, 2023
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
5 changes: 5 additions & 0 deletions .changeset/honest-mangos-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: incorrect location when there is whitespace at the beginning of block
40 changes: 29 additions & 11 deletions src/parser/converts/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { getWithLoc, indexOf, lastIndexOf } from "./common";
import type * as ESTree from "estree";

/** Get start index of block */
function startBlockIndex(code: string, endIndex: number): number {
function startBlockIndex(
code: string,
endIndex: number,
block: string,
): number {
return lastIndexOf(
code,
(c, index) => {
Expand All @@ -34,7 +38,7 @@ function startBlockIndex(code: string, endIndex: number): number {
if (!nextC.trim()) {
continue;
}
return code.startsWith("#if", next) || code.startsWith(":else", next);
return code.startsWith(block, next);
}
return false;
},
Expand Down Expand Up @@ -62,9 +66,11 @@ export function convertIfBlock(
): SvelteIfBlock {
// {#if expr} {:else} {/if}
// {:else if expr} {/if}
const nodeStart = elseif
? startBlockIndex(ctx.code, node.start - 1)
: node.start;
const nodeStart = startBlockIndex(
ctx.code,
elseif ? node.start - 1 : node.start,
elseif ? ":else" : "#if",
);
const ifBlock: SvelteIfBlock = {
type: "SvelteIfBlock",
elseif: Boolean(elseif),
Expand All @@ -90,7 +96,15 @@ export function convertIfBlock(
return ifBlock;
}

const elseStart = startBlockIndex(ctx.code, node.else.start - 1);
let baseStart = node.else.start;
if (node.else.children.length === 1) {
const c = node.else.children[0];
if (c.type === "IfBlock" && c.elseif) {
baseStart = Math.min(baseStart, c.start, getWithLoc(c.expression).start);
}
}

const elseStart = startBlockIndex(ctx.code, baseStart - 1, ":else");

if (node.else.children.length === 1) {
const c = node.else.children[0];
Expand Down Expand Up @@ -145,6 +159,7 @@ export function convertEachBlock(
ctx: Context,
): SvelteEachBlock {
// {#each expr as item, index (key)} {/each}
const nodeStart = startBlockIndex(ctx.code, node.start, "#each");
const eachBlock: SvelteEachBlock = {
type: "SvelteEachBlock",
expression: null as any,
Expand All @@ -154,7 +169,7 @@ export function convertEachBlock(
children: [],
else: null,
parent,
...ctx.getConvertLocation(node),
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
};

let indexRange: null | { start: number; end: number } = null;
Expand Down Expand Up @@ -199,7 +214,7 @@ export function convertEachBlock(
return eachBlock;
}

const elseStart = startBlockIndex(ctx.code, node.else.start - 1);
const elseStart = startBlockIndex(ctx.code, node.else.start - 1, ":else");

const elseBlock: SvelteElseBlockAlone = {
type: "SvelteElseBlock",
Expand Down Expand Up @@ -227,6 +242,7 @@ export function convertAwaitBlock(
parent: SvelteAwaitBlock["parent"],
ctx: Context,
): SvelteAwaitBlock {
const nodeStart = startBlockIndex(ctx.code, node.start, "#await");
const awaitBlock = {
type: "SvelteAwaitBlock",
expression: null as any,
Expand All @@ -235,7 +251,7 @@ export function convertAwaitBlock(
then: null as any,
catch: null as any,
parent,
...ctx.getConvertLocation(node),
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
} as SvelteAwaitBlock;

ctx.scriptLet.addExpression(
Expand Down Expand Up @@ -413,12 +429,13 @@ export function convertKeyBlock(
parent: SvelteKeyBlock["parent"],
ctx: Context,
): SvelteKeyBlock {
const nodeStart = startBlockIndex(ctx.code, node.start, "#key");
const keyBlock: SvelteKeyBlock = {
type: "SvelteKeyBlock",
expression: null as any,
children: [],
parent,
...ctx.getConvertLocation(node),
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
};

ctx.scriptLet.addExpression(node.expression, keyBlock, null, (expression) => {
Expand All @@ -441,13 +458,14 @@ export function convertSnippetBlock(
ctx: Context,
): SvelteSnippetBlock {
// {#snippet x(args)}...{/snippet}
const nodeStart = startBlockIndex(ctx.code, node.start, "#snippet");
const snippetBlock: SvelteSnippetBlock = {
type: "SvelteSnippetBlock",
id: null as any,
context: null as any,
children: [],
parent,
...ctx.getConvertLocation(node),
...ctx.getConvertLocation({ start: nodeStart, end: node.end }),
};

const closeParenIndex = ctx.code.indexOf(
Expand Down
6 changes: 0 additions & 6 deletions tests/fixtures/parser/ast/if-block01-requirements.json

This file was deleted.

53 changes: 53 additions & 0 deletions tests/fixtures/parser/ast/newline-in-blocks-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--await-->
{
#await
expression
}
...
{
:then
name
}
...
{
:catch
name
}
...
{
/await
}
<!--each-->
{
#each
cats
as
{
id,
name
}
,
i
}
...
{
/each
}
<!--if-->
{
#if
a
}
...
{
/if
}
<!--key-->
{
#key
value
}
...
{
/key
}
26 changes: 26 additions & 0 deletions tests/fixtures/parser/ast/newline-in-blocks-no-undef-result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"ruleId": "no-undef",
"code": "expression",
"line": 4,
"column": 1
},
{
"ruleId": "no-undef",
"code": "cats",
"line": 23,
"column": 1
},
{
"ruleId": "no-undef",
"code": "a",
"line": 39,
"column": 1
},
{
"ruleId": "no-undef",
"code": "value",
"line": 48,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"ruleId": "no-unused-vars",
"code": "name",
"line": 9,
"column": 1
},
{
"ruleId": "no-unused-vars",
"code": "name",
"line": 14,
"column": 1
},
{
"ruleId": "no-unused-vars",
"code": "id",
"line": 26,
"column": 1
},
{
"ruleId": "no-unused-vars",
"code": "name",
"line": 27,
"column": 1
},
{
"ruleId": "no-unused-vars",
"code": "i",
"line": 30,
"column": 1
}
]
Loading