forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-simple-unless.js
More file actions
155 lines (141 loc) · 4.9 KB
/
template-simple-unless.js
File metadata and controls
155 lines (141 loc) · 4.9 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function isUnless(node) {
return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless';
}
function isIf(node) {
return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'if';
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require simple conditions in unless blocks',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-simple-unless.md',
templateMode: 'both',
},
schema: [
{
type: 'object',
properties: {
allowlist: { type: 'array', items: { type: 'string' } },
denylist: { type: 'array', items: { type: 'string' } },
maxHelpers: { type: 'integer' },
},
additionalProperties: false,
},
],
messages: {
followingElseBlock: 'Using an `else` block with `unless` should be avoided.',
asElseUnlessBlock: 'Using an `else unless` block should be avoided.',
withHelper: '{{message}}',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/simple-unless.js',
docs: 'docs/rule/simple-unless.md',
tests: 'test/unit/rules/simple-unless-test.js',
},
},
create(context) {
const options = context.options[0] || {};
const allowlist = options.allowlist || [];
const denylist = options.denylist || [];
const maxHelpers = options.maxHelpers === undefined ? 1 : options.maxHelpers;
const sourceCode = context.sourceCode;
function isElseUnlessBlock(node) {
if (!node) {
return false;
}
if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless') {
const text = sourceCode.getText(node);
return text.startsWith('{{else ');
}
return false;
}
function checkWithHelper(node) {
let helperCount = 0;
let nextParams = node.params || [];
do {
const currentParams = nextParams;
nextParams = [];
for (const param of currentParams) {
if (param.type === 'GlimmerSubExpression') {
helperCount++;
const helperName = param.path?.original || '';
if (maxHelpers > -1 && helperCount > maxHelpers) {
context.report({
node: param,
messageId: 'withHelper',
data: {
message: `Using {{unless}} in combination with other helpers should be avoided. MaxHelpers: ${maxHelpers}`,
},
});
return;
}
if (allowlist.length > 0 && !allowlist.includes(helperName)) {
context.report({
node: param,
messageId: 'withHelper',
data: {
message: `Using {{unless}} in combination with other helpers should be avoided. Allowed helper${allowlist.length > 1 ? 's' : ''}: ${allowlist}`,
},
});
return;
}
if (denylist.length > 0 && denylist.includes(helperName)) {
context.report({
node: param,
messageId: 'withHelper',
data: {
message: `Using {{unless}} in combination with other helpers should be avoided. Restricted helper${denylist.length > 1 ? 's' : ''}: ${denylist}`,
},
});
return;
}
if (param.params) {
nextParams.push(...param.params);
}
}
}
} while (nextParams.some((p) => p.type === 'GlimmerSubExpression'));
}
return {
GlimmerMustacheStatement(node) {
if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless') {
if (node.params?.[0]?.path) {
checkWithHelper(node);
}
}
},
GlimmerBlockStatement(node) {
const nodeInverse = node.inverse;
if (nodeInverse && nodeInverse.body?.length > 0) {
if (isUnless(node)) {
// Check for {{#unless}}...{{else if}}
if (nodeInverse.body[0] && isIf(nodeInverse.body[0])) {
context.report({
node: node.program || node,
messageId: 'followingElseBlock',
});
} else {
// {{#unless}}...{{else}}
context.report({
node: node.program || node,
messageId: 'followingElseBlock',
});
}
} else if (isElseUnlessBlock(nodeInverse.body[0])) {
// {{#if}}...{{else unless}}
context.report({
node: nodeInverse.body[0],
messageId: 'asElseUnlessBlock',
});
}
} else if (isUnless(node) && node.params?.[0]?.path) {
checkWithHelper(node);
}
},
};
},
};