Skip to content

Commit 083514b

Browse files
committed
Add autofix for template-no-multiple-empty-lines
1 parent 131cd12 commit 083514b

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ rules in templates can be disabled with eslint directives with mustache or html
234234
| [template-no-invalid-meta](docs/rules/template-no-invalid-meta.md) | disallow invalid meta tags | | | |
235235
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
236236
| [template-no-model-argument-in-route-templates](docs/rules/template-no-model-argument-in-route-templates.md) | disallow @model argument in route templates | | 🔧 | |
237-
| [template-no-multiple-empty-lines](docs/rules/template-no-multiple-empty-lines.md) | disallow multiple consecutive empty lines in templates | | | |
237+
| [template-no-multiple-empty-lines](docs/rules/template-no-multiple-empty-lines.md) | disallow multiple consecutive empty lines in templates | | 🔧 | |
238238
| [template-no-mut-helper](docs/rules/template-no-mut-helper.md) | disallow usage of (mut) helper | | | |
239239
| [template-no-negated-comparison](docs/rules/template-no-negated-comparison.md) | disallow negated comparisons in templates | | | |
240240
| [template-no-negated-condition](docs/rules/template-no-negated-condition.md) | disallow negated conditions in if/unless | | | |

docs/rules/template-no-multiple-empty-lines.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ember/template-no-multiple-empty-lines
22

3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
35
<!-- end auto-generated rule header -->
46

57
Disallows multiple consecutive empty lines in templates.

lib/rules/template-no-multiple-empty-lines.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-multiple-empty-lines.md',
99
templateMode: 'both',
1010
},
11-
fixable: null,
11+
fixable: 'whitespace',
1212
schema: [
1313
{
1414
type: 'object',
@@ -40,6 +40,15 @@ module.exports = {
4040
Program(node) {
4141
const text = sourceCode.getText();
4242
const lines = text.split('\n');
43+
44+
// Precompute the character offset of the start of each line.
45+
const lineOffsets = [];
46+
let offset = 0;
47+
for (const line of lines) {
48+
lineOffsets.push(offset);
49+
offset += line.length + 1; // +1 for the '\n'
50+
}
51+
4352
let emptyCount = 0;
4453
let firstEmptyLine = -1;
4554

@@ -54,6 +63,12 @@ module.exports = {
5463
const startLine = firstEmptyLine + max + 1;
5564
const endLine = index;
5665

66+
// Remove the excess empty lines: keep `max` empty lines,
67+
// remove everything from the start of the (max+1)-th empty
68+
// line to the start of the next non-empty line.
69+
const rangeStart = lineOffsets[firstEmptyLine + max];
70+
const rangeEnd = lineOffsets[endLine];
71+
5772
context.report({
5873
loc: {
5974
start: { line: startLine + 1, column: 0 },
@@ -64,6 +79,9 @@ module.exports = {
6479
max,
6580
pluralizedLines: max === 1 ? 'line' : 'lines',
6681
},
82+
fix(fixer) {
83+
return fixer.replaceTextRange([rangeStart, rangeEnd], '');
84+
},
6785
});
6886
}
6987
emptyCount = 0;

tests/lib/rules/template-no-multiple-empty-lines.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ r
6060
6161
<div>World</div>
6262
</template>`,
63-
output: null,
63+
output: `<template>
64+
<div>Hello</div>
65+
66+
<div>World</div>
67+
</template>`,
6468
errors: [
6569
{
6670
message: 'More than 1 blank line not allowed.',
@@ -75,7 +79,11 @@ r
7579
7680
<div>Second</div>
7781
</template>`,
78-
output: null,
82+
output: `<template>
83+
<div>First</div>
84+
85+
<div>Second</div>
86+
</template>`,
7987
errors: [
8088
{
8189
message: 'More than 1 blank line not allowed.',
@@ -88,7 +96,9 @@ r
8896
8997
9098
<div>bar</div></template>`,
91-
output: null,
99+
output: `<template><div>foo</div>
100+
101+
<div>bar</div></template>`,
92102
errors: [{ message: 'More than 1 blank line not allowed.' }],
93103
},
94104
{
@@ -98,7 +108,9 @@ r
98108
99109
100110
<div>bar</div></template>`,
101-
output: null,
111+
output: `<template><div>foo</div>
112+
113+
<div>bar</div></template>`,
102114
errors: [{ message: 'More than 1 blank line not allowed.' }],
103115
},
104116
],
@@ -142,7 +154,9 @@ hbsRuleTester.run('template-no-multiple-empty-lines', rule, {
142154
143155
144156
<div>bar</div>`,
145-
output: null,
157+
output: `<div>foo</div>
158+
159+
<div>bar</div>`,
146160
errors: [{ message: 'More than 1 blank line not allowed.' }],
147161
},
148162
{
@@ -152,12 +166,14 @@ hbsRuleTester.run('template-no-multiple-empty-lines', rule, {
152166
153167
154168
<div>bar</div>`,
155-
output: null,
169+
output: `<div>foo</div>
170+
171+
<div>bar</div>`,
156172
errors: [{ message: 'More than 1 blank line not allowed.' }],
157173
},
158174
{
159175
code: '<div>foo</div>\n\n\n\n\n<div>bar</div>',
160-
output: null,
176+
output: '<div>foo</div>\n\n\n\n<div>bar</div>',
161177
options: [{ max: 3 }],
162178
errors: [{ message: 'More than 3 blank lines not allowed.' }],
163179
},

0 commit comments

Comments
 (0)