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
136 lines (119 loc) · 3.98 KB
/
template-no-unnecessary-component-helper.js
File metadata and controls
136 lines (119 loc) · 3.98 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
132
133
134
135
136
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;
const filename = context.filename;
const isStrictMode = filename.endsWith('.gjs') || filename.endsWith('.gts');
let inAttribute = 0;
// In strict mode, a kebab-case / slash component name cannot become
// a bare mustache invocation — the resulting identifier would not be
// a valid JS binding. Detect the rule's violation, but skip autofix
// when it would produce unparseable GJS/GTS output.
function canAutofix(componentName) {
if (!isStrictMode) {
return true;
}
return /^[$A-Z_a-z][\w$]*$/.test(componentName);
}
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);
const report = {
node,
messageId: 'noUnnecessaryComponent',
};
if (canAutofix(componentName)) {
report.fix = (fixer) => fixer.replaceText(node, `{{${invocation}}}`);
}
context.report(report);
},
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);
const report = {
node,
messageId: 'noUnnecessaryComponent',
};
if (canAutofix(componentName)) {
report.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),
];
};
}
context.report(report);
},
};
},
};