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-strings.js
More file actions
57 lines (56 loc) · 1.87 KB
/
template-no-unnecessary-curly-strings.js
File metadata and controls
57 lines (56 loc) · 1.87 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
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unnecessary curly braces in string interpolations',
category: 'Style',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-unnecessary-curly-strings.md',
templateMode: 'both',
},
fixable: 'code',
schema: [],
messages: { unnecessary: 'Unnecessary curly braces around string' },
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-unnecessary-curly-strings.js',
docs: 'docs/rule/no-unnecessary-curly-strings.md',
tests: 'test/unit/rules/no-unnecessary-curly-strings-test.js',
},
},
create(context) {
const sourceCode = context.sourceCode;
return {
GlimmerAttrNode(node) {
if (
node.value?.type === 'GlimmerMustacheStatement' &&
node.value.path?.type === 'GlimmerStringLiteral'
) {
const strNode = node.value.path;
const strValue = strNode.value || strNode.original;
context.report({
node: node.value,
messageId: 'unnecessary',
fix(fixer) {
const strSource = sourceCode.getText(strNode);
const quoteChar = strSource[0] === "'" ? "'" : '"';
return fixer.replaceText(node.value, `${quoteChar}${strValue}${quoteChar}`);
},
});
}
},
GlimmerMustacheStatement(node) {
if (node.path?.type === 'GlimmerStringLiteral' && node.parent?.type !== 'GlimmerAttrNode') {
const strValue = node.path.value || node.path.original;
context.report({
node,
messageId: 'unnecessary',
fix(fixer) {
return fixer.replaceText(node, strValue);
},
});
}
},
};
},
};