Files
SnowLuma/.pnpm-store/v11/files/db/0b01f5bca51ccf126e7fc72bebae7a6db2fd8b01aad76f676bd79e2950f01b61cce43d18197f57a0e048717b2d80e2015c3d9a98ad9c38721d4a609998f222
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

67 lines
2.2 KiB
Plaintext

import { Z as skipChainExpression, f as createRule, g as import_ast_utils } from "../utils.js";
var newline_per_chained_call_default = createRule({
name: "newline-per-chained-call",
meta: {
type: "layout",
docs: { description: "Require a newline after each call in a method chain" },
fixable: "whitespace",
schema: [{
type: "object",
properties: { ignoreChainWithDepth: {
type: "integer",
minimum: 1,
maximum: 10
} },
additionalProperties: false
}],
defaultOptions: [{ ignoreChainWithDepth: 2 }],
messages: { expected: "Expected line break before `{{callee}}`." }
},
create(context, [options]) {
const { ignoreChainWithDepth } = options;
const sourceCode = context.sourceCode;
function getPrefix(node) {
if (node.computed) {
if (node.optional) return "?.[";
return "[";
}
if (node.optional) return "?.";
return ".";
}
function getPropertyText(node) {
const prefix = getPrefix(node);
const lines = sourceCode.getText(node.property).split(import_ast_utils.LINEBREAK_MATCHER);
const suffix = node.computed && lines.length === 1 ? "]" : "";
return prefix + lines[0] + suffix;
}
return { "CallExpression:exit": function(node) {
const callee = skipChainExpression(node.callee);
if (callee.type !== "MemberExpression") return;
let parent = skipChainExpression(callee.object);
let depth = 1;
while (parent && "callee" in parent && parent.callee) {
depth += 1;
const parentCallee = skipChainExpression(parent.callee);
if (!("object" in parentCallee)) break;
parent = skipChainExpression(parentCallee.object);
}
if (depth > ignoreChainWithDepth && (0, import_ast_utils.isTokenOnSameLine)(callee.object, callee.property)) {
const firstTokenAfterObject = sourceCode.getTokenAfter(callee.object, import_ast_utils.isNotClosingParenToken);
context.report({
node: callee.property,
loc: {
start: firstTokenAfterObject.loc.start,
end: callee.loc.end
},
messageId: "expected",
data: { callee: getPropertyText(callee) },
fix(fixer) {
return fixer.insertTextBefore(firstTokenAfterObject, "\n");
}
});
}
} };
}
});
export { newline_per_chained_call_default as t };