Files
SnowLuma/.pnpm-store/v11/files/98/89f7a460044afdcd75b15f411e80c5f85d2d04f66d24b1063f490968b0d0aac73eda059145b7a411318e59221c1d3798178c52ea1deef0c58ccdc6575b06d4
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

51 lines
1.7 KiB
Plaintext

import { a as isDOMComponent, f as createRule } from "../utils.js";
var jsx_self_closing_comp_default = createRule({
name: "jsx-self-closing-comp",
meta: {
type: "layout",
docs: { description: "Disallow extra closing tags for components without children" },
fixable: "code",
schema: [{
type: "object",
properties: {
component: { type: "boolean" },
html: { type: "boolean" }
},
additionalProperties: false
}],
defaultOptions: [{
component: true,
html: true
}],
messages: { notSelfClosing: "Empty components are self-closing" }
},
create(context, [configuration]) {
const { component, html } = configuration;
function isComponent(node) {
return node.name && (node.name.type === "JSXIdentifier" || node.name.type === "JSXMemberExpression") && !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) {
return (!!component && isComponent(node) || !!html && isDOMComponent(node)) && !node.selfClosing && (childrenIsEmpty(node) || childrenIsMultilineSpaces(node));
}
return { JSXOpeningElement(node) {
if (!isShouldBeSelfClosed(node)) return;
context.report({
messageId: "notSelfClosing",
node,
fix(fixer) {
const range = [node.range[1] - 1, node.parent.closingElement?.range[1] ?? NaN];
return fixer.replaceTextRange(range, " />");
}
});
} };
}
});
export { jsx_self_closing_comp_default as t };