Skip to content

Commit 947a9b0

Browse files
committed
feat(no-empty-component-block): add autofix option
1 parent 50bde65 commit 947a9b0

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

lib/rules/no-empty-component-block.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ module.exports = {
4545
categories: undefined,
4646
url: 'https://eslint.vuejs.org/rules/no-empty-component-block.html'
4747
},
48-
fixable: null,
49-
schema: [],
48+
fixable: 'code',
49+
schema: [
50+
{
51+
type: 'object',
52+
properties: {
53+
autofix: { type: 'boolean' }
54+
},
55+
additionalProperties: false
56+
}
57+
],
5058
messages: {
5159
unexpected: '`<{{ blockName }}>` is empty. Empty block is not allowed.'
5260
}
@@ -66,10 +74,16 @@ module.exports = {
6674
return {}
6775
}
6876

77+
const options = context.options[0]
78+
const autofix = options?.autofix === true
79+
6980
const componentBlocks = documentFragment.children.filter(isVElement)
7081

7182
return {
7283
Program() {
84+
/** @type {VElement[]} */
85+
const emptyBlocks = []
86+
7387
for (const componentBlock of componentBlocks) {
7488
if (
7589
componentBlock.name !== 'template' &&
@@ -85,6 +99,29 @@ module.exports = {
8599
isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) ||
86100
componentBlock.children.length === 0
87101
) {
102+
emptyBlocks.push(componentBlock)
103+
}
104+
}
105+
106+
if (emptyBlocks.length === 0) return
107+
108+
if (autofix) {
109+
const firstEmptyBlock = emptyBlocks[0]
110+
context.report({
111+
node: firstEmptyBlock,
112+
loc: firstEmptyBlock.loc,
113+
messageId: 'unexpected',
114+
data: {
115+
blockName: firstEmptyBlock.name
116+
},
117+
*fix(fixer) {
118+
for (const componentBlock of emptyBlocks) {
119+
yield fixer.remove(componentBlock)
120+
}
121+
}
122+
})
123+
} else {
124+
for (const componentBlock of emptyBlocks) {
88125
context.report({
89126
node: componentBlock,
90127
loc: componentBlock.loc,

tests/lib/rules/no-empty-component-block.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ tester.run('no-empty-component-block', rule, {
2222
`<template src="./template.html" /><script src="./script.js" />`,
2323
`<template src="./template.html"></template><script src="./script.js"></script><style src="./style.css"></style>`,
2424
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`,
25-
`var a = 1`
25+
`var a = 1`,
26+
// options
27+
{
28+
code: '<template><p>foo</p></template>',
29+
options: [{ autofix: true }]
30+
}
2631
],
2732
invalid: [
2833
{
2934
code: `<template></template>`,
35+
output: null,
3036
errors: [
3137
{
3238
message: '`<template>` is empty. Empty block is not allowed.'
@@ -35,6 +41,7 @@ tester.run('no-empty-component-block', rule, {
3541
},
3642
{
3743
code: `<template> </template>`,
44+
output: null,
3845
errors: [
3946
{
4047
message: '`<template>` is empty. Empty block is not allowed.'
@@ -44,6 +51,7 @@ tester.run('no-empty-component-block', rule, {
4451
{
4552
code: `<template>
4653
</template>`,
54+
output: null,
4755
errors: [
4856
{
4957
message: '`<template>` is empty. Empty block is not allowed.'
@@ -52,6 +60,7 @@ tester.run('no-empty-component-block', rule, {
5260
},
5361
{
5462
code: '<template />',
63+
output: null,
5564
errors: [
5665
{
5766
message: '`<template>` is empty. Empty block is not allowed.'
@@ -60,6 +69,7 @@ tester.run('no-empty-component-block', rule, {
6069
},
6170
{
6271
code: '<template src="" />',
72+
output: null,
6373
errors: [
6474
{
6575
message: '`<template>` is empty. Empty block is not allowed.'
@@ -68,6 +78,7 @@ tester.run('no-empty-component-block', rule, {
6878
},
6979
{
7080
code: '<template></template><script></script>',
81+
output: null,
7182
errors: [
7283
{
7384
message: '`<template>` is empty. Empty block is not allowed.'
@@ -79,6 +90,7 @@ tester.run('no-empty-component-block', rule, {
7990
},
8091
{
8192
code: '<template /><script />',
93+
output: null,
8294
errors: [
8395
{
8496
message: '`<template>` is empty. Empty block is not allowed.'
@@ -90,6 +102,7 @@ tester.run('no-empty-component-block', rule, {
90102
},
91103
{
92104
code: '<template src="" /><script src="" />',
105+
output: null,
93106
errors: [
94107
{
95108
message: '`<template>` is empty. Empty block is not allowed.'
@@ -101,6 +114,7 @@ tester.run('no-empty-component-block', rule, {
101114
},
102115
{
103116
code: '<template></template><script></script><style></style>',
117+
output: null,
104118
errors: [
105119
{
106120
message: '`<template>` is empty. Empty block is not allowed.'
@@ -115,6 +129,7 @@ tester.run('no-empty-component-block', rule, {
115129
},
116130
{
117131
code: '<template /><script /><style />',
132+
output: null,
118133
errors: [
119134
{
120135
message: '`<template>` is empty. Empty block is not allowed.'
@@ -129,6 +144,7 @@ tester.run('no-empty-component-block', rule, {
129144
},
130145
{
131146
code: '<template src="" /><script src="" /><style src="" />',
147+
output: null,
132148
errors: [
133149
{
134150
message: '`<template>` is empty. Empty block is not allowed.'
@@ -140,6 +156,47 @@ tester.run('no-empty-component-block', rule, {
140156
message: '`<style>` is empty. Empty block is not allowed.'
141157
}
142158
]
159+
},
160+
// autofix
161+
{
162+
code: `<template></template>`,
163+
output: '',
164+
options: [{ autofix: true }],
165+
errors: [
166+
{
167+
message: '`<template>` is empty. Empty block is not allowed.'
168+
}
169+
]
170+
},
171+
{
172+
code: '<template></template><script></script><style></style>',
173+
output: '',
174+
options: [{ autofix: true }],
175+
errors: [
176+
{
177+
message: '`<template>` is empty. Empty block is not allowed.'
178+
}
179+
]
180+
},
181+
{
182+
code: '<template /><script /><style />',
183+
output: '',
184+
options: [{ autofix: true }],
185+
errors: [
186+
{
187+
message: '`<template>` is empty. Empty block is not allowed.'
188+
}
189+
]
190+
},
191+
{
192+
code: '<template src="" /><script src="" /><style src="" />',
193+
output: '',
194+
options: [{ autofix: true }],
195+
errors: [
196+
{
197+
message: '`<template>` is empty. Empty block is not allowed.'
198+
}
199+
]
143200
}
144201
]
145202
})

0 commit comments

Comments
 (0)