Skip to content

Commit

Permalink
feat: Skip type injection if template uses TypeScript (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Nov 26, 2023
1 parent 34232c5 commit 726f21f
Show file tree
Hide file tree
Showing 20 changed files with 8,589 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-dolphins-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": minor
---

feat: skip type injection if template uses TypeScript
2 changes: 1 addition & 1 deletion explorer-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"eslint-scope": "^7.2.2",
"esquery": "^1.5.0",
"pako": "^2.1.0",
"svelte": "^5.0.0-next.8",
"svelte": "^5.0.0-next.10",
"svelte-eslint-parser": "link:..",
"tslib": "^2.6.2"
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
},
"peerDependencies": {
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.8"
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.10"
},
"peerDependenciesMeta": {
"svelte": {
Expand Down Expand Up @@ -104,7 +104,7 @@
"prettier-plugin-svelte": "^3.0.0",
"rimraf": "^5.0.1",
"semver": "^7.5.1",
"svelte": "^5.0.0-next.8",
"svelte": "^5.0.0-next.10",
"svelte2tsx": "^0.6.25",
"typescript": "~5.1.3",
"typescript-eslint-parser-for-extra-files": "^0.5.0"
Expand Down
4 changes: 4 additions & 0 deletions src/parser/converts/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ParseError } from "../../errors";
import type { ScriptLetCallback } from "../../context/script-let";
import type { AttributeToken } from "../html";
import { svelteVersion } from "../svelte-version";
import { hasTypeInfo } from "../../utils";

/** Convert for Attributes */
export function* convertAttributes(
Expand Down Expand Up @@ -922,6 +923,9 @@ function buildProcessExpressionForExpression(
typing: string | null,
): (expression: ESTree.Expression) => ScriptLetCallback<ESTree.Expression>[] {
return (expression) => {
if (hasTypeInfo(expression)) {
return ctx.scriptLet.addExpression(expression, directive, null);
}
return ctx.scriptLet.addExpression(expression, directive, typing);
};
}
Expand Down
14 changes: 11 additions & 3 deletions src/parser/converts/mustache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {
} from "../../ast";
import type { Context } from "../../context";
import type * as SvAST from "../svelte-ast-types";
import { hasTypeInfo } from "../../utils";

/** Convert for MustacheTag */
export function convertMustacheTag(
node: SvAST.MustacheTag,
Expand Down Expand Up @@ -76,8 +78,14 @@ function convertMustacheTag0<T extends SvelteMustacheTag>(
parent,
...ctx.getConvertLocation(node),
} as T;
ctx.scriptLet.addExpression(node.expression, mustache, typing, (es) => {
mustache.expression = es;
});

ctx.scriptLet.addExpression(
node.expression,
mustache,
hasTypeInfo(node.expression) ? null : typing,
(es) => {
mustache.expression = es;
},
);
return mustache;
}
19 changes: 19 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ export function sortedLastIndex<T>(

return upper;
}

export function hasTypeInfo(element: any): boolean {
if (element.type?.startsWith("TS")) {
return true;
}
for (const key of Object.keys(element)) {
if (key === "parent") continue;
const value = element[key];
if (value == null) continue;
if (typeof value === "object") {
if (hasTypeInfo(value)) return true;
} else if (Array.isArray(value)) {
for (const v of value) {
if (typeof v === "object" && hasTypeInfo(v)) return true;
}
}
}
return false;
}
10 changes: 10 additions & 0 deletions tests/fixtures/parser/ast/svelte5/ts-event07-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
let count = $state(0);
</script>

<button
on:click={(e: MouseEvent) => {
const next: number = count + 1;
count = next;
}}
>clicks: {count}</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"ruleId": "no-unused-vars",
"code": "e: MouseEvent",
"line": 6,
"column": 13
}
]

0 comments on commit 726f21f

Please sign in to comment.