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-curly-parens.js
More file actions
64 lines (59 loc) · 2.08 KB
/
template-no-unnecessary-curly-parens.js
File metadata and controls
64 lines (59 loc) · 2.08 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
function isFixableMustache(node) {
// Check if the mustache's "path" is actually a SubExpression with params or hash
// e.g., {{(helper arg)}} where the path is (helper arg)
return (
node.path?.type === 'GlimmerSubExpression' &&
((node.path.params && node.path.params.length > 0) ||
(node.path.hash?.pairs && node.path.hash.pairs.length > 0))
);
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unnecessary parentheses enclosing statements in curlies',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unnecessary-curly-parens.md',
templateMode: 'both',
},
fixable: 'code',
schema: [],
messages: {
noUnnecessaryCurlyParens: 'Unnecessary parentheses enclosing statement',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-unnecessary-curly-parens.js',
docs: 'docs/rule/no-unnecessary-curly-parens.md',
tests: 'test/unit/rules/no-unnecessary-curly-parens-test.js',
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();
return {
GlimmerMustacheStatement(node) {
if (isFixableMustache(node)) {
const subExpr = node.path;
context.report({
node,
messageId: 'noUnnecessaryCurlyParens',
fix(fixer) {
// Replace {{(helper params hash)}} with {{helper params hash}}
const helperName = subExpr.path?.original || '';
let replacement = `{{${helperName}`;
for (const param of subExpr.params || []) {
replacement += ` ${sourceCode.getText(param)}`;
}
for (const pair of subExpr.hash?.pairs || []) {
replacement += ` ${sourceCode.getText(pair)}`;
}
replacement += '}}';
return fixer.replaceText(node, replacement);
},
});
}
},
};
},
};