Skip to content

Commit 6e063c2

Browse files
authored
fix: missing ts node for key of shorthand attribute (#238)
* fix: missing ts node for key of shorthand attribute * Create chilly-experts-reply.md * fix: for old parsers
1 parent 4591b3b commit 6e063c2

8 files changed

+10162
-3
lines changed

Diff for: .changeset/chilly-experts-reply.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: missing ts node for key of shorthand attribute

Diff for: src/context/script-let.ts

+39
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ type RestoreCallback = {
107107

108108
type TypeGenHelper = { generateUniqueId: (base: string) => string };
109109

110+
type ObjectShorthandProperty = ESTree.Property & {
111+
key: ESTree.Identifier;
112+
value: ESTree.Identifier;
113+
};
114+
110115
/**
111116
* A class that handles script fragments.
112117
* The script fragment AST node remaps and connects to the original directive AST node.
@@ -185,6 +190,40 @@ export class ScriptLetContext {
185190
return callbacks;
186191
}
187192

193+
public addObjectShorthandProperty(
194+
identifier: SvelteName,
195+
parent: SvelteNode,
196+
...callbacks: ScriptLetCallback<ObjectShorthandProperty>[]
197+
): void {
198+
const range = getNodeRange(identifier);
199+
const part = this.ctx.code.slice(...range);
200+
this.appendScript(
201+
`({${part}});`,
202+
range[0] - 2,
203+
(st, tokens, _comments, result) => {
204+
const exprSt = st as ESTree.ExpressionStatement;
205+
const objectExpression: ESTree.ObjectExpression =
206+
exprSt.expression as ESTree.ObjectExpression;
207+
const node = objectExpression.properties[0] as ObjectShorthandProperty;
208+
// Process for nodes
209+
for (const callback of callbacks) {
210+
callback(node, result);
211+
}
212+
(node.key as any).parent = parent;
213+
(node.value as any).parent = parent;
214+
215+
tokens.shift(); // (
216+
tokens.shift(); // {
217+
tokens.pop(); // }
218+
tokens.pop(); // )
219+
tokens.pop(); // ;
220+
221+
// Disconnect the tree structure.
222+
exprSt.expression = null as never;
223+
}
224+
);
225+
}
226+
188227
public addVariableDeclarator(
189228
expression: ESTree.AssignmentExpression,
190229
parent: SvelteNode,

Diff for: src/parser/converts/attr.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,15 @@ function convertAttribute(
180180
range: attribute.range,
181181
};
182182
(key as any).parent = sAttr;
183-
ctx.scriptLet.addExpression(key, sAttr, null, (es) => {
184-
sAttr.value = es;
183+
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
184+
if (
185+
// FIXME: Older parsers may use the same node. In that case, do not replace.
186+
// We will drop support for ESLint v7 in the next major version and remove this branch.
187+
es.key !== es.value
188+
) {
189+
sAttr.key = es.key;
190+
}
191+
sAttr.value = es.value;
185192
});
186193
return sAttr;
187194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script lang="ts">
2+
const src = 'Hello';
3+
</script>
4+
5+
<img src={src} alt="foo">
6+
<img {src} alt="foo">

0 commit comments

Comments
 (0)