Skip to content

Commit

Permalink
feat(no-empty-component-block): support auto fix (#2595)
Browse files Browse the repository at this point in the history
Co-authored-by: Yosuke Ota <[email protected]>
Co-authored-by: Flo Edelmann <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent a5fb774 commit b779f88
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ For example:
| [vue/no-deprecated-delete-set](./no-deprecated-delete-set.md) | disallow using deprecated `$delete` and `$set` (in Vue.js 3.0.0+) | | :warning: |
| [vue/no-deprecated-model-definition](./no-deprecated-model-definition.md) | disallow deprecated `model` definition (in Vue.js 3.0.0+) | :bulb: | :warning: |
| [vue/no-duplicate-attr-inheritance](./no-duplicate-attr-inheritance.md) | enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"` | | :hammer: |
| [vue/no-empty-component-block](./no-empty-component-block.md) | disallow the `<template>` `<script>` `<style>` block to be empty | | :hammer: |
| [vue/no-empty-component-block](./no-empty-component-block.md) | disallow the `<template>` `<script>` `<style>` block to be empty | :wrench: | :hammer: |
| [vue/no-multiple-objects-in-class](./no-multiple-objects-in-class.md) | disallow to pass multiple objects into array to class | | :hammer: |
| [vue/no-potential-component-option-typo](./no-potential-component-option-typo.md) | disallow a potential typo in your component property | :bulb: | :hammer: |
| [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: |
Expand Down
4 changes: 3 additions & 1 deletion docs/rules/no-empty-component-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ since: v7.0.0

> disallow the `<template>` `<script>` `<style>` block to be empty
- :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.

## :book: Rule Details

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

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

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

```vue
<!-- ✓ GOOD -->
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-empty-component-block.html'
},
fixable: null,
fixable: 'code',
schema: [],
messages: {
unexpected: '`<{{ blockName }}>` is empty. Empty block is not allowed.'
Expand Down Expand Up @@ -91,6 +91,9 @@ module.exports = {
messageId: 'unexpected',
data: {
blockName: componentBlock.name
},
fix(fixer) {
return fixer.remove(componentBlock)
}
})
}
Expand Down
69 changes: 69 additions & 0 deletions tests/lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tester.run('no-empty-component-block', rule, {
invalid: [
{
code: `<template></template>`,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -35,6 +36,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: `<template> </template>`,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -44,6 +46,7 @@ tester.run('no-empty-component-block', rule, {
{
code: `<template>
</template>`,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -52,6 +55,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template />',
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -60,6 +64,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template src="" />',
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -68,6 +73,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template></template><script></script>',
output: '<script></script>',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -79,6 +85,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template /><script />',
output: '<script />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -90,6 +97,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template src="" /><script src="" />',
output: '<script src="" />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -101,6 +109,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template></template><script></script><style></style>',
output: '<script></script>',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -115,6 +124,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template /><script /><style />',
output: '<script />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -129,6 +139,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template src="" /><script src="" /><style src="" />',
output: '<script src="" />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -140,6 +151,64 @@ tester.run('no-empty-component-block', rule, {
message: '`<style>` is empty. Empty block is not allowed.'
}
]
},
// auto fix with whitespace
{
code: '<template></template> <script></script> <style></style>',
output: ' ',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
},
{
message: '`<script>` is empty. Empty block is not allowed.'
},
{
message: '`<style>` is empty. Empty block is not allowed.'
}
]
},
{
code: '<template /> <script /> <style />',
output: ' ',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
},
{
message: '`<script>` is empty. Empty block is not allowed.'
},
{
message: '`<style>` is empty. Empty block is not allowed.'
}
]
},
{
code: '<template src="" /> <script src="" /> <style src="" />',
output: ' ',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
},
{
message: '`<script>` is empty. Empty block is not allowed.'
},
{
message: '`<style>` is empty. Empty block is not allowed.'
}
]
},
{
code: '<template><p></p></template> <script src="" /> <style src="" />',
output: '<template><p></p></template> ',
errors: [
{
message: '`<script>` is empty. Empty block is not allowed.'
},
{
message: '`<style>` is empty. Empty block is not allowed.'
}
]
}
]
})

0 comments on commit b779f88

Please sign in to comment.