Skip to content

Commit 2faed4e

Browse files
authored
Merge pull request #78 from ember-template-lint/add-fixer
Fix templates with Prettier
2 parents 4464576 + f3bb10a commit 2faed4e

4 files changed

Lines changed: 192 additions & 142 deletions

File tree

lib/rules/prettier.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;
1111

1212
let prettier;
1313

14-
const Rule = require("ember-template-lint").Rule;
14+
const { Rule, recast } = require("ember-template-lint");
1515

1616
function isFile(loc) {
1717
return loc.start && loc.start.line === 1 && loc.start.column === 0;
@@ -99,6 +99,11 @@ module.exports = class Prettier extends Rule {
9999
}
100100

101101
if (source !== prettierSource) {
102+
if (this.mode === "fix") {
103+
node.body = recast.parse(prettierSource).body;
104+
return;
105+
}
106+
102107
const differences = generateDifferences(source, prettierSource);
103108

104109
differences.forEach(difference => {
@@ -126,7 +131,7 @@ module.exports = class Prettier extends Rule {
126131
break;
127132
}
128133

129-
this.log({ message, line, column, source });
134+
this.log({ message, line, column, source, isFixable: true });
130135
});
131136
}
132137
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
"license": "Apache-2.0",
2929
"private": false,
3030
"peerDependencies": {
31-
"ember-template-lint": "^2.1.0",
31+
"ember-template-lint": "^2.10.0",
3232
"prettier": "^1.18.2"
3333
},
3434
"devDependencies": {
35-
"ember-template-lint": "^2.1.0",
35+
"ember-template-lint": "^2.10.0",
3636
"eslint": "^7.1.0",
3737
"eslint-config-prettier": "^6.0.0",
3838
"eslint-plugin-node": "^11.0.0",

test/unit/rules/lint-prettier-test.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,47 @@ test
2929
{
3030
config: true,
3131
template: "{{#my-component}}{{/my-component}}\n",
32+
fixedTemplate: "{{#my-component}}{{/my-component}}",
3233
result: {
3334
moduleId: "layout",
3435
message: "Delete `⏎`",
3536
line: 1,
3637
column: 34,
37-
source: "{{#my-component}}{{/my-component}}\n"
38+
source: "{{#my-component}}{{/my-component}}\n",
39+
isFixable: true
40+
}
41+
},
42+
{
43+
config: true,
44+
template: `<div data-foo
45+
data-bar="lol"
46+
some-other-thing={{haha-morethaneightychars}}>
47+
</div>`,
48+
fixedTemplate: `<div data-foo data-bar="lol" some-other-thing={{haha-morethaneightychars}}>
49+
</div>`,
50+
result: {
51+
moduleId: "layout",
52+
message: 'Replace `⏎·data-bar="lol"⏎·····` with ` data-bar="lol"`',
53+
line: 1,
54+
column: 13,
55+
source: `<div data-foo
56+
data-bar="lol"
57+
some-other-thing={{haha-morethaneightychars}}>
58+
</div>`,
59+
isFixable: true
3860
}
3961
},
4062
{
4163
config: true,
4264
template: "test\n",
65+
fixedTemplate: "test",
4366
result: {
4467
moduleId: "layout",
4568
message: "Delete `⏎`",
4669
line: 1,
4770
column: 4,
48-
source: "test\n"
71+
source: "test\n",
72+
isFixable: true
4973
}
5074
},
5175
{
@@ -54,28 +78,36 @@ test
5478
5579
test
5680
81+
{{/my-component}}`,
82+
fixedTemplate: `{{#my-component}}
83+
test
5784
{{/my-component}}`,
5885
result: {
5986
moduleId: "layout",
6087
message: "Replace `⏎test⏎` with ` test`",
6188
line: 1,
6289
column: 18,
63-
source: "{{#my-component}}\n\ntest\n\n{{/my-component}}"
90+
source: "{{#my-component}}\n\ntest\n\n{{/my-component}}",
91+
isFixable: true
6492
}
6593
},
6694
{
6795
config: true,
6896
template: `{{#my-component class="class1 class2"}}
6997
test
7098
99+
{{/my-component}}`,
100+
fixedTemplate: `{{#my-component class="class1 class2"}}
101+
test
71102
{{/my-component}}`,
72103
result: {
73104
moduleId: "layout",
74105
message: "Delete `⏎`",
75106
line: 2,
76107
column: 7,
77108
source:
78-
'{{#my-component class="class1 class2"}}\n test\n\n{{/my-component}}'
109+
'{{#my-component class="class1 class2"}}\n test\n\n{{/my-component}}',
110+
isFixable: true
79111
}
80112
}
81113
]

0 commit comments

Comments
 (0)