Skip to content

Commit 48bb2a1

Browse files
committed
Sync with template-lint
1 parent 9b5f84a commit 48bb2a1

3 files changed

Lines changed: 55 additions & 60 deletions

File tree

docs/rules/template-no-valueless-arguments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ This rule **forbids** the following:
1313
```
1414

1515
```gjs
16-
<template><SomeComponent @valuelessByAccident {{@canBeAModifier}} /></template>
16+
<template><SomeComponent @valuelessByAccident{{@canBeAModifier}} /></template>
1717
```
1818

1919
This rule **allows** the following:
2020

2121
```gjs
22-
<template><Editor @defaultText='' /></template>
22+
<template><Editor @defaultText="" /></template>
2323
```
2424

2525
## Migration

lib/rules/template-no-valueless-arguments.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/** @type {import('eslint').Rule.RuleModule} */
2+
function isNamedArgument(attrName) {
3+
return attrName.startsWith('@');
4+
}
5+
26
module.exports = {
37
meta: {
48
type: 'problem',
@@ -18,16 +22,16 @@ module.exports = {
1822
},
1923
},
2024
create(context) {
25+
const sourceCode = context.sourceCode || context.getSourceCode();
26+
2127
return {
2228
GlimmerAttrNode(node) {
23-
// Check if it's a named argument (@foo) with no value
24-
if (node.name?.startsWith('@')) {
25-
const sourceCode = context.sourceCode || context.getSourceCode();
26-
const text = sourceCode.getText(node);
27-
// Truly valueless if no '=' in the source text
28-
if (!text.includes('=')) {
29-
context.report({ node, messageId: 'valueless' });
30-
}
29+
if (!isNamedArgument(node.name)) {
30+
return;
31+
}
32+
33+
if (!sourceCode.getText(node).includes('=')) {
34+
context.report({ node, messageId: 'valueless' });
3135
}
3236
},
3337
};
Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
const rule = require('../../../lib/rules/template-no-valueless-arguments');
22
const RuleTester = require('eslint').RuleTester;
33

4-
const ruleTester = new RuleTester({
4+
const validHbs = [
5+
'<SomeComponent @emptyString="" data-test-some-component />',
6+
'<button type="submit" disabled {{on "click" this.submit}}></button>',
7+
];
8+
9+
const invalidHbs = [
10+
{
11+
code: '<SomeComponent @valueless />',
12+
output: null,
13+
errors: [{ messageId: 'valueless' }],
14+
},
15+
{
16+
code: '<SomeComponent @valuelessByAccident{{this.canBeAModifier}} />',
17+
output: null,
18+
errors: [{ messageId: 'valueless' }],
19+
},
20+
{
21+
code: '<SomeComponent @valuelessByAccident{{@canBeAModifier}} />',
22+
output: null,
23+
errors: [{ messageId: 'valueless' }],
24+
},
25+
];
26+
27+
function wrapTemplate(entry) {
28+
if (typeof entry === 'string') {
29+
return `<template>${entry}</template>`;
30+
}
31+
32+
return {
33+
...entry,
34+
code: `<template>${entry.code}</template>`,
35+
output: entry.output ? `<template>${entry.output}</template>` : entry.output,
36+
};
37+
}
38+
39+
const gjsRuleTester = new RuleTester({
540
parser: require.resolve('ember-eslint-parser'),
641
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
742
});
8-
ruleTester.run('template-no-valueless-arguments', rule, {
9-
valid: [
10-
'<template><Foo @bar={{true}} /></template>',
11-
'<template><SomeComponent @emptyString="" data-test-some-component /></template>',
12-
'<template><button type="submit" disabled {{on "click" this.submit}}></button></template>',
13-
],
14-
invalid: [
15-
{
16-
code: '<template><Foo @bar /></template>',
17-
output: null,
18-
errors: [{ messageId: 'valueless' }],
19-
},
2043

21-
{
22-
code: '<template><SomeComponent @valueless /></template>',
23-
output: null,
24-
errors: [{ messageId: 'valueless' }],
25-
},
26-
{
27-
code: '<template><SomeComponent @valuelessByAccident{{this.canBeAModifier}} /></template>',
28-
output: null,
29-
errors: [{ messageId: 'valueless' }],
30-
},
31-
{
32-
code: '<template><SomeComponent @valuelessByAccident{{@canBeAModifier}} /></template>',
33-
output: null,
34-
errors: [{ messageId: 'valueless' }],
35-
},
36-
],
44+
gjsRuleTester.run('template-no-valueless-arguments', rule, {
45+
valid: validHbs.map(wrapTemplate),
46+
invalid: invalidHbs.map(wrapTemplate),
3747
});
3848

3949
const hbsRuleTester = new RuleTester({
@@ -45,25 +55,6 @@ const hbsRuleTester = new RuleTester({
4555
});
4656

4757
hbsRuleTester.run('template-no-valueless-arguments', rule, {
48-
valid: [
49-
'<SomeComponent @emptyString="" data-test-some-component />',
50-
'<button type="submit" disabled {{on "click" this.submit}}></button>',
51-
],
52-
invalid: [
53-
{
54-
code: '<SomeComponent @valueless />',
55-
output: null,
56-
errors: [{ message: 'Named arguments should have an explicitly assigned value.' }],
57-
},
58-
{
59-
code: '<SomeComponent @valuelessByAccident{{this.canBeAModifier}} />',
60-
output: null,
61-
errors: [{ message: 'Named arguments should have an explicitly assigned value.' }],
62-
},
63-
{
64-
code: '<SomeComponent @valuelessByAccident{{@canBeAModifier}} />',
65-
output: null,
66-
errors: [{ message: 'Named arguments should have an explicitly assigned value.' }],
67-
},
68-
],
58+
valid: validHbs,
59+
invalid: invalidHbs,
6960
});

0 commit comments

Comments
 (0)