eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
61 lines (60 loc) • 2.26 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/no-empty-component-block.ts
/**
* @author tyankatsu <https://github.com/tyankatsu0105>
* See LICENSE file in root directory for full license.
*/
/**
* check whether has attribute `src`
*/
function hasAttributeSrc(componentBlock) {
const hasAttribute = componentBlock.startTag.attributes.length > 0;
const hasSrc = componentBlock.startTag.attributes.some((attribute) => !attribute.directive && attribute.key.name === "src" && attribute.value && attribute.value.value !== "");
return hasAttribute && hasSrc;
}
/**
* check whether value under the component block is only whitespaces or break lines
*/
function isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) {
return componentBlock.children.length === 1 && componentBlock.children[0].type === "VText" && !componentBlock.children[0].value.trim();
}
var no_empty_component_block_default = {
meta: {
type: "suggestion",
docs: {
description: "disallow the `<template>` `<script>` `<style>` block to be empty",
categories: void 0,
url: "https://eslint.vuejs.org/rules/no-empty-component-block.html"
},
fixable: "code",
schema: [],
messages: { unexpected: "`<{{ blockName }}>` is empty. Empty block is not allowed." }
},
create(context) {
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.getDocumentFragment) return {};
const documentFragment = sourceCode.parserServices.getDocumentFragment();
if (!documentFragment) return {};
const blockNamesToCheck = /* @__PURE__ */ new Set([
"template",
"script",
"style"
]);
const componentBlocks = documentFragment.children.filter(require_index.default.isVElement);
return { Program() {
for (const componentBlock of componentBlocks) {
if (!blockNamesToCheck.has(componentBlock.name) || hasAttributeSrc(componentBlock)) continue;
if (isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) || componentBlock.children.length === 0) context.report({
node: componentBlock,
loc: componentBlock.loc,
messageId: "unexpected",
data: { blockName: componentBlock.name },
fix(fixer) {
return fixer.remove(componentBlock);
}
});
}
} };
}
};
exports.default = no_empty_component_block_default;