Files
SnowLuma/.pnpm-store/v11/files/2a/a4e99351281e04ccbf87ad3dd3838f12aec529e096f292c9350dfb1ed91bb683638ef70f0b7b8d42ec568b7c63fb01faee0ab6abc0c65a5380f466fef0b4ce
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

60 lines
2.3 KiB
Plaintext

import { V as isNodeFirstInLine, f as createRule } from "../utils.js";
const MESSAGE_LOCATION = {
"tag-aligned": "matchIndent",
"line-aligned": "alignWithOpening"
};
var jsx_closing_tag_location_default = createRule({
name: "jsx-closing-tag-location",
meta: {
type: "layout",
docs: { description: "Enforce closing tag location for multiline JSX" },
fixable: "whitespace",
schema: [{ anyOf: [{
type: "string",
enum: ["tag-aligned", "line-aligned"]
}] }],
defaultOptions: ["tag-aligned"],
messages: {
onOwnLine: "Closing tag of a multiline JSX expression must be on its own line.",
matchIndent: "Expected closing tag to match indentation of opening.",
alignWithOpening: "Expected closing tag to be aligned with the line containing the opening tag"
}
},
create(context, [option]) {
function getIndentation(openingStartOfLine, opening) {
if (option === "line-aligned") return openingStartOfLine.column;
else return opening.loc.start.column;
}
function handleClosingElement(node) {
if (!node.parent) return;
const sourceCode = context.sourceCode;
const opening = "openingFragment" in node.parent ? node.parent.openingFragment : node.parent.openingElement;
const openingLoc = sourceCode.getFirstToken(opening).loc.start;
const openingLine = sourceCode.lines[openingLoc.line - 1];
const openingStartOfLine = {
column: /^\s*/.exec(openingLine)?.[0].length,
line: openingLoc.line
};
if (opening.loc.start.line === node.loc.start.line) return;
if (opening.loc.start.column === node.loc.start.column && option === "tag-aligned") return;
if (openingStartOfLine.column === node.loc.start.column && option === "line-aligned") return;
const messageId = isNodeFirstInLine(context, node) ? MESSAGE_LOCATION[option] : "onOwnLine";
context.report({
node,
messageId,
loc: node.loc,
fix(fixer) {
const indent = new Array((getIndentation(openingStartOfLine, opening) || 0) + 1).join(" ");
if (isNodeFirstInLine(context, node)) return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]], indent);
return fixer.insertTextBefore(node, `\n${indent}`);
}
});
}
return {
JSXClosingElement: handleClosingElement,
JSXClosingFragment: handleClosingElement
};
}
});
export { jsx_closing_tag_location_default as t };