Skip to content

Commit 032cdd4

Browse files
committed
fix: parsing error
1 parent 366ac20 commit 032cdd4

12 files changed

+17408
-3
lines changed

src/parser/typescript/analyze/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
removeIdentifierVariable,
1111
replaceScope,
1212
} from "../../../scope";
13-
import { addElementsToSortedArray } from "../../../utils";
13+
import { addElementsToSortedArray, sortedLastIndex } from "../../../utils";
1414
import { parseScriptWithoutAnalyzeScope } from "../../script";
1515
import { TypeScriptContext } from "../context";
1616
import type { TSESParseForESLintResult } from "../types";
@@ -233,15 +233,28 @@ function transformForDeclareReactiveVar(
233233
// function fn () { return foo; }
234234

235235
const openParens: TSESTree.Token[] = [];
236+
let eq: TSESTree.Token | null = null;
236237
const closeParens: TSESTree.Token[] = [];
237-
for (const token of tokens) {
238+
const startIndex = sortedLastIndex(
239+
tokens,
240+
(target) => target.range[0] - statement.range[0]
241+
);
242+
for (let index = startIndex; index < tokens.length; index++) {
243+
const token = tokens[index];
238244
if (
239245
statement.range[0] <= token.range[0] &&
240246
token.range[1] <= statement.range[1]
241247
) {
242248
if (token.value === "(" && token.range[1] <= expression.range[0]) {
243249
openParens.push(token);
244250
}
251+
if (
252+
token.value === "=" &&
253+
expression.left.range[1] <= token.range[0] &&
254+
token.range[1] <= expression.right.range[0]
255+
) {
256+
eq = token;
257+
}
245258
if (token.value === ")" && expression.range[1] <= token.range[0]) {
246259
closeParens.push(token);
247260
}
@@ -258,7 +271,7 @@ function transformForDeclareReactiveVar(
258271
ctx.appendOriginal(expression.range[0]);
259272
ctx.skipUntilOriginalOffset(id.range[0]);
260273
ctx.appendScript("let ");
261-
ctx.appendOriginal(expression.right.range[0]);
274+
ctx.appendOriginal(eq ? eq.range[1] : expression.right.range[0]);
262275
ctx.appendScript(`${functionId}();\nfunction ${functionId}(){return `);
263276
for (const token of closeParens) {
264277
ctx.appendOriginal(token.range[0]);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script lang="ts">
2+
type EventInfo = {
3+
start_at: number,
4+
}
5+
let info: EventInfo | null = null
6+
7+
const lightFormat = (i: Date | number) => i.toString()
8+
9+
fetch('/fakeurl').then(() => { info = { start_at: 0 } }).catch(console.error)
10+
11+
$: startDate = (info?.start_at ? lightFormat(info.start_at) : null)
12+
const endDate = () => (info?.start_at ? lightFormat(info.start_at) : null)
13+
</script>
14+
15+
<input type='text'/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */
2+
import type { Linter } from "eslint";
3+
import { BASIC_PARSER_OPTIONS } from "../../../src/parser/test-utils";
4+
import { rules } from "@typescript-eslint/eslint-plugin";
5+
export function setupLinter(linter: Linter) {
6+
linter.defineRule(
7+
"@typescript-eslint/no-unsafe-argument",
8+
rules["no-unsafe-argument"] as never
9+
);
10+
}
11+
12+
export function getConfig() {
13+
return {
14+
parser: "svelte-eslint-parser",
15+
parserOptions: BASIC_PARSER_OPTIONS,
16+
rules: {
17+
"@typescript-eslint/no-unsafe-argument": "error",
18+
},
19+
env: {
20+
browser: true,
21+
es2021: true,
22+
},
23+
};
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script lang="ts">
2+
let isOpen = false;
3+
4+
let id: string | undefined = undefined;
5+
</script>
6+
7+
<h1>Welcome to SvelteKit</h1>
8+
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
9+
10+
<button on:click={() => (isOpen = !isOpen)} />
11+
12+
{#if !isOpen}
13+
<div>{id}</div>
14+
{/if}
15+
16+
{#if !isOpen && id}
17+
<div>{id}</div>
18+
{/if}
19+
20+
{#if !isOpen && !!id}
21+
<div>only visible when isOpen and id</div>
22+
{/if}
23+
24+
{#if !isOpen && id !== undefined}
25+
<div>only visible when isopen and id</div>
26+
{/if}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */
2+
import type { Linter } from "eslint";
3+
import { BASIC_PARSER_OPTIONS } from "../../../src/parser/test-utils";
4+
import { rules } from "@typescript-eslint/eslint-plugin";
5+
export function setupLinter(linter: Linter) {
6+
linter.defineRule(
7+
"@typescript-eslint/no-unnecessary-condition",
8+
rules["no-unnecessary-condition"] as never
9+
);
10+
}
11+
12+
export function getConfig() {
13+
return {
14+
parser: "svelte-eslint-parser",
15+
parserOptions: BASIC_PARSER_OPTIONS,
16+
rules: {
17+
"@typescript-eslint/no-unnecessary-condition": "error",
18+
},
19+
env: {
20+
browser: true,
21+
es2021: true,
22+
},
23+
};
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script lang="ts">
2+
type EventInfo = {
3+
start_at: number,
4+
}
5+
let info: EventInfo | null = null
6+
7+
const lightFormat = (i: Date | number) => i.toString()
8+
9+
fetch('/fakeurl').then(() => { info = { start_at: 0 } }).catch(console.error)
10+
11+
$: startDate = (info?.start_at ? lightFormat(info.start_at) : null)
12+
const endDate = () => (info?.start_at ? lightFormat(info.start_at) : null)
13+
</script>
14+
15+
<input type='text'/>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"ruleId": "no-unused-vars",
4+
"code": "startDate",
5+
"line": 11,
6+
"column": 4
7+
},
8+
{
9+
"ruleId": "no-unused-vars",
10+
"code": "endDate",
11+
"line": 12,
12+
"column": 7
13+
}
14+
]

0 commit comments

Comments
 (0)