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

fix: missing ts node for key of shorthand attribute #238

Merged
merged 3 commits into from
Oct 28, 2022
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-experts-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: missing ts node for key of shorthand attribute
39 changes: 39 additions & 0 deletions src/context/script-let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ type RestoreCallback = {

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

type ObjectShorthandProperty = ESTree.Property & {
key: ESTree.Identifier;
value: ESTree.Identifier;
};

/**
* A class that handles script fragments.
* The script fragment AST node remaps and connects to the original directive AST node.
Expand Down Expand Up @@ -185,6 +190,40 @@ export class ScriptLetContext {
return callbacks;
}

public addObjectShorthandProperty(
identifier: SvelteName,
parent: SvelteNode,
...callbacks: ScriptLetCallback<ObjectShorthandProperty>[]
): void {
const range = getNodeRange(identifier);
const part = this.ctx.code.slice(...range);
this.appendScript(
`({${part}});`,
range[0] - 2,
(st, tokens, _comments, result) => {
const exprSt = st as ESTree.ExpressionStatement;
const objectExpression: ESTree.ObjectExpression =
exprSt.expression as ESTree.ObjectExpression;
const node = objectExpression.properties[0] as ObjectShorthandProperty;
// Process for nodes
for (const callback of callbacks) {
callback(node, result);
}
(node.key as any).parent = parent;
(node.value as any).parent = parent;

tokens.shift(); // (
tokens.shift(); // {
tokens.pop(); // }
tokens.pop(); // )
tokens.pop(); // ;

// Disconnect the tree structure.
exprSt.expression = null as never;
}
);
}

public addVariableDeclarator(
expression: ESTree.AssignmentExpression,
parent: SvelteNode,
Expand Down
11 changes: 9 additions & 2 deletions src/parser/converts/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,15 @@ function convertAttribute(
range: attribute.range,
};
(key as any).parent = sAttr;
ctx.scriptLet.addExpression(key, sAttr, null, (es) => {
sAttr.value = es;
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
if (
// FIXME: Older parsers may use the same node. In that case, do not replace.
// We will drop support for ESLint v7 in the next major version and remove this branch.
es.key !== es.value
) {
sAttr.key = es.key;
}
sAttr.value = es.value;
});
return sAttr;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/parser/ast/ts-shorthand-attr01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts">
const src = 'Hello';
</script>

<img src={src} alt="foo">
<img {src} alt="foo">
Loading