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 00c5f82 commit 19963be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
32 changes: 11 additions & 21 deletions lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,22 @@ module.exports = {

if (emptyBlocks.length === 0) return

if (autofix) {
const firstEmptyBlock = emptyBlocks[0]
for (const componentBlock of emptyBlocks) {
context.report({
node: firstEmptyBlock,
loc: firstEmptyBlock.loc,
node: componentBlock,
loc: componentBlock.loc,
messageId: 'unexpected',
data: {
blockName: firstEmptyBlock.name
blockName: componentBlock.name
},
*fix(fixer) {
for (const componentBlock of emptyBlocks) {
yield fixer.remove(componentBlock)
}
}
fix: autofix
? function* (fixer) {
for (const componentBlock of emptyBlocks) {
yield fixer.remove(componentBlock)
}
}
: undefined
})
} else {
for (const componentBlock of emptyBlocks) {
context.report({
node: componentBlock,
loc: componentBlock.loc,
messageId: 'unexpected',
data: {
blockName: componentBlock.name
}
})
}
}
}
}
Expand Down
37 changes: 29 additions & 8 deletions tests/lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,42 +169,63 @@ tester.run('no-empty-component-block', rule, {
]
},
{
code: '<template></template><script></script><style></style>',
output: '',
code: '<template></template> <script></script> <style></style>',
output: ' ',
options: [{ autofix: true }],
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: '',
code: '<template /> <script /> <style />',
output: ' ',
options: [{ autofix: true }],
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: '',
code: '<template src="" /> <script src="" /> <style src="" />',
output: ' ',
options: [{ autofix: true }],
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>',
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.'
},
{
message: '`<style>` is empty. Empty block is not allowed.'
}
]
}
Expand Down

0 comments on commit 19963be

Please sign in to comment.