Skip to content

fix: wrong type information for #await with same id #371

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 3 commits into from
Jul 8, 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/twenty-pandas-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: wrong type information for `#await` with same id
33 changes: 32 additions & 1 deletion src/parser/converts/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ export function convertAwaitBlock(
};
}
const idAwaitThenValue = typeCtx.generateUniqueId("AwaitThenValue");
if (node.expression.type === "Identifier") {
if (
node.expression.type === "Identifier" &&
// We cannot use type annotations like `(x: Foo<x>)` if they have the same identifier name.
!hasIdentifierFor(node.expression.name, baseParam.node)
) {
return {
preparationScript: [generateAwaitThenValueType(idAwaitThenValue)],
param: {
Expand Down Expand Up @@ -484,3 +488,30 @@ function generateAwaitThenValueType(id: string) {
: never
: T;`;
}

/** Checks whether the given name identifier is exists or not. */
function hasIdentifierFor(name: string, node: ESTree.Pattern): boolean {
if (node.type === "Identifier") {
return node.name === name;
}
if (node.type === "ObjectPattern") {
return node.properties.some((property) =>
property.type === "Property"
? hasIdentifierFor(name, property.value)
: hasIdentifierFor(name, property)
);
}
if (node.type === "ArrayPattern") {
return node.elements.some(
(element) => element && hasIdentifierFor(name, element)
);
}
if (node.type === "RestElement") {
return hasIdentifierFor(name, node.argument);
}
if (node.type === "AssignmentPattern") {
return hasIdentifierFor(name, node.left);
}

return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script lang="ts">
let a: Promise<{ x: number }>
let b: Promise<{ x: number }>
let c: Promise<{ x: number }>
let d: Promise<number[]>
let e: Promise<number[]>
</script>

{#await a}
<div>await</div>
{:then a}
<div>{a.x}</div>
{/await}

{#await b}
<div>await</div>
{:then { x: b = 42 }}
<div>{b.toExponential()}</div>
{/await}

{#await c}
<div>await</div>
{:then {...c}}
<div>{c.x}</div>
{/await}

{#await d}
<div>await</div>
{:then [d]}
<div>{d.toExponential()}</div>
{/await}

{#await e}
<div>await</div>
{:then [...e]}
<div>{e[0].toExponential()}</div>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */
import type { Linter } from "eslint";
import { generateParserOptions } from "../../../src/parser/test-utils";
import { rules } from "@typescript-eslint/eslint-plugin";
export function setupLinter(linter: Linter) {
linter.defineRule(
"@typescript-eslint/no-unsafe-member-access",
rules["no-unsafe-member-access"] as never
);
}

export function getConfig() {
return {
parser: "svelte-eslint-parser",
parserOptions: generateParserOptions(),
rules: {
"@typescript-eslint/no-unsafe-member-access": "error",
},
env: {
browser: true,
es2021: true,
},
};
}
2 changes: 1 addition & 1 deletion tests/fixtures/parser/ast/ts-store01-input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
{$b}
{c}
{$c}
{$d}
{$unknown}
2 changes: 1 addition & 1 deletion tests/fixtures/parser/ast/ts-store01-no-undef-result.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"ruleId": "no-undef",
"code": "$d",
"code": "$unknown",
"line": 14,
"column": 2
}
Expand Down
26 changes: 13 additions & 13 deletions tests/fixtures/parser/ast/ts-store01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,10 @@
"kind": "text",
"expression": {
"type": "Identifier",
"name": "$d",
"name": "$unknown",
"range": [
197,
199
205
],
"loc": {
"start": {
Expand All @@ -1024,13 +1024,13 @@
},
"end": {
"line": 14,
"column": 3
"column": 9
}
}
},
"range": [
196,
200
206
],
"loc": {
"start": {
Expand All @@ -1039,7 +1039,7 @@
},
"end": {
"line": 14,
"column": 4
"column": 10
}
}
}
Expand Down Expand Up @@ -2291,10 +2291,10 @@
},
{
"type": "Identifier",
"value": "$d",
"value": "$unknown",
"range": [
197,
199
205
],
"loc": {
"start": {
Expand All @@ -2303,32 +2303,32 @@
},
"end": {
"line": 14,
"column": 3
"column": 9
}
}
},
{
"type": "Punctuator",
"value": "}",
"range": [
199,
200
205,
206
],
"loc": {
"start": {
"line": 14,
"column": 3
"column": 9
},
"end": {
"line": 14,
"column": 4
"column": 10
}
}
}
],
"range": [
0,
201
207
],
"loc": {
"start": {
Expand Down
18 changes: 9 additions & 9 deletions tests/fixtures/parser/ast/ts-store01-scope-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -10847,10 +10847,10 @@
{
"identifier": {
"type": "Identifier",
"name": "$d",
"name": "$unknown",
"range": [
197,
199
205
],
"loc": {
"start": {
Expand All @@ -10859,7 +10859,7 @@
},
"end": {
"line": 14,
"column": 3
"column": 9
}
}
},
Expand All @@ -10873,10 +10873,10 @@
{
"identifier": {
"type": "Identifier",
"name": "$d",
"name": "$unknown",
"range": [
197,
199
205
],
"loc": {
"start": {
Expand All @@ -10885,7 +10885,7 @@
},
"end": {
"line": 14,
"column": 3
"column": 9
}
}
},
Expand All @@ -10900,10 +10900,10 @@
{
"identifier": {
"type": "Identifier",
"name": "$d",
"name": "$unknown",
"range": [
197,
199
205
],
"loc": {
"start": {
Expand All @@ -10912,7 +10912,7 @@
},
"end": {
"line": 14,
"column": 3
"column": 9
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/parser/ast/ts-store01-type-output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
{$b} <!-- $b: "abc" -->
{c} <!-- c: "abc" -->
{$c} <!-- $c: any -->
{$d} <!-- $d: any -->
{$unknown} <!-- $unknown: any -->