Skip to content

Commit ef24a69

Browse files
authored
feat: support for svelte 5.0.0-next.191 (#550)
1 parent 7b29182 commit ef24a69

File tree

4 files changed

+51
-44
lines changed

4 files changed

+51
-44
lines changed

.changeset/chilly-dolls-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": minor
3+
---
4+
5+
feat: support for svelte 5.0.0-next.191

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
4848
},
4949
"peerDependencies": {
50-
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.181"
50+
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.191"
5151
},
5252
"peerDependenciesMeta": {
5353
"svelte": {
@@ -73,11 +73,11 @@
7373
"@types/eslint-visitor-keys": "^3.3.0",
7474
"@types/estree": "^1.0.5",
7575
"@types/mocha": "^10.0.7",
76-
"@types/node": "^20.14.10",
76+
"@types/node": "^20.14.11",
7777
"@types/semver": "^7.5.8",
78-
"@typescript-eslint/eslint-plugin": "^7.16.0",
79-
"@typescript-eslint/parser": "~7.16.0",
80-
"@typescript-eslint/types": "~7.16.0",
78+
"@typescript-eslint/eslint-plugin": "^7.16.1",
79+
"@typescript-eslint/parser": "~7.16.1",
80+
"@typescript-eslint/types": "~7.16.1",
8181
"benchmark": "^2.1.4",
8282
"chai": "^4.4.1",
8383
"env-cmd": "^10.1.0",
@@ -90,22 +90,22 @@
9090
"eslint-plugin-jsonc": "^2.16.0",
9191
"eslint-plugin-n": "^17.9.0",
9292
"eslint-plugin-node-dependencies": "^0.12.0",
93-
"eslint-plugin-prettier": "^5.1.3",
93+
"eslint-plugin-prettier": "^5.2.1",
9494
"eslint-plugin-regexp": "^2.6.0",
95-
"eslint-plugin-svelte": "^2.41.0",
95+
"eslint-plugin-svelte": "^2.42.0",
9696
"eslint-plugin-yml": "^1.14.0",
9797
"estree-walker": "^3.0.3",
9898
"locate-character": "^3.0.0",
9999
"magic-string": "^0.30.10",
100100
"mocha": "^10.6.0",
101101
"mocha-chai-jest-snapshot": "^1.1.4",
102102
"nyc": "^17.0.0",
103-
"prettier": "~3.3.2",
103+
"prettier": "~3.3.3",
104104
"prettier-plugin-pkg": "^0.18.1",
105-
"prettier-plugin-svelte": "^3.2.5",
106-
"rimraf": "^6.0.0",
107-
"semver": "^7.6.2",
108-
"svelte": "^5.0.0-next.181",
105+
"prettier-plugin-svelte": "^3.2.6",
106+
"rimraf": "^6.0.1",
107+
"semver": "^7.6.3",
108+
"svelte": "^5.0.0-next.191",
109109
"svelte2tsx": "^0.7.13",
110110
"typescript": "~5.5.3",
111111
"typescript-eslint-parser-for-extra-files": "^0.7.0"

src/parser/converts/attr.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,10 @@ function convertAttribute(
150150
ctx.addToken("HTMLIdentifier", keyRange);
151151
return attribute;
152152
}
153-
const value = node.value as (
154-
| Compiler.Text
155-
| Compiler.ExpressionTag
156-
| SvAST.Text
157-
| SvAST.MustacheTag
158-
| SvAST.AttributeShorthand
159-
)[];
153+
const value = node.value;
160154
const shorthand =
161-
value.find((v) => v.type === "AttributeShorthand") ||
162-
// for Svelte v5
163-
(value.length === 1 &&
164-
value[0].type === "ExpressionTag" &&
165-
ctx.code[node.start] === "{" &&
166-
ctx.code[node.end - 1] === "}");
155+
isAttributeShorthandForSvelteV4(value) ||
156+
isAttributeShorthandForSvelteV5(value);
167157
if (shorthand) {
168158
const key: ESTree.Identifier = {
169159
...attribute.key,
@@ -193,34 +183,46 @@ function convertAttribute(
193183
// Not required for shorthands. Therefore, register the token here.
194184
ctx.addToken("HTMLIdentifier", keyRange);
195185

196-
processAttributeValue(
197-
node.value as (
198-
| SvAST.Text
199-
| SvAST.MustacheTag
200-
| Compiler.Text
201-
| Compiler.ExpressionTag
202-
)[],
203-
attribute,
204-
parent,
205-
ctx,
206-
);
186+
processAttributeValue(value, attribute, parent, ctx);
207187

208188
return attribute;
189+
190+
function isAttributeShorthandForSvelteV4(
191+
value: Exclude<(SvAST.Attribute | Compiler.Attribute)["value"], boolean>,
192+
): value is [SvAST.AttributeShorthand] {
193+
return Array.isArray(value) && value[0]?.type === "AttributeShorthand";
194+
}
195+
196+
function isAttributeShorthandForSvelteV5(
197+
value: Exclude<
198+
(SvAST.Attribute | Compiler.Attribute)["value"],
199+
boolean | [SvAST.AttributeShorthand]
200+
>,
201+
): boolean {
202+
return (
203+
!Array.isArray(value) &&
204+
value.type === "ExpressionTag" &&
205+
ctx.code[node.start] === "{" &&
206+
ctx.code[node.end - 1] === "}"
207+
);
208+
}
209209
}
210210

211211
/** Common process attribute value */
212212
function processAttributeValue(
213-
nodeValue: (
214-
| SvAST.Text
215-
| SvAST.MustacheTag
216-
| Compiler.Text
217-
| Compiler.ExpressionTag
218-
)[],
213+
nodeValue:
214+
| (
215+
| SvAST.Text
216+
| SvAST.MustacheTag
217+
| Compiler.Text
218+
| Compiler.ExpressionTag
219+
)[]
220+
| Compiler.ExpressionTag,
219221
attribute: SvelteAttribute | SvelteStyleDirectiveLongform,
220222
attributeParent: (SvelteAttribute | SvelteStyleDirectiveLongform)["parent"],
221223
ctx: Context,
222224
) {
223-
const nodes = nodeValue
225+
const nodes = (Array.isArray(nodeValue) ? nodeValue : [nodeValue])
224226
.filter(
225227
(v) =>
226228
v.type !== "Text" ||

src/parser/svelte-ast-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export interface Comment extends BaseNode {
192192
export interface Attribute extends BaseNode {
193193
type: "Attribute";
194194
name: string;
195-
value: (Text | AttributeShorthand | MustacheTag)[] | true;
195+
value: (Text | MustacheTag)[] | [AttributeShorthand] | true;
196196
}
197197
export interface Spread extends BaseNode {
198198
type: "Spread";

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy