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: crash with unquoted attribute value #280

Merged
merged 3 commits into from
Jan 11, 2024
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/mean-eagles-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-eslint-parser": patch
---

fix: crash with unquoted attribute value
5 changes: 5 additions & 0 deletions src/context/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class RestoreNodeProcessContext {
public getParent(node: TSESTree.Node): TSESTree.Node | null {
return this.nodeMap.get(node) || null;
}

public findToken(startIndex: number): TSESTree.Token | null {
const tokens = this.result.ast.tokens || [];
return tokens.find((t) => t.range[0] === startIndex) || null;
}
}

export class RestoreContext {
Expand Down
37 changes: 36 additions & 1 deletion src/parser/process-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,42 @@ export function processTemplate(
processAttributePunctuators(attr);
}
}
if (attr.kind === "shorthand") {
if (attr.kind === "quoted") {
if (
attr.raw &&
!attr.raw.startsWith('"') &&
!attr.raw.startsWith("'")
) {
const attrStart = attr.position!.start.offset;
const start = calcAttributeValueStartOffset(attr, ctx);
const end = calcAttributeEndOffset(attr, ctx);
script.appendOriginal(start);
script.appendVirtualScript('"');
script.appendOriginal(end);
script.appendVirtualScript('"');

script.restoreContext.addRestoreNodeProcess(
(scriptNode, context) => {
if (
scriptNode.type === AST_NODE_TYPES.JSXAttribute &&
scriptNode.range[0] === attrStart
) {
const attrNode = scriptNode;
if (
attrNode.value?.type === "Literal" &&
typeof attrNode.value.value === "string"
) {
const raw = ctx.code.slice(start, end);
attrNode.value.raw = raw;
context.findToken(start)!.value = raw;
return true;
}
}
return false;
},
);
}
} else if (attr.kind === "shorthand") {
const start = attr.position!.start.offset;
script.appendOriginal(start);
const jsxName = /[\s"'[\]{}]/u.test(attr.name)
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/parser/ast/attr01-unquote-input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

<div title=foo></div>
Loading
Loading