forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-unnecessary-component-helper.js
More file actions
131 lines (122 loc) · 5.31 KB
/
template-no-unnecessary-component-helper.js
File metadata and controls
131 lines (122 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const eslint = require('eslint');
const rule = require('../../../lib/rules/template-no-unnecessary-component-helper');
const { RuleTester } = eslint;
const validHbs = [
'{{component SOME_COMPONENT_NAME}}',
'{{component SOME_COMPONENT_NAME SOME_ARG}}',
'{{component SOME_COMPONENT_NAME "Hello World"}}',
'{{my-component}}',
'{{my-component "Hello world"}}',
'{{my-component "Hello world" 123}}',
'{{#component SOME_COMPONENT_NAME}}{{/component}}',
'{{#component SOME_COMPONENT_NAME SOME_ARG}}{{/component}}',
'{{#component SOME_COMPONENT_NAME "Hello World"}}{{/component}}',
'{{#my-component}}{{/my-component}}',
'{{#my-component "Hello world"}}{{/my-component}}',
'{{#my-component "Hello world" 123}}{{/my-component}}',
'(component SOME_COMPONENT_NAME)',
'(component "my-component")',
'<Foo @bar={{component SOME_COMPONENT_NAME}} />',
'<Foo @bar={{component "my-component"}} />',
'<Foo @bar={{component SOME_COMPONENT_NAME}}></Foo>',
'<Foo @bar={{component "my-component"}}></Foo>',
'<Foo @arg="foo" />',
'<Foo class="foo" />',
'<Foo data-test-bar="foo" />',
'<Foo @arg={{if this.user.isAdmin "admin"}} />',
'<Foo @arg={{if this.user.isAdmin (component "my-component")}} />',
"{{component 'addon-name@component-name'}}",
"{{#component 'addon-name@component-name'}}{{/component}}",
];
const invalidHbs = [
{
code: '{{component "my-component-name" foo=123 bar=456}}',
output: '{{my-component-name foo=123 bar=456}}',
errors: [{ message: 'Invoke component directly instead of using `component` helper' }],
},
{
code: '{{#component "my-component-name" foo=123 bar=456}}{{/component}}',
output: '{{#my-component-name foo=123 bar=456}}{{/my-component-name}}',
errors: [{ message: 'Invoke component directly instead of using `component` helper' }],
},
{
code: '<Foo @arg={{component "allowed-component"}}>{{component "forbidden-component"}}</Foo>',
output: '<Foo @arg={{component "allowed-component"}}>{{forbidden-component}}</Foo>',
errors: [{ message: 'Invoke component directly instead of using `component` helper' }],
},
];
const validGjs = [
'<template>{{component SOME_COMPONENT_NAME}}</template>',
'<template>{{component SOME_COMPONENT_NAME SOME_ARG}}</template>',
'<template>{{component SOME_COMPONENT_NAME "Hello World"}}</template>',
'<template>{{my-component}}</template>',
'<template>{{my-component "Hello world"}}</template>',
'<template>{{my-component "Hello world" 123}}</template>',
'<template>{{#component SOME_COMPONENT_NAME}}{{/component}}</template>',
'<template>{{#component SOME_COMPONENT_NAME SOME_ARG}}{{/component}}</template>',
'<template>{{#component SOME_COMPONENT_NAME "Hello World"}}{{/component}}</template>',
'<template>{{#my-component}}{{/my-component}}</template>',
'<template>{{#my-component "Hello world"}}{{/my-component}}</template>',
'<template>{{#my-component "Hello world" 123}}{{/my-component}}</template>',
'<template><Foo @bar={{component SOME_COMPONENT_NAME}} /></template>',
'<template><Foo @bar={{component "my-component"}} /></template>',
'<template><Foo @bar={{component SOME_COMPONENT_NAME}}></Foo></template>',
'<template><Foo @bar={{component "my-component"}}></Foo></template>',
'<template><Foo @arg="foo" /></template>',
'<template><Foo class="foo" /></template>',
'<template><Foo data-test-bar="foo" /></template>',
'<template><Foo @arg={{if this.user.isAdmin "admin"}} /></template>',
'<template><Foo @arg={{if this.user.isAdmin (component "my-component")}} /></template>',
"<template>{{component 'addon-name@component-name'}}</template>",
"<template>{{#component 'addon-name@component-name'}}{{/component}}</template>",
];
function wrapTemplate(entry) {
return {
...entry,
code: `<template>${entry.code}</template>`,
output: entry.output ? `<template>${entry.output}</template>` : entry.output,
errors: entry.errors.map(() => ({ messageId: 'noUnnecessaryComponent' })),
};
}
const gjsRuleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});
gjsRuleTester.run('template-no-unnecessary-component-helper', rule, {
valid: validGjs,
invalid: [
...invalidHbs.map(wrapTemplate),
// GJS/GTS: autofix is skipped when the component name isn't a valid JS
// identifier. The error is still reported so the user sees the issue.
{
filename: 'test.gjs',
code: '<template>{{component "my-component-name" foo=123}}</template>',
output: null,
errors: [{ messageId: 'noUnnecessaryComponent' }],
},
{
filename: 'test.gts',
code: '<template>{{#component "my-component-name"}}content{{/component}}</template>',
output: null,
errors: [{ messageId: 'noUnnecessaryComponent' }],
},
// GJS/GTS: valid JS identifier → autofix still applies
{
filename: 'test.gjs',
code: '<template>{{component "myComponent" foo=123}}</template>',
output: '<template>{{myComponent foo=123}}</template>',
errors: [{ messageId: 'noUnnecessaryComponent' }],
},
],
});
const hbsRuleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser/hbs'),
parserOptions: {
ecmaVersion: 2022,
sourceType: 'module',
},
});
hbsRuleTester.run('template-no-unnecessary-component-helper', rule, {
valid: validHbs,
invalid: invalidHbs,
});