Files
SnowLuma/.pnpm-store/v11/files/30/e168a80ec5e66bbbea9da43dfa05eddca55e1147c839465e7de74940905d688786e3c604eb44fdd15aa617c3fb8dc17eec531d9739e1f07e41f8671981f9ad
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

140 lines
5.0 KiB
Plaintext

import { _ as ASSIGNMENT_OPERATOR, f as createRule, z as isKeywordToken } from "../utils.js";
var indent_binary_ops_default = createRule({
name: "indent-binary-ops",
meta: {
type: "layout",
docs: { description: "Indentation for binary operators" },
fixable: "whitespace",
schema: [{ oneOf: [{
type: "integer",
minimum: 0
}, {
type: "string",
enum: ["tab"]
}] }],
defaultOptions: [2],
messages: { wrongIndentation: "Expected indentation of {{expected}}" }
},
create: (context, options) => {
const { sourceCode } = context;
const indentStr = options[0] === "tab" ? " " : " ".repeat(options[0] ?? 2);
const indentCache = /* @__PURE__ */ new Map();
function getIndentOfLine(line) {
if (indentCache.has(line)) return indentCache.get(line);
return sourceCode.lines[line - 1].match(/^\s*/)?.[0] ?? "";
}
function subtractionIndent(indent) {
if (options[0] === "tab") return indent.slice(1);
return indent.slice(options[0] ?? 2);
}
function getTargetIndent(indent, needAdditionIndent, needSubtractionIndent) {
if (needAdditionIndent && !needSubtractionIndent) return indent + indentStr;
if (!needAdditionIndent && needSubtractionIndent) return subtractionIndent(indent);
return indent;
}
function firstTokenOfLine(line) {
return sourceCode.tokensAndComments.find((token) => token.loc.start.line === line);
}
function lastTokenOfLine(line) {
return [...sourceCode.tokensAndComments].reverse().find((token) => token.loc.end.line === line);
}
function isGreaterThanCloseBracketOfLine(line) {
const tokensAndCommentsOfLine = sourceCode.tokensAndComments.filter((token) => token.loc.start.line === line);
let openBracketCount = 0;
let closeBracketCount = 0;
for (const token of tokensAndCommentsOfLine) {
if (token.value === "(") openBracketCount++;
if (token.value === ")") closeBracketCount++;
}
return openBracketCount < closeBracketCount;
}
function isTypeKeywordOfNode(typeToken, node) {
while (node.parent) {
node = node.parent;
if (node.type === "TSTypeAliasDeclaration" && context.sourceCode.getTokenBefore(node.id) === typeToken) return true;
}
return false;
}
function handler(node, right) {
if (node.loc.start.line === node.loc.end.line) return;
let tokenRight = sourceCode.getFirstToken(right);
let tokenOperator = sourceCode.getTokenBefore(tokenRight);
while (tokenOperator.value === "(") {
tokenRight = tokenOperator;
tokenOperator = sourceCode.getTokenBefore(tokenRight);
if (tokenOperator.range[0] <= right.parent.range[0]) return;
}
const tokenBeforeAll = sourceCode.getTokenBefore(node);
const tokenLeft = sourceCode.getTokenBefore(tokenOperator);
if (!(tokenRight.loc.start.line !== tokenLeft.loc.start.line)) return;
const firstTokenOfLineLeft = firstTokenOfLine(tokenLeft.loc.start.line);
const lastTokenOfLineLeft = lastTokenOfLine(tokenLeft.loc.start.line);
const needAdditionIndent = isKeywordToken(firstTokenOfLineLeft) && ![
"typeof",
"instanceof",
"this"
].includes(firstTokenOfLineLeft.value) || firstTokenOfLineLeft?.type === "Identifier" && firstTokenOfLineLeft.value === "type" && isTypeKeywordOfNode(firstTokenOfLineLeft, node) || [
":",
"[",
"(",
"<"
].concat(ASSIGNMENT_OPERATOR).includes(lastTokenOfLineLeft?.value || "") || [
"[",
"(",
"{",
"=>",
":"
].concat(ASSIGNMENT_OPERATOR).includes(tokenBeforeAll?.value || "") && firstTokenOfLineLeft?.loc.start.line === tokenBeforeAll?.loc.start.line;
const needSubtractionIndent = lastTokenOfLineLeft?.value === ")" && isGreaterThanCloseBracketOfLine(tokenLeft.loc.start.line) && ![
"]",
")",
"}"
].includes(firstTokenOfLineLeft?.value || "");
const indentLeft = getIndentOfLine(tokenLeft.loc.start.line);
const indentRight = getIndentOfLine(tokenRight.loc.start.line);
const indentTarget = getTargetIndent(indentLeft, needAdditionIndent, needSubtractionIndent);
if (indentTarget !== indentRight) {
const start = {
line: tokenRight.loc.start.line,
column: 0
};
const end = {
line: tokenRight.loc.start.line,
column: indentRight.length
};
context.report({
loc: {
start,
end
},
messageId: "wrongIndentation",
data: { expected: `${indentTarget.length} ${options[0] === "tab" ? "tab" : "space"}${indentTarget.length === 1 ? "" : "s"}` },
fix(fixer) {
return fixer.replaceTextRange([sourceCode.getIndexFromLoc(start), sourceCode.getIndexFromLoc(end)], indentTarget);
}
});
indentCache.set(tokenRight.loc.start.line, indentTarget);
}
}
return {
BinaryExpression(node) {
handler(node, node.right);
},
LogicalExpression(node) {
handler(node, node.right);
},
TSUnionType(node) {
if (node.types.length > 1) node.types.forEach((type) => {
handler(node, type);
});
},
TSIntersectionType(node) {
if (node.types.length > 1) node.types.forEach((type) => {
handler(node, type);
});
}
};
}
});
export { indent_binary_ops_default as t };