Skip to content

Commit b779f88

Browse files
waynzhota-meshiFloEdelmann
authored
feat(no-empty-component-block): support auto fix (#2595)
Co-authored-by: Yosuke Ota <[email protected]> Co-authored-by: Flo Edelmann <[email protected]>
1 parent a5fb774 commit b779f88

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

docs/rules/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ For example:
232232
| [vue/no-deprecated-delete-set](./no-deprecated-delete-set.md) | disallow using deprecated `$delete` and `$set` (in Vue.js 3.0.0+) | | :warning: |
233233
| [vue/no-deprecated-model-definition](./no-deprecated-model-definition.md) | disallow deprecated `model` definition (in Vue.js 3.0.0+) | :bulb: | :warning: |
234234
| [vue/no-duplicate-attr-inheritance](./no-duplicate-attr-inheritance.md) | enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"` | | :hammer: |
235-
| [vue/no-empty-component-block](./no-empty-component-block.md) | disallow the `<template>` `<script>` `<style>` block to be empty | | :hammer: |
235+
| [vue/no-empty-component-block](./no-empty-component-block.md) | disallow the `<template>` `<script>` `<style>` block to be empty | :wrench: | :hammer: |
236236
| [vue/no-multiple-objects-in-class](./no-multiple-objects-in-class.md) | disallow to pass multiple objects into array to class | | :hammer: |
237237
| [vue/no-potential-component-option-typo](./no-potential-component-option-typo.md) | disallow a potential typo in your component property | :bulb: | :hammer: |
238238
| [vue/no-ref-object-reactivity-loss](./no-ref-object-reactivity-loss.md) | disallow usages of ref objects that can lead to loss of reactivity | | :warning: |

docs/rules/no-empty-component-block.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ since: v7.0.0
1010

1111
> disallow the `<template>` `<script>` `<style>` block to be empty
1212
13+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
14+
1315
## :book: Rule Details
1416

1517
This rule disallows the `<template>` `<script>` `<style>` block to be empty.
1618

1719
This rule also checks block what has attribute `src`.
1820
See [Vue Single-File Component (SFC) Spec](https://vue-loader.vuejs.org/spec.html#src-imports).
1921

20-
<eslint-code-block :rules="{'vue/no-empty-component-block': ['error']}">
22+
<eslint-code-block fix :rules="{'vue/no-empty-component-block': ['error']}">
2123

2224
```vue
2325
<!-- ✓ GOOD -->

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545
categories: undefined,
4646
url: 'https://eslint.vuejs.org/rules/no-empty-component-block.html'
4747
},
48-
fixable: null,
48+
fixable: 'code',
4949
schema: [],
5050
messages: {
5151
unexpected: '`<{{ blockName }}>` is empty. Empty block is not allowed.'
@@ -91,6 +91,9 @@ module.exports = {
9191
messageId: 'unexpected',
9292
data: {
9393
blockName: componentBlock.name
94+
},
95+
fix(fixer) {
96+
return fixer.remove(componentBlock)
9497
}
9598
})
9699
}

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

+69
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ tester.run('no-empty-component-block', rule, {
2727
invalid: [
2828
{
2929
code: `<template></template>`,
30+
output: '',
3031
errors: [
3132
{
3233
message: '`<template>` is empty. Empty block is not allowed.'
@@ -35,6 +36,7 @@ tester.run('no-empty-component-block', rule, {
3536
},
3637
{
3738
code: `<template> </template>`,
39+
output: '',
3840
errors: [
3941
{
4042
message: '`<template>` is empty. Empty block is not allowed.'
@@ -44,6 +46,7 @@ tester.run('no-empty-component-block', rule, {
4446
{
4547
code: `<template>
4648
</template>`,
49+
output: '',
4750
errors: [
4851
{
4952
message: '`<template>` is empty. Empty block is not allowed.'
@@ -52,6 +55,7 @@ tester.run('no-empty-component-block', rule, {
5255
},
5356
{
5457
code: '<template />',
58+
output: '',
5559
errors: [
5660
{
5761
message: '`<template>` is empty. Empty block is not allowed.'
@@ -60,6 +64,7 @@ tester.run('no-empty-component-block', rule, {
6064
},
6165
{
6266
code: '<template src="" />',
67+
output: '',
6368
errors: [
6469
{
6570
message: '`<template>` is empty. Empty block is not allowed.'
@@ -68,6 +73,7 @@ tester.run('no-empty-component-block', rule, {
6873
},
6974
{
7075
code: '<template></template><script></script>',
76+
output: '<script></script>',
7177
errors: [
7278
{
7379
message: '`<template>` is empty. Empty block is not allowed.'
@@ -79,6 +85,7 @@ tester.run('no-empty-component-block', rule, {
7985
},
8086
{
8187
code: '<template /><script />',
88+
output: '<script />',
8289
errors: [
8390
{
8491
message: '`<template>` is empty. Empty block is not allowed.'
@@ -90,6 +97,7 @@ tester.run('no-empty-component-block', rule, {
9097
},
9198
{
9299
code: '<template src="" /><script src="" />',
100+
output: '<script src="" />',
93101
errors: [
94102
{
95103
message: '`<template>` is empty. Empty block is not allowed.'
@@ -101,6 +109,7 @@ tester.run('no-empty-component-block', rule, {
101109
},
102110
{
103111
code: '<template></template><script></script><style></style>',
112+
output: '<script></script>',
104113
errors: [
105114
{
106115
message: '`<template>` is empty. Empty block is not allowed.'
@@ -115,6 +124,7 @@ tester.run('no-empty-component-block', rule, {
115124
},
116125
{
117126
code: '<template /><script /><style />',
127+
output: '<script />',
118128
errors: [
119129
{
120130
message: '`<template>` is empty. Empty block is not allowed.'
@@ -129,6 +139,7 @@ tester.run('no-empty-component-block', rule, {
129139
},
130140
{
131141
code: '<template src="" /><script src="" /><style src="" />',
142+
output: '<script src="" />',
132143
errors: [
133144
{
134145
message: '`<template>` is empty. Empty block is not allowed.'
@@ -140,6 +151,64 @@ tester.run('no-empty-component-block', rule, {
140151
message: '`<style>` is empty. Empty block is not allowed.'
141152
}
142153
]
154+
},
155+
// auto fix with whitespace
156+
{
157+
code: '<template></template> <script></script> <style></style>',
158+
output: ' ',
159+
errors: [
160+
{
161+
message: '`<template>` is empty. Empty block is not allowed.'
162+
},
163+
{
164+
message: '`<script>` is empty. Empty block is not allowed.'
165+
},
166+
{
167+
message: '`<style>` is empty. Empty block is not allowed.'
168+
}
169+
]
170+
},
171+
{
172+
code: '<template /> <script /> <style />',
173+
output: ' ',
174+
errors: [
175+
{
176+
message: '`<template>` is empty. Empty block is not allowed.'
177+
},
178+
{
179+
message: '`<script>` is empty. Empty block is not allowed.'
180+
},
181+
{
182+
message: '`<style>` is empty. Empty block is not allowed.'
183+
}
184+
]
185+
},
186+
{
187+
code: '<template src="" /> <script src="" /> <style src="" />',
188+
output: ' ',
189+
errors: [
190+
{
191+
message: '`<template>` is empty. Empty block is not allowed.'
192+
},
193+
{
194+
message: '`<script>` is empty. Empty block is not allowed.'
195+
},
196+
{
197+
message: '`<style>` is empty. Empty block is not allowed.'
198+
}
199+
]
200+
},
201+
{
202+
code: '<template><p></p></template> <script src="" /> <style src="" />',
203+
output: '<template><p></p></template> ',
204+
errors: [
205+
{
206+
message: '`<script>` is empty. Empty block is not allowed.'
207+
},
208+
{
209+
message: '`<style>` is empty. Empty block is not allowed.'
210+
}
211+
]
143212
}
144213
]
145214
})

0 commit comments

Comments
 (0)