Skip to content
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

feat: Skip type injection if template uses TypeScript #440

Merged
merged 8 commits into from
Nov 26, 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/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 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 @@ -188,7 +189,7 @@
(key as any).parent = sAttr;
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
if (
// FIXME: Older parsers may use the same node. In that case, do not replace.

Check warning on line 192 in src/parser/converts/attr.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Older parsers may use the same...'
// We will drop support for ESLint v7 in the next major version and remove this branch.
es.key !== es.value
) {
Expand Down Expand Up @@ -922,6 +923,9 @@
typing: string | null,
): (expression: ESTree.Expression) => ScriptLetCallback<ESTree.Expression>[] {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ota-meshi

Should I use TSESTree here? Write ESTree.Expression | TSESTree.Expression everytime is hassle, and since the type information is only needed in a few places, I thought it would be okay to leave the type information as it is. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's OK to leave it as is for now 👍. I think that acorn-typescript nodes and TSESTree are not compatible, so just using TSESTree will not be a complete solution.

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
}
]
Loading
Loading