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-index-component-invocation.js
More file actions
104 lines (95 loc) · 3.52 KB
/
template-no-index-component-invocation.js
File metadata and controls
104 lines (95 loc) · 3.52 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
/* eslint-disable complexity, eslint-plugin/prefer-placeholders, unicorn/explicit-length-check */
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow index component invocations',
category: 'Best Practices',
recommended: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-index-component-invocation.md',
templateMode: 'both',
},
fixable: null,
schema: [],
messages: {},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-index-component-invocation.js',
docs: 'docs/rule/no-index-component-invocation.md',
tests: 'test/unit/rules/no-index-component-invocation-test.js',
},
},
create(context) {
function lintIndexUsage(node) {
// Handle angle bracket components: <Foo::Index />
if (node.type === 'GlimmerElementNode') {
if (node.tag && node.tag.endsWith('::Index')) {
const invocation = `<${node.tag}`;
const replacement = `<${node.tag.replace('::Index', '')}`;
context.report({
node,
message: `Replace \`${invocation} ...\` to \`${replacement} ...\``,
});
}
return;
}
// Handle mustache and block statements: {{foo/index}} or {{#foo/index}}
if (node.type === 'GlimmerMustacheStatement' || node.type === 'GlimmerBlockStatement') {
if (
node.path &&
node.path.type === 'GlimmerPathExpression' &&
node.path.original &&
node.path.original.endsWith('/index')
) {
const invocationPrefix = node.type === 'GlimmerBlockStatement' ? '{{#' : '{{';
const invocation = `${invocationPrefix}${node.path.original}`;
const replacement = `${invocationPrefix}${node.path.original.replace('/index', '')}`;
context.report({
node: node.path,
message: `Replace \`${invocation} ...\` to \`${replacement} ...\``,
});
return;
}
}
// Handle component helper: {{component "foo/index"}} or (component "foo/index")
if (
node.type === 'GlimmerMustacheStatement' ||
node.type === 'GlimmerBlockStatement' ||
node.type === 'GlimmerSubExpression'
) {
const prefix =
node.type === 'GlimmerMustacheStatement'
? '{{'
: node.type === 'GlimmerBlockStatement'
? '{{#'
: '(';
if (
node.path &&
node.path.type === 'GlimmerPathExpression' &&
node.path.original === 'component' &&
node.params &&
node.params.length > 0 &&
node.params[0].type === 'GlimmerStringLiteral'
) {
const componentName = node.params[0].value;
if (componentName.endsWith('/index')) {
const invocation = `${prefix}component "${componentName}"`;
const replacement = `${prefix}component "${componentName.replace('/index', '')}"`;
context.report({
node: node.params[0],
message: `Replace \`${invocation} ...\` to \`${replacement} ...\``,
});
}
}
}
}
return {
GlimmerElementNode: lintIndexUsage,
GlimmerMustacheStatement: lintIndexUsage,
GlimmerBlockStatement: lintIndexUsage,
GlimmerSubExpression: lintIndexUsage,
};
},
};
/* eslint-enable complexity, eslint-plugin/prefer-placeholders, unicorn/explicit-length-check */