Skip to content

Commit b705850

Browse files
Merge pull request #2650 from johanrd/post-merge-review/no-quoteless-attributes
Post merge-review: Fix `template-no-quoteless-attributes` false positive on quoted values
2 parents d3e985f + 5588e6a commit b705850

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

lib/rules/template-no-quoteless-attributes.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,27 @@ module.exports = {
2121
},
2222
},
2323
create(context) {
24+
const sourceCode = context.sourceCode;
2425
return {
2526
GlimmerAttrNode(node) {
26-
// Check if attribute has text value without quotes
27-
if (node.value?.type === 'GlimmerTextNode' && !/^["']/.test(node.value.chars)) {
28-
const sourceCode = context.sourceCode;
29-
const attrText = sourceCode.getText(node);
27+
if (node.value?.type !== 'GlimmerTextNode') {
28+
return;
29+
}
3030

31-
// If value looks unquoted (no = or =value without quotes)
32-
if (/=\s*[^"'{]/.test(attrText)) {
33-
const type = node.name?.startsWith('@') ? 'Argument' : 'Attribute';
34-
context.report({
35-
node,
36-
messageId: 'missing',
37-
data: { type, name: node.name },
38-
fix(fixer) {
39-
const valueText = node.value.chars;
40-
const replacementText = `${node.name}="${valueText}"`;
41-
return fixer.replaceText(node, replacementText);
42-
},
43-
});
44-
}
31+
const valueSource = sourceCode.getText(node.value);
32+
if (valueSource.length === 0 || valueSource[0] === '"' || valueSource[0] === "'") {
33+
return;
4534
}
35+
36+
const type = node.name?.startsWith('@') ? 'Argument' : 'Attribute';
37+
context.report({
38+
node,
39+
messageId: 'missing',
40+
data: { type, name: node.name },
41+
fix(fixer) {
42+
return fixer.replaceText(node, `${node.name}="${node.value.chars}"`);
43+
},
44+
});
4645
},
4746
};
4847
},

tests/lib/rules/template-no-quoteless-attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ruleTester.run('template-no-quoteless-attributes', rule, {
1717
'<template><SomeThing ...attributes /></template>',
1818
'<template><div></div></template>',
1919
'<template><input disabled></template>',
20+
'<template><div data-x="foo=bar"></div></template>',
2021
],
2122
invalid: [
2223
{
@@ -57,6 +58,7 @@ hbsRuleTester.run('template-no-quoteless-attributes', rule, {
5758
'<SomeThing ...attributes />',
5859
'<div></div>',
5960
'<input disabled>',
61+
'<div data-x="foo=bar"></div>',
6062
],
6163
invalid: [
6264
{

0 commit comments

Comments
 (0)