Files
NapCatQQ/.pnpm-store/v3/files/d8/f0d392d689494b7c37211b5320dba91864983d140b68c18824795cd3f2f30fcf38b14182d7091725024171b2084bf9d0c0aed8653e1cfd27170729c094d8e2
T
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

74 lines
2.3 KiB
Plaintext

'use strict';
var utils = require('../utils.js');
require('eslint-visitor-keys');
require('espree');
require('estraverse');
const optionDefaults = { component: true, html: true };
const messages = {
notSelfClosing: "Empty components are self-closing"
};
var jsxSelfClosingComp = utils.createRule({
name: "jsx-self-closing-comp",
package: "jsx",
meta: {
type: "layout",
docs: {
description: "Disallow extra closing tags for components without children"
},
fixable: "code",
messages,
schema: [
{
type: "object",
properties: {
component: {
default: optionDefaults.component,
type: "boolean"
},
html: {
default: optionDefaults.html,
type: "boolean"
}
},
additionalProperties: false
}
]
},
create(context) {
function isComponent(node) {
return node.name && (node.name.type === "JSXIdentifier" || node.name.type === "JSXMemberExpression") && !utils.isDOMComponent(node);
}
function childrenIsEmpty(node) {
return node.parent.children.length === 0;
}
function childrenIsMultilineSpaces(node) {
const childrens = node.parent.children;
return childrens.length === 1 && childrens[0].type === "JSXText" && childrens[0].value.includes("\n") && childrens[0].value.replace(/(?!\xA0)\s/g, "") === "";
}
function isShouldBeSelfClosed(node) {
const configuration = Object.assign({}, optionDefaults, context.options[0]);
return (configuration.component && isComponent(node) || configuration.html && utils.isDOMComponent(node)) && !node.selfClosing && (childrenIsEmpty(node) || childrenIsMultilineSpaces(node));
}
return {
JSXOpeningElement(node) {
if (!isShouldBeSelfClosed(node))
return;
context.report({
messageId: "notSelfClosing",
node,
fix(fixer) {
const openingElementEnding = node.range[1] - 1;
const closingElementEnding = node.parent.closingElement?.range[1] ?? NaN;
const range = [openingElementEnding, closingElementEnding];
return fixer.replaceTextRange(range, " />");
}
});
}
};
}
});
module.exports = jsxSelfClosingComp;