From 947a9b0f40ea0b6c8fe1e5ab9be7fb6a8b7ebecf Mon Sep 17 00:00:00 2001 From: waynzh Date: Mon, 4 Nov 2024 01:22:53 +0800 Subject: [PATCH 01/11] feat(no-empty-component-block): add autofix option --- lib/rules/no-empty-component-block.js | 41 +++++++++++++- tests/lib/rules/no-empty-component-block.js | 59 ++++++++++++++++++++- 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/lib/rules/no-empty-component-block.js b/lib/rules/no-empty-component-block.js index b2e428bc7..1d6179b2d 100644 --- a/lib/rules/no-empty-component-block.js +++ b/lib/rules/no-empty-component-block.js @@ -45,8 +45,16 @@ module.exports = { categories: undefined, url: 'https://eslint.vuejs.org/rules/no-empty-component-block.html' }, - fixable: null, - schema: [], + fixable: 'code', + schema: [ + { + type: 'object', + properties: { + autofix: { type: 'boolean' } + }, + additionalProperties: false + } + ], messages: { unexpected: '`<{{ blockName }}>` is empty. Empty block is not allowed.' } @@ -66,10 +74,16 @@ module.exports = { return {} } + const options = context.options[0] + const autofix = options?.autofix === true + const componentBlocks = documentFragment.children.filter(isVElement) return { Program() { + /** @type {VElement[]} */ + const emptyBlocks = [] + for (const componentBlock of componentBlocks) { if ( componentBlock.name !== 'template' && @@ -85,6 +99,29 @@ module.exports = { isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) || componentBlock.children.length === 0 ) { + emptyBlocks.push(componentBlock) + } + } + + if (emptyBlocks.length === 0) return + + if (autofix) { + const firstEmptyBlock = emptyBlocks[0] + context.report({ + node: firstEmptyBlock, + loc: firstEmptyBlock.loc, + messageId: 'unexpected', + data: { + blockName: firstEmptyBlock.name + }, + *fix(fixer) { + for (const componentBlock of emptyBlocks) { + yield fixer.remove(componentBlock) + } + } + }) + } else { + for (const componentBlock of emptyBlocks) { context.report({ node: componentBlock, loc: componentBlock.loc, diff --git a/tests/lib/rules/no-empty-component-block.js b/tests/lib/rules/no-empty-component-block.js index e54fd9df6..8c5cc1ac2 100644 --- a/tests/lib/rules/no-empty-component-block.js +++ b/tests/lib/rules/no-empty-component-block.js @@ -22,11 +22,17 @@ tester.run('no-empty-component-block', rule, { `