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
119 lines (103 loc) · 3.31 KB
/
template-no-unnecessary-component-helper.js
File metadata and controls
119 lines (103 loc) · 3.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
function isComponentWithStringLiteral(node) {
return (
node.path &&
node.path.type === 'GlimmerPathExpression' &&
node.path.original === 'component' &&
node.params &&
node.params.length > 0 &&
node.params[0].type === 'GlimmerStringLiteral' &&
!(node.params[0].value || '').includes('@')
);
}
function getComponentInvocationText(sourceCode, node, componentName) {
const parts = [];
for (const param of node.params.slice(1)) {
parts.push(sourceCode.getText(param));
}
for (const pair of node.hash?.pairs || []) {
parts.push(sourceCode.getText(pair));
}
return [componentName, ...parts].join(' ');
}
function getOpenInvocationEnd(node) {
if (node.hash?.pairs?.length) {
return node.hash.range[1];
}
const lastParam = node.params.at(-1);
return lastParam ? lastParam.range[1] : node.path.range[1];
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unnecessary component helper',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unnecessary-component-helper.md',
templateMode: 'loose',
},
fixable: 'code',
schema: [],
messages: {
noUnnecessaryComponent: 'Invoke component directly instead of using `component` helper',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-unnecessary-component-helper.js',
docs: 'docs/rule/no-unnecessary-component-helper.md',
tests: 'test/unit/rules/no-unnecessary-component-helper-test.js',
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();
let inAttribute = 0;
return {
GlimmerAttrNode() {
inAttribute++;
},
'GlimmerAttrNode:exit'() {
inAttribute--;
},
GlimmerMustacheStatement(node) {
if (inAttribute > 0) {
return;
}
if (!isComponentWithStringLiteral(node)) {
return;
}
const componentName = node.params[0].value || node.params[0].original;
const invocation = getComponentInvocationText(sourceCode, node, componentName);
context.report({
node,
messageId: 'noUnnecessaryComponent',
fix(fixer) {
return fixer.replaceText(node, `{{${invocation}}}`);
},
});
},
GlimmerBlockStatement(node) {
if (inAttribute > 0) {
return;
}
if (!isComponentWithStringLiteral(node)) {
return;
}
const componentName = node.params[0].value || node.params[0].original;
const invocation = getComponentInvocationText(sourceCode, node, componentName);
context.report({
node,
messageId: 'noUnnecessaryComponent',
fix(fixer) {
const openInvocationEnd = getOpenInvocationEnd(node);
const closingPathEnd = node.range[1] - 2;
const closingPathStart = closingPathEnd - node.path.original.length;
return [
fixer.replaceTextRange([node.path.range[0], openInvocationEnd], invocation),
fixer.replaceTextRange([closingPathStart, closingPathEnd], componentName),
];
},
});
},
};
},
};