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

69 lines
2.2 KiB
Plaintext

import { d as safeReplaceTextBetween, f as createRule, g as import_ast_utils } from "../utils.js";
const POSITION_SCHEMA = {
type: "string",
enum: [
"beside",
"below",
"any"
]
};
var nonblock_statement_body_position_default = createRule({
name: "nonblock-statement-body-position",
meta: {
type: "layout",
docs: { description: "Enforce the location of single-line statements" },
fixable: "whitespace",
schema: [POSITION_SCHEMA, {
type: "object",
properties: { overrides: {
type: "object",
properties: {
if: POSITION_SCHEMA,
else: POSITION_SCHEMA,
while: POSITION_SCHEMA,
do: POSITION_SCHEMA,
for: POSITION_SCHEMA
},
additionalProperties: false
} },
additionalProperties: false
}],
defaultOptions: ["beside"],
messages: {
expectNoLinebreak: "Expected no linebreak before this statement.",
expectLinebreak: "Expected a linebreak before this statement."
}
},
create(context, [style, { overrides = {} } = {}]) {
const sourceCode = context.sourceCode;
function validateStatement(node, keywordName) {
const option = overrides[keywordName] ?? style;
if (node.type === "BlockStatement" || option === "any") return;
const tokenBefore = sourceCode.getTokenBefore(node);
const onSameLine = (0, import_ast_utils.isTokenOnSameLine)(tokenBefore, node);
if (onSameLine && option === "below") context.report({
node,
messageId: "expectLinebreak",
fix: (fixer) => fixer.insertTextBefore(node, "\n")
});
else if (!onSameLine && option === "beside") context.report({
node,
messageId: "expectNoLinebreak",
fix: safeReplaceTextBetween(sourceCode, tokenBefore, node, " ")
});
}
return {
IfStatement(node) {
validateStatement(node.consequent, "if");
if (node.alternate && node.alternate.type !== "IfStatement") validateStatement(node.alternate, "else");
},
WhileStatement: (node) => validateStatement(node.body, "while"),
DoWhileStatement: (node) => validateStatement(node.body, "do"),
ForStatement: (node) => validateStatement(node.body, "for"),
ForInStatement: (node) => validateStatement(node.body, "for"),
ForOfStatement: (node) => validateStatement(node.body, "for")
};
}
});
export { nonblock_statement_body_position_default as t };