Skip to content

Commit e0654c0

Browse files
committed
Sync with template-lint
1 parent b23f8f7 commit e0654c0

2 files changed

Lines changed: 46 additions & 50 deletions

File tree

docs/rules/template-no-unnecessary-concat.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ This rule **forbids** the following:
1515
```gjs
1616
<template>
1717
<span class="{{if errors.length 'text-danger' 'text-grey'}}">
18-
1918
<img src="{{customSrc}}" alt="{{customAlt}}">
20-
2119
<label for="{{concat elementId "-date"}}">
2220
</template>
2321
```
@@ -27,9 +25,7 @@ This rule **allows** the following:
2725
```gjs
2826
<template>
2927
<span class={{if errors.length 'text-danger' 'text-grey'}}>
30-
3128
<img src={{customSrc}} alt={{customAlt}}>
32-
3329
<label for={{concat elementId "-date"}}>
3430
</template>
3531
```
Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
const rule = require('../../../lib/rules/template-no-unnecessary-concat');
22
const RuleTester = require('eslint').RuleTester;
33

4-
const ruleTester = new RuleTester({
4+
const validHbs = [
5+
'<div class={{clazz}}></div>',
6+
'<div class="first {{second}}"></div>',
7+
'"{{foo}}"',
8+
];
9+
10+
const invalidHbs = [
11+
{
12+
code: '<div class="{{clazz}}"></div>',
13+
output: '<div class={{clazz}}></div>',
14+
errors: [
15+
{ message: 'Unnecessary string concatenation. Use {{clazz}} instead of "{{clazz}}".' },
16+
],
17+
},
18+
{
19+
code: '<img src="{{url}}" alt="{{t "alternate-text"}}">',
20+
output: '<img src={{url}} alt={{t "alternate-text"}}>',
21+
errors: [
22+
{ message: 'Unnecessary string concatenation. Use {{url}} instead of "{{url}}".' },
23+
{
24+
message:
25+
'Unnecessary string concatenation. Use {{t "alternate-text"}} instead of "{{t "alternate-text"}}".',
26+
},
27+
],
28+
},
29+
];
30+
31+
function wrapTemplate(entry) {
32+
if (typeof entry === 'string') {
33+
return `<template>${entry}</template>`;
34+
}
35+
36+
return {
37+
...entry,
38+
code: `<template>${entry.code}</template>`,
39+
output: entry.output ? `<template>${entry.output}</template>` : entry.output,
40+
errors: entry.errors.map(() => ({ messageId: 'unnecessary' })),
41+
};
42+
}
43+
44+
const gjsRuleTester = new RuleTester({
545
parser: require.resolve('ember-eslint-parser'),
646
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
747
});
8-
ruleTester.run('template-no-unnecessary-concat', rule, {
9-
valid: [
10-
'<template><div class="foo {{bar}}"></div></template>',
11-
'<template><div class={{clazz}}></div></template>',
12-
'<template><div class="first {{second}}"></div></template>',
13-
'<template>"{{foo}}"</template>',
14-
],
15-
invalid: [
16-
{
17-
code: '<template><div class="{{foo}}"></div></template>',
18-
output: '<template><div class={{foo}}></div></template>',
19-
errors: [{ messageId: 'unnecessary' }],
20-
},
2148

22-
{
23-
code: '<template><div class="{{clazz}}"></div></template>',
24-
output: '<template><div class={{clazz}}></div></template>',
25-
errors: [{ messageId: 'unnecessary' }],
26-
},
27-
{
28-
code: '<template><img src="{{url}}" alt="{{t "alternate-text"}}"></template>',
29-
output: '<template><img src={{url}} alt={{t "alternate-text"}}></template>',
30-
errors: [{ messageId: 'unnecessary' }, { messageId: 'unnecessary' }],
31-
},
32-
],
49+
gjsRuleTester.run('template-no-unnecessary-concat', rule, {
50+
valid: validHbs.map(wrapTemplate),
51+
invalid: invalidHbs.map(wrapTemplate),
3352
});
3453

3554
const hbsRuleTester = new RuleTester({
@@ -41,25 +60,6 @@ const hbsRuleTester = new RuleTester({
4160
});
4261

4362
hbsRuleTester.run('template-no-unnecessary-concat', rule, {
44-
valid: ['<div class={{clazz}}></div>', '<div class="first {{second}}"></div>', '"{{foo}}"'],
45-
invalid: [
46-
{
47-
code: '<div class="{{clazz}}"></div>',
48-
output: '<div class={{clazz}}></div>',
49-
errors: [
50-
{ message: 'Unnecessary string concatenation. Use {{clazz}} instead of "{{clazz}}".' },
51-
],
52-
},
53-
{
54-
code: '<img src="{{url}}" alt="{{t "alternate-text"}}">',
55-
output: '<img src={{url}} alt={{t "alternate-text"}}>',
56-
errors: [
57-
{ message: 'Unnecessary string concatenation. Use {{url}} instead of "{{url}}".' },
58-
{
59-
message:
60-
'Unnecessary string concatenation. Use {{t "alternate-text"}} instead of "{{t "alternate-text"}}".',
61-
},
62-
],
63-
},
64-
],
63+
valid: validHbs,
64+
invalid: invalidHbs,
6565
});

0 commit comments

Comments
 (0)