Skip to content

Commit

Permalink
feat: autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh committed Nov 11, 2024
1 parent 05bac31 commit a37b596
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 71 deletions.
12 changes: 2 additions & 10 deletions docs/rules/no-empty-component-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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 fix :rules="{'vue/no-empty-component-block': ['error', { autofix: true }]}">
<eslint-code-block fix :rules="{'vue/no-empty-component-block': ['error']}">

```vue
<!-- ✓ GOOD -->
Expand Down Expand Up @@ -64,15 +64,7 @@ p {

## :wrench: Options

```json
{
"vue/no-empty-component-block": ["error", {
"autofix": false,
}]
}
```

- `"autofix"` ... If `true`, enable autofix. (Default: `false`)
Nothing.

## :rocket: Version

Expand Down
17 changes: 4 additions & 13 deletions lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/no-empty-component-block.html'
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
autofix: { type: 'boolean' }
},
additionalProperties: false
}
],
schema: [],
messages: {
unexpected: '`<{{ blockName }}>` is empty. Empty block is not allowed.'
}
Expand All @@ -74,9 +66,6 @@ module.exports = {
return {}
}

const options = context.options[0]
const autofix = options?.autofix === true

const componentBlocks = documentFragment.children.filter(isVElement)

return {
Expand Down Expand Up @@ -113,7 +102,9 @@ module.exports = {
data: {
blockName: componentBlock.name
},
fix: autofix ? (fixer) => fixer.remove(componentBlock) : undefined
fix(fixer) {
return fixer.remove(componentBlock)
}
})
}
}
Expand Down
61 changes: 13 additions & 48 deletions tests/lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,12 @@ tester.run('no-empty-component-block', rule, {
`<template src="./template.html" /><script src="./script.js" />`,
`<template src="./template.html"></template><script src="./script.js"></script><style src="./style.css"></style>`,
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`,
`var a = 1`,
// options
{
code: '<template><p>foo</p></template>',
options: [{ autofix: true }]
}
`var a = 1`
],
invalid: [
{
code: `<template></template>`,
output: null,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -41,7 +36,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: `<template> </template>`,
output: null,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -51,7 +46,7 @@ tester.run('no-empty-component-block', rule, {
{
code: `<template>
</template>`,
output: null,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -60,7 +55,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template />',
output: null,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -69,7 +64,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template src="" />',
output: null,
output: '',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -78,7 +73,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template></template><script></script>',
output: null,
output: '<script></script>',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -90,7 +85,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template /><script />',
output: null,
output: '<script />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -102,7 +97,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template src="" /><script src="" />',
output: null,
output: '<script src="" />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -114,7 +109,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template></template><script></script><style></style>',
output: null,
output: '<script></script>',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -129,7 +124,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template /><script /><style />',
output: null,
output: '<script />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -144,34 +139,7 @@ tester.run('no-empty-component-block', rule, {
},
{
code: '<template src="" /><script src="" /><style src="" />',
output: null,
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.'
}
]
},
// autofix
{
code: `<template></template>`,
output: '',
options: [{ autofix: true }],
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
}
]
},
{
code: '<template></template><script></script><style></style>',
output: '<script></script>',
options: [{ autofix: true }],
output: '<script src="" />',
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -184,10 +152,10 @@ tester.run('no-empty-component-block', rule, {
}
]
},
// auto fix with whitespace
{
code: '<template></template> <script></script> <style></style>',
output: ' ',
options: [{ autofix: true }],
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -203,7 +171,6 @@ tester.run('no-empty-component-block', rule, {
{
code: '<template /> <script /> <style />',
output: ' ',
options: [{ autofix: true }],
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -219,7 +186,6 @@ tester.run('no-empty-component-block', rule, {
{
code: '<template src="" /> <script src="" /> <style src="" />',
output: ' ',
options: [{ autofix: true }],
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand All @@ -235,7 +201,6 @@ tester.run('no-empty-component-block', rule, {
{
code: '<template><p></p></template> <script src="" /> <style src="" />',
output: '<template><p></p></template> ',
options: [{ autofix: true }],
errors: [
{
message: '`<script>` is empty. Empty block is not allowed.'
Expand Down

0 comments on commit a37b596

Please sign in to comment.