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: support for svelte 5.0.0-next.191 #550

Merged
merged 3 commits into from
Jul 19, 2024
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/chilly-dolls-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": minor
---

feat: support for svelte 5.0.0-next.191
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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.181"
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.191"
},
"peerDependenciesMeta": {
"svelte": {
Expand All @@ -73,11 +73,11 @@
"@types/eslint-visitor-keys": "^3.3.0",
"@types/estree": "^1.0.5",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.10",
"@types/node": "^20.14.11",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^7.16.0",
"@typescript-eslint/parser": "~7.16.0",
"@typescript-eslint/types": "~7.16.0",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "~7.16.1",
"@typescript-eslint/types": "~7.16.1",
"benchmark": "^2.1.4",
"chai": "^4.4.1",
"env-cmd": "^10.1.0",
Expand All @@ -90,22 +90,22 @@
"eslint-plugin-jsonc": "^2.16.0",
"eslint-plugin-n": "^17.9.0",
"eslint-plugin-node-dependencies": "^0.12.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-regexp": "^2.6.0",
"eslint-plugin-svelte": "^2.41.0",
"eslint-plugin-svelte": "^2.42.0",
"eslint-plugin-yml": "^1.14.0",
"estree-walker": "^3.0.3",
"locate-character": "^3.0.0",
"magic-string": "^0.30.10",
"mocha": "^10.6.0",
"mocha-chai-jest-snapshot": "^1.1.4",
"nyc": "^17.0.0",
"prettier": "~3.3.2",
"prettier": "~3.3.3",
"prettier-plugin-pkg": "^0.18.1",
"prettier-plugin-svelte": "^3.2.5",
"rimraf": "^6.0.0",
"semver": "^7.6.2",
"svelte": "^5.0.0-next.181",
"prettier-plugin-svelte": "^3.2.6",
"rimraf": "^6.0.1",
"semver": "^7.6.3",
"svelte": "^5.0.0-next.191",
"svelte2tsx": "^0.7.13",
"typescript": "~5.5.3",
"typescript-eslint-parser-for-extra-files": "^0.7.0"
Expand Down
64 changes: 33 additions & 31 deletions src/parser/converts/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,10 @@
ctx.addToken("HTMLIdentifier", keyRange);
return attribute;
}
const value = node.value as (
| Compiler.Text
| Compiler.ExpressionTag
| SvAST.Text
| SvAST.MustacheTag
| SvAST.AttributeShorthand
)[];
const value = node.value;
const shorthand =
value.find((v) => v.type === "AttributeShorthand") ||
// for Svelte v5
(value.length === 1 &&
value[0].type === "ExpressionTag" &&
ctx.code[node.start] === "{" &&
ctx.code[node.end - 1] === "}");
isAttributeShorthandForSvelteV4(value) ||
isAttributeShorthandForSvelteV5(value);
if (shorthand) {
const key: ESTree.Identifier = {
...attribute.key,
Expand All @@ -180,7 +170,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 173 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 All @@ -193,34 +183,46 @@
// Not required for shorthands. Therefore, register the token here.
ctx.addToken("HTMLIdentifier", keyRange);

processAttributeValue(
node.value as (
| SvAST.Text
| SvAST.MustacheTag
| Compiler.Text
| Compiler.ExpressionTag
)[],
attribute,
parent,
ctx,
);
processAttributeValue(value, attribute, parent, ctx);

return attribute;

function isAttributeShorthandForSvelteV4(
value: Exclude<(SvAST.Attribute | Compiler.Attribute)["value"], boolean>,
): value is [SvAST.AttributeShorthand] {
return Array.isArray(value) && value[0]?.type === "AttributeShorthand";
}

function isAttributeShorthandForSvelteV5(
value: Exclude<
(SvAST.Attribute | Compiler.Attribute)["value"],
boolean | [SvAST.AttributeShorthand]
>,
): boolean {
return (
!Array.isArray(value) &&
value.type === "ExpressionTag" &&
ctx.code[node.start] === "{" &&
ctx.code[node.end - 1] === "}"
);
}
}

/** Common process attribute value */
function processAttributeValue(
nodeValue: (
| SvAST.Text
| SvAST.MustacheTag
| Compiler.Text
| Compiler.ExpressionTag
)[],
nodeValue:
| (
| SvAST.Text
| SvAST.MustacheTag
| Compiler.Text
| Compiler.ExpressionTag
)[]
| Compiler.ExpressionTag,
attribute: SvelteAttribute | SvelteStyleDirectiveLongform,
attributeParent: (SvelteAttribute | SvelteStyleDirectiveLongform)["parent"],
ctx: Context,
) {
const nodes = nodeValue
const nodes = (Array.isArray(nodeValue) ? nodeValue : [nodeValue])
.filter(
(v) =>
v.type !== "Text" ||
Expand Down
2 changes: 1 addition & 1 deletion src/parser/svelte-ast-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export interface Comment extends BaseNode {
export interface Attribute extends BaseNode {
type: "Attribute";
name: string;
value: (Text | AttributeShorthand | MustacheTag)[] | true;
value: (Text | MustacheTag)[] | [AttributeShorthand] | true;
}
export interface Spread extends BaseNode {
type: "Spread";
Expand Down
Loading