Skip to content

Commit cf20c86

Browse files
authored
fix: wrong type information for #await with same id (#371)
* fix: wrong type information for `#await` with same id * Create twenty-pandas-tie.md * fix
1 parent 182c6cb commit cf20c86

10 files changed

+124
-26
lines changed

.changeset/twenty-pandas-tie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: wrong type information for `#await` with same id

src/parser/converts/block.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ export function convertAwaitBlock(
307307
};
308308
}
309309
const idAwaitThenValue = typeCtx.generateUniqueId("AwaitThenValue");
310-
if (node.expression.type === "Identifier") {
310+
if (
311+
node.expression.type === "Identifier" &&
312+
// We cannot use type annotations like `(x: Foo<x>)` if they have the same identifier name.
313+
!hasIdentifierFor(node.expression.name, baseParam.node)
314+
) {
311315
return {
312316
preparationScript: [generateAwaitThenValueType(idAwaitThenValue)],
313317
param: {
@@ -484,3 +488,30 @@ function generateAwaitThenValueType(id: string) {
484488
: never
485489
: T;`;
486490
}
491+
492+
/** Checks whether the given name identifier is exists or not. */
493+
function hasIdentifierFor(name: string, node: ESTree.Pattern): boolean {
494+
if (node.type === "Identifier") {
495+
return node.name === name;
496+
}
497+
if (node.type === "ObjectPattern") {
498+
return node.properties.some((property) =>
499+
property.type === "Property"
500+
? hasIdentifierFor(name, property.value)
501+
: hasIdentifierFor(name, property)
502+
);
503+
}
504+
if (node.type === "ArrayPattern") {
505+
return node.elements.some(
506+
(element) => element && hasIdentifierFor(name, element)
507+
);
508+
}
509+
if (node.type === "RestElement") {
510+
return hasIdentifierFor(name, node.argument);
511+
}
512+
if (node.type === "AssignmentPattern") {
513+
return hasIdentifierFor(name, node.left);
514+
}
515+
516+
return false;
517+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script lang="ts">
2+
let a: Promise<{ x: number }>
3+
let b: Promise<{ x: number }>
4+
let c: Promise<{ x: number }>
5+
let d: Promise<number[]>
6+
let e: Promise<number[]>
7+
</script>
8+
9+
{#await a}
10+
<div>await</div>
11+
{:then a}
12+
<div>{a.x}</div>
13+
{/await}
14+
15+
{#await b}
16+
<div>await</div>
17+
{:then { x: b = 42 }}
18+
<div>{b.toExponential()}</div>
19+
{/await}
20+
21+
{#await c}
22+
<div>await</div>
23+
{:then {...c}}
24+
<div>{c.x}</div>
25+
{/await}
26+
27+
{#await d}
28+
<div>await</div>
29+
{:then [d]}
30+
<div>{d.toExponential()}</div>
31+
{/await}
32+
33+
{#await e}
34+
<div>await</div>
35+
{:then [...e]}
36+
<div>{e[0].toExponential()}</div>
37+
{/await}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
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 { generateParserOptions } 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-member-access",
8+
rules["no-unsafe-member-access"] as never
9+
);
10+
}
11+
12+
export function getConfig() {
13+
return {
14+
parser: "svelte-eslint-parser",
15+
parserOptions: generateParserOptions(),
16+
rules: {
17+
"@typescript-eslint/no-unsafe-member-access": "error",
18+
},
19+
env: {
20+
browser: true,
21+
es2021: true,
22+
},
23+
};
24+
}

tests/fixtures/parser/ast/ts-store01-input.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
{$b}
1212
{c}
1313
{$c}
14-
{$d}
14+
{$unknown}

tests/fixtures/parser/ast/ts-store01-no-undef-result.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"ruleId": "no-undef",
4-
"code": "$d",
4+
"code": "$unknown",
55
"line": 14,
66
"column": 2
77
}

tests/fixtures/parser/ast/ts-store01-output.json

+13-13
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,10 @@
10121012
"kind": "text",
10131013
"expression": {
10141014
"type": "Identifier",
1015-
"name": "$d",
1015+
"name": "$unknown",
10161016
"range": [
10171017
197,
1018-
199
1018+
205
10191019
],
10201020
"loc": {
10211021
"start": {
@@ -1024,13 +1024,13 @@
10241024
},
10251025
"end": {
10261026
"line": 14,
1027-
"column": 3
1027+
"column": 9
10281028
}
10291029
}
10301030
},
10311031
"range": [
10321032
196,
1033-
200
1033+
206
10341034
],
10351035
"loc": {
10361036
"start": {
@@ -1039,7 +1039,7 @@
10391039
},
10401040
"end": {
10411041
"line": 14,
1042-
"column": 4
1042+
"column": 10
10431043
}
10441044
}
10451045
}
@@ -2291,10 +2291,10 @@
22912291
},
22922292
{
22932293
"type": "Identifier",
2294-
"value": "$d",
2294+
"value": "$unknown",
22952295
"range": [
22962296
197,
2297-
199
2297+
205
22982298
],
22992299
"loc": {
23002300
"start": {
@@ -2303,32 +2303,32 @@
23032303
},
23042304
"end": {
23052305
"line": 14,
2306-
"column": 3
2306+
"column": 9
23072307
}
23082308
}
23092309
},
23102310
{
23112311
"type": "Punctuator",
23122312
"value": "}",
23132313
"range": [
2314-
199,
2315-
200
2314+
205,
2315+
206
23162316
],
23172317
"loc": {
23182318
"start": {
23192319
"line": 14,
2320-
"column": 3
2320+
"column": 9
23212321
},
23222322
"end": {
23232323
"line": 14,
2324-
"column": 4
2324+
"column": 10
23252325
}
23262326
}
23272327
}
23282328
],
23292329
"range": [
23302330
0,
2331-
201
2331+
207
23322332
],
23332333
"loc": {
23342334
"start": {

tests/fixtures/parser/ast/ts-store01-scope-output.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -10847,10 +10847,10 @@
1084710847
{
1084810848
"identifier": {
1084910849
"type": "Identifier",
10850-
"name": "$d",
10850+
"name": "$unknown",
1085110851
"range": [
1085210852
197,
10853-
199
10853+
205
1085410854
],
1085510855
"loc": {
1085610856
"start": {
@@ -10859,7 +10859,7 @@
1085910859
},
1086010860
"end": {
1086110861
"line": 14,
10862-
"column": 3
10862+
"column": 9
1086310863
}
1086410864
}
1086510865
},
@@ -10873,10 +10873,10 @@
1087310873
{
1087410874
"identifier": {
1087510875
"type": "Identifier",
10876-
"name": "$d",
10876+
"name": "$unknown",
1087710877
"range": [
1087810878
197,
10879-
199
10879+
205
1088010880
],
1088110881
"loc": {
1088210882
"start": {
@@ -10885,7 +10885,7 @@
1088510885
},
1088610886
"end": {
1088710887
"line": 14,
10888-
"column": 3
10888+
"column": 9
1088910889
}
1089010890
}
1089110891
},
@@ -10900,10 +10900,10 @@
1090010900
{
1090110901
"identifier": {
1090210902
"type": "Identifier",
10903-
"name": "$d",
10903+
"name": "$unknown",
1090410904
"range": [
1090510905
197,
10906-
199
10906+
205
1090710907
],
1090810908
"loc": {
1090910909
"start": {
@@ -10912,7 +10912,7 @@
1091210912
},
1091310913
"end": {
1091410914
"line": 14,
10915-
"column": 3
10915+
"column": 9
1091610916
}
1091710917
}
1091810918
},

tests/fixtures/parser/ast/ts-store01-type-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
{$b} <!-- $b: "abc" -->
1212
{c} <!-- c: "abc" -->
1313
{$c} <!-- $c: any -->
14-
{$d} <!-- $d: any -->
14+
{$unknown} <!-- $unknown: any -->

0 commit comments

Comments
 (0)