Skip to content

Commit 2f1370f

Browse files
committed
Sync with template-lint
1 parent f90cc11 commit 2f1370f

3 files changed

Lines changed: 53 additions & 79 deletions

File tree

docs/rules/template-no-unnecessary-curly-strings.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66

77
Strings need not be wrapped in curly braces (mustache expressions).
88

9-
## Rule Details
10-
11-
This rule detects unnecessary mustache expressions wrapping simple string literals. It checks both **attribute values** and **text content**.
12-
139
## Examples
1410

1511
This rule **forbids** the following:
1612

1713
```gjs
18-
<template><FooBar class={{'btn'}} /></template>
14+
<template><FooBar class={{"btn"}} /></template>
1915
```
2016

2117
```gjs
@@ -25,11 +21,11 @@ This rule **forbids** the following:
2521
This rule **allows** the following:
2622

2723
```gjs
28-
<template><FooBar class='btn' /></template>
24+
<template><FooBar class="btn" /></template>
2925
```
3026

3127
```gjs
32-
<template><FooBar class='btn'>Hello</FooBar></template>
28+
<template><FooBar class="btn">Hello</FooBar></template>
3329
```
3430

3531
## References

lib/rules/template-no-unnecessary-curly-strings.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ module.exports = {
55
docs: {
66
description: 'disallow unnecessary curly braces in string interpolations',
77
category: 'Style',
8-
recommendedGjs: false,
9-
recommendedGts: false,
108
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unnecessary-curly-strings.md',
119
templateMode: 'both',
1210
},
1311
fixable: 'code',
1412
schema: [],
15-
messages: { unnecessary: 'Unnecessary curly braces in string.' },
13+
messages: { unnecessary: 'Unnecessary curly braces around string' },
1614
originallyFrom: {
1715
name: 'ember-template-lint',
1816
rule: 'lib/rules/no-unnecessary-curly-strings.js',
Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,57 @@
11
const rule = require('../../../lib/rules/template-no-unnecessary-curly-strings');
22
const RuleTester = require('eslint').RuleTester;
33

4-
const ruleTester = new RuleTester({
4+
const validHbs = [
5+
'<FooBar class="btn" />',
6+
'{{foo}}',
7+
'{{(foo)}}',
8+
'{{this.calculate 1 2 op="add"}}',
9+
'{{get address part}}',
10+
'foo',
11+
'"foo"',
12+
'<FooBar value=12345 />',
13+
'<FooBar value=null />',
14+
'<FooBar value=true />',
15+
'<FooBar value=undefined />',
16+
'<FooBar value={{12345}} />',
17+
'<FooBar value={{null}} />',
18+
'<FooBar value={{true}} />',
19+
'<FooBar value={{undefined}} />',
20+
];
21+
22+
const invalidHbs = [
23+
{
24+
code: '<FooBar class={{"btn"}} @fooArg={{\'barbaz\'}} />',
25+
output: '<FooBar class="btn" @fooArg=\'barbaz\' />',
26+
errors: [{ messageId: 'unnecessary' }, { messageId: 'unnecessary' }],
27+
},
28+
{
29+
code: '<FooBar class="btn">{{"Foo"}}</FooBar>',
30+
output: '<FooBar class="btn">Foo</FooBar>',
31+
errors: [{ messageId: 'unnecessary' }],
32+
},
33+
];
34+
35+
function wrapTemplate(entry) {
36+
if (typeof entry === 'string') {
37+
return `<template>${entry}</template>`;
38+
}
39+
40+
return {
41+
...entry,
42+
code: `<template>${entry.code}</template>`,
43+
output: entry.output ? `<template>${entry.output}</template>` : entry.output,
44+
};
45+
}
46+
47+
const gjsRuleTester = new RuleTester({
548
parser: require.resolve('ember-eslint-parser'),
649
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
750
});
8-
ruleTester.run('template-no-unnecessary-curly-strings', rule, {
9-
valid: [
10-
'<template><div class="foo"></div></template>',
11-
'<template><FooBar class="btn" /></template>',
12-
'<template>{{foo}}</template>',
13-
'<template>{{(foo)}}</template>',
14-
'<template>{{this.calculate 1 2 op="add"}}</template>',
15-
'<template>{{get address part}}</template>',
16-
'<template>foo</template>',
17-
'<template>"foo"</template>',
18-
'<template><FooBar value=12345 /></template>',
19-
'<template><FooBar value=null /></template>',
20-
'<template><FooBar value=true /></template>',
21-
'<template><FooBar value=undefined /></template>',
22-
'<template><FooBar value={{12345}} /></template>',
23-
'<template><FooBar value={{null}} /></template>',
24-
'<template><FooBar value={{true}} /></template>',
25-
'<template><FooBar value={{undefined}} /></template>',
26-
],
27-
invalid: [
28-
{
29-
code: '<template><div class={{"foo"}}></div></template>',
30-
output: '<template><div class="foo"></div></template>',
31-
errors: [{ messageId: 'unnecessary' }],
32-
},
3351

34-
{
35-
code: '<template><FooBar class={{"btn"}} @fooArg={{\'barbaz\'}} /></template>',
36-
output: '<template><FooBar class="btn" @fooArg=\'barbaz\' /></template>',
37-
errors: [{ messageId: 'unnecessary' }, { messageId: 'unnecessary' }],
38-
},
39-
{
40-
code: '<template><FooBar class="btn">{{"Foo"}}</FooBar></template>',
41-
output: '<template><FooBar class="btn">Foo</FooBar></template>',
42-
errors: [{ messageId: 'unnecessary' }],
43-
},
44-
],
52+
gjsRuleTester.run('template-no-unnecessary-curly-strings', rule, {
53+
valid: validHbs.map(wrapTemplate),
54+
invalid: invalidHbs.map(wrapTemplate),
4555
});
4656

4757
const hbsRuleTester = new RuleTester({
@@ -53,36 +63,6 @@ const hbsRuleTester = new RuleTester({
5363
});
5464

5565
hbsRuleTester.run('template-no-unnecessary-curly-strings', rule, {
56-
valid: [
57-
'<FooBar class="btn" />',
58-
'{{foo}}',
59-
'{{(foo)}}',
60-
'{{this.calculate 1 2 op="add"}}',
61-
'{{get address part}}',
62-
'foo',
63-
'"foo"',
64-
'<FooBar value=12345 />',
65-
'<FooBar value=null />',
66-
'<FooBar value=true />',
67-
'<FooBar value=undefined />',
68-
'<FooBar value={{12345}} />',
69-
'<FooBar value={{null}} />',
70-
'<FooBar value={{true}} />',
71-
'<FooBar value={{undefined}} />',
72-
],
73-
invalid: [
74-
{
75-
code: '<FooBar class={{"btn"}} @fooArg={{\'barbaz\'}} />',
76-
output: '<FooBar class="btn" @fooArg=\'barbaz\' />',
77-
errors: [
78-
{ message: 'Unnecessary curly braces in string.' },
79-
{ message: 'Unnecessary curly braces in string.' },
80-
],
81-
},
82-
{
83-
code: '<FooBar class="btn">{{"Foo"}}</FooBar>',
84-
output: '<FooBar class="btn">Foo</FooBar>',
85-
errors: [{ message: 'Unnecessary curly braces in string.' }],
86-
},
87-
],
66+
valid: validHbs,
67+
invalid: invalidHbs,
8868
});

0 commit comments

Comments
 (0)