Skip to content

Commit

Permalink
Merge pull request #2895 from lin-ll/lin-ll/no-unnecessary-curly-stri…
Browse files Browse the repository at this point in the history
…ngs-fixer

Add autofixer to `no-unnecessary-curly-strings` rule
  • Loading branch information
lin-ll committed May 25, 2023
2 parents 0704a6d + c107718 commit 1692795
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Each rule has emojis denoting:
| [no-unnecessary-component-helper](./docs/rule/no-unnecessary-component-helper.md) || | | 🔧 |
| [no-unnecessary-concat](./docs/rule/no-unnecessary-concat.md) | | 💅 | | 🔧 |
| [no-unnecessary-curly-parens](./docs/rule/no-unnecessary-curly-parens.md) | | | | 🔧 |
| [no-unnecessary-curly-strings](./docs/rule/no-unnecessary-curly-strings.md) | | | | |
| [no-unnecessary-curly-strings](./docs/rule/no-unnecessary-curly-strings.md) | | | | 🔧 |
| [no-unsupported-role-attributes](./docs/rule/no-unsupported-role-attributes.md) || | ⌨️ | 🔧 |
| [no-unused-block-params](./docs/rule/no-unused-block-params.md) || | | |
| [no-valueless-arguments](./docs/rule/no-valueless-arguments.md) || | | |
Expand Down
2 changes: 2 additions & 0 deletions docs/rule/no-unnecessary-curly-strings.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# no-unnecessary-curly-strings

🔧 The `--fix` option on the command line can automatically fix some of the problems reported by this rule.

Strings need not be wrapped in the curly braces (mustache expressions).

## Examples
Expand Down
21 changes: 16 additions & 5 deletions lib/rules/no-unnecessary-curly-strings.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import Rule from './_base.js';
import { builders as b } from 'ember-template-recast';
import replaceNode from '../helpers/replace-node.js';

export default class NoUnnecessaryCurlyStrings extends Rule {
visitor() {
return {
MustacheStatement(node) {
MustacheStatement(node, { parentNode, parentKey }) {
if (node.path.type === 'StringLiteral') {
this.log({
node,
message: 'Unnecessary curly braces around string',
});
if (this.mode === 'fix') {
if (parentNode.type === 'AttrNode') {
parentNode.quoteType = node.path.quoteType;
}
const newNode = b.text(node.path.original);
replaceNode(node, parentNode, parentKey, newNode);
} else {
this.log({
node,
message: 'Unnecessary curly braces around string',
isFixable: true,
});
}
}
},
};
Expand Down
18 changes: 17 additions & 1 deletion test/unit/rules/no-unnecessary-curly-strings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ generateRuleTests({

bad: [
{
template: '<FooBar class={{"btn"}} />',
template: '<FooBar class={{"btn"}} @fooArg={{\'barbaz\'}} />',
fixedTemplate: '<FooBar class="btn" @fooArg=\'barbaz\' />',

verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
Expand All @@ -35,18 +36,32 @@ generateRuleTests({
"endColumn": 23,
"endLine": 1,
"filePath": "layout.hbs",
"isFixable": true,
"line": 1,
"message": "Unnecessary curly braces around string",
"rule": "no-unnecessary-curly-strings",
"severity": 2,
"source": "{{\\"btn\\"}}",
},
{
"column": 32,
"endColumn": 44,
"endLine": 1,
"filePath": "layout.hbs",
"isFixable": true,
"line": 1,
"message": "Unnecessary curly braces around string",
"rule": "no-unnecessary-curly-strings",
"severity": 2,
"source": "{{'barbaz'}}",
},
]
`);
},
},
{
template: '<FooBar class="btn">{{"Foo"}}</FooBar>',
fixedTemplate: '<FooBar class="btn">Foo</FooBar>',

verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
Expand All @@ -56,6 +71,7 @@ generateRuleTests({
"endColumn": 29,
"endLine": 1,
"filePath": "layout.hbs",
"isFixable": true,
"line": 1,
"message": "Unnecessary curly braces around string",
"rule": "no-unnecessary-curly-strings",
Expand Down

0 comments on commit 1692795

Please sign in to comment.