From 859cc68039b166fb5f58127297ff68a7c132eb2b Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 19 Jul 2024 00:26:58 +0900 Subject: [PATCH 1/3] feat: support for svelte 5.0.0-next.191 --- package.json | 24 ++++++++++++------------ src/parser/converts/attr.ts | 33 ++++++++++++--------------------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index acae23e3..82810ec2 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", @@ -90,9 +90,9 @@ "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", @@ -100,12 +100,12 @@ "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" diff --git a/src/parser/converts/attr.ts b/src/parser/converts/attr.ts index d30e5b2b..1b1be527 100644 --- a/src/parser/converts/attr.ts +++ b/src/parser/converts/attr.ts @@ -150,15 +150,9 @@ function convertAttribute( ctx.addToken("HTMLIdentifier", keyRange); return attribute; } - const value = node.value as ( - | Compiler.Text - | Compiler.ExpressionTag - | SvAST.Text - | SvAST.MustacheTag - | SvAST.AttributeShorthand - )[]; + const value = Array.isArray(node.value) ? node.value : [node.value]; const shorthand = - value.find((v) => v.type === "AttributeShorthand") || + value.some((v) => v.type === "AttributeShorthand") || // for Svelte v5 (value.length === 1 && value[0].type === "ExpressionTag" && @@ -194,12 +188,7 @@ function convertAttribute( ctx.addToken("HTMLIdentifier", keyRange); processAttributeValue( - node.value as ( - | SvAST.Text - | SvAST.MustacheTag - | Compiler.Text - | Compiler.ExpressionTag - )[], + value as Exclude<(typeof value)[number], SvAST.AttributeShorthand>[], attribute, parent, ctx, @@ -210,17 +199,19 @@ function convertAttribute( /** 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" || From 48cf781734f675ded70e3ac9d3ea0c99c9e99f25 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Fri, 19 Jul 2024 00:28:09 +0900 Subject: [PATCH 2/3] Create chilly-dolls-push.md --- .changeset/chilly-dolls-push.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chilly-dolls-push.md diff --git a/.changeset/chilly-dolls-push.md b/.changeset/chilly-dolls-push.md new file mode 100644 index 00000000..df655769 --- /dev/null +++ b/.changeset/chilly-dolls-push.md @@ -0,0 +1,5 @@ +--- +"svelte-eslint-parser": minor +--- + +feat: support for svelte 5.0.0-next.191 From 91e49616573ecd049698fc40225b61ae10a761e8 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 19 Jul 2024 08:17:13 +0900 Subject: [PATCH 3/3] refactor --- src/parser/converts/attr.ts | 37 ++++++++++++++++++++++------------ src/parser/svelte-ast-types.ts | 2 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/parser/converts/attr.ts b/src/parser/converts/attr.ts index 1b1be527..da8a5b3d 100644 --- a/src/parser/converts/attr.ts +++ b/src/parser/converts/attr.ts @@ -150,14 +150,10 @@ function convertAttribute( ctx.addToken("HTMLIdentifier", keyRange); return attribute; } - const value = Array.isArray(node.value) ? node.value : [node.value]; + const value = node.value; const shorthand = - value.some((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, @@ -187,14 +183,29 @@ function convertAttribute( // Not required for shorthands. Therefore, register the token here. ctx.addToken("HTMLIdentifier", keyRange); - processAttributeValue( - value as Exclude<(typeof value)[number], SvAST.AttributeShorthand>[], - 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 */ diff --git a/src/parser/svelte-ast-types.ts b/src/parser/svelte-ast-types.ts index 31c8a3a8..6cd31660 100644 --- a/src/parser/svelte-ast-types.ts +++ b/src/parser/svelte-ast-types.ts @@ -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";