Skip to content

Commit 726f21f

Browse files
authored
feat: Skip type injection if template uses TypeScript (#440)
1 parent 34232c5 commit 726f21f

20 files changed

+8589
-6
lines changed

.changeset/grumpy-dolphins-pay.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": minor
3+
---
4+
5+
feat: skip type injection if template uses TypeScript

explorer-v2/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"eslint-scope": "^7.2.2",
1919
"esquery": "^1.5.0",
2020
"pako": "^2.1.0",
21-
"svelte": "^5.0.0-next.8",
21+
"svelte": "^5.0.0-next.10",
2222
"svelte-eslint-parser": "link:..",
2323
"tslib": "^2.6.2"
2424
},

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
4747
},
4848
"peerDependencies": {
49-
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.8"
49+
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.10"
5050
},
5151
"peerDependenciesMeta": {
5252
"svelte": {
@@ -104,7 +104,7 @@
104104
"prettier-plugin-svelte": "^3.0.0",
105105
"rimraf": "^5.0.1",
106106
"semver": "^7.5.1",
107-
"svelte": "^5.0.0-next.8",
107+
"svelte": "^5.0.0-next.10",
108108
"svelte2tsx": "^0.6.25",
109109
"typescript": "~5.1.3",
110110
"typescript-eslint-parser-for-extra-files": "^0.5.0"

src/parser/converts/attr.ts

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { ParseError } from "../../errors";
3434
import type { ScriptLetCallback } from "../../context/script-let";
3535
import type { AttributeToken } from "../html";
3636
import { svelteVersion } from "../svelte-version";
37+
import { hasTypeInfo } from "../../utils";
3738

3839
/** Convert for Attributes */
3940
export function* convertAttributes(
@@ -922,6 +923,9 @@ function buildProcessExpressionForExpression(
922923
typing: string | null,
923924
): (expression: ESTree.Expression) => ScriptLetCallback<ESTree.Expression>[] {
924925
return (expression) => {
926+
if (hasTypeInfo(expression)) {
927+
return ctx.scriptLet.addExpression(expression, directive, null);
928+
}
925929
return ctx.scriptLet.addExpression(expression, directive, typing);
926930
};
927931
}

src/parser/converts/mustache.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type {
66
} from "../../ast";
77
import type { Context } from "../../context";
88
import type * as SvAST from "../svelte-ast-types";
9+
import { hasTypeInfo } from "../../utils";
10+
911
/** Convert for MustacheTag */
1012
export function convertMustacheTag(
1113
node: SvAST.MustacheTag,
@@ -76,8 +78,14 @@ function convertMustacheTag0<T extends SvelteMustacheTag>(
7678
parent,
7779
...ctx.getConvertLocation(node),
7880
} as T;
79-
ctx.scriptLet.addExpression(node.expression, mustache, typing, (es) => {
80-
mustache.expression = es;
81-
});
81+
82+
ctx.scriptLet.addExpression(
83+
node.expression,
84+
mustache,
85+
hasTypeInfo(node.expression) ? null : typing,
86+
(es) => {
87+
mustache.expression = es;
88+
},
89+
);
8290
return mustache;
8391
}

src/utils/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,22 @@ export function sortedLastIndex<T>(
5959

6060
return upper;
6161
}
62+
63+
export function hasTypeInfo(element: any): boolean {
64+
if (element.type?.startsWith("TS")) {
65+
return true;
66+
}
67+
for (const key of Object.keys(element)) {
68+
if (key === "parent") continue;
69+
const value = element[key];
70+
if (value == null) continue;
71+
if (typeof value === "object") {
72+
if (hasTypeInfo(value)) return true;
73+
} else if (Array.isArray(value)) {
74+
for (const v of value) {
75+
if (typeof v === "object" && hasTypeInfo(v)) return true;
76+
}
77+
}
78+
}
79+
return false;
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
let count = $state(0);
3+
</script>
4+
5+
<button
6+
on:click={(e: MouseEvent) => {
7+
const next: number = count + 1;
8+
count = next;
9+
}}
10+
>clicks: {count}</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"ruleId": "no-unused-vars",
4+
"code": "e: MouseEvent",
5+
"line": 6,
6+
"column": 13
7+
}
8+
]

0 commit comments

Comments
 (0)