Skip to content

Commit 91e4961

Browse files
committed
refactor
1 parent 48cf781 commit 91e4961

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/parser/converts/attr.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,10 @@ function convertAttribute(
150150
ctx.addToken("HTMLIdentifier", keyRange);
151151
return attribute;
152152
}
153-
const value = Array.isArray(node.value) ? node.value : [node.value];
153+
const value = node.value;
154154
const shorthand =
155-
value.some((v) => v.type === "AttributeShorthand") ||
156-
// for Svelte v5
157-
(value.length === 1 &&
158-
value[0].type === "ExpressionTag" &&
159-
ctx.code[node.start] === "{" &&
160-
ctx.code[node.end - 1] === "}");
155+
isAttributeShorthandForSvelteV4(value) ||
156+
isAttributeShorthandForSvelteV5(value);
161157
if (shorthand) {
162158
const key: ESTree.Identifier = {
163159
...attribute.key,
@@ -187,14 +183,29 @@ function convertAttribute(
187183
// Not required for shorthands. Therefore, register the token here.
188184
ctx.addToken("HTMLIdentifier", keyRange);
189185

190-
processAttributeValue(
191-
value as Exclude<(typeof value)[number], SvAST.AttributeShorthand>[],
192-
attribute,
193-
parent,
194-
ctx,
195-
);
186+
processAttributeValue(value, attribute, parent, ctx);
196187

197188
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+
}
198209
}
199210

200211
/** Common process attribute value */

src/parser/svelte-ast-types.ts

+1-1
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)