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

68 lines
2.0 KiB
Plaintext

import { $ as warnDeprecation, c as hasLinesAndGetLocFromIndex, f as createRule, u as isTextSourceCode } from "../utils.js";
var eol_last_default = createRule({
name: "eol-last",
meta: {
type: "layout",
docs: { description: "Require or disallow newline at the end of files" },
fixable: "whitespace",
schema: [{
type: "string",
enum: [
"always",
"never",
"unix",
"windows"
]
}],
defaultOptions: ["always"],
messages: {
missing: "Newline required at end of file but not found.",
unexpected: "Newline not allowed at end of file."
}
},
create(context, [mode]) {
const sourceCode = context.sourceCode;
if (!isTextSourceCode(sourceCode) || !hasLinesAndGetLocFromIndex(sourceCode)) return {};
const src = sourceCode.text;
const LF = "\n";
const CRLF = `\r${LF}`;
const endsWithNewline = src.endsWith(LF);
if (!src.length) return {};
let appendCRLF = false;
if (mode === "unix") {
warnDeprecation("option(\"unix\")", "\"always\" and \"@stylistic/eslint-plugin/rules/linebreak-style\"", "eol-last");
mode = "always";
}
if (mode === "windows") {
warnDeprecation("option(\"windows\")", "\"always\" and \"@stylistic/eslint-plugin/rules/linebreak-style\"", "eol-last");
mode = "always";
appendCRLF = true;
}
if (mode === "always" && !endsWithNewline) context.report({
loc: sourceCode.getLocFromIndex(src.length),
messageId: "missing",
fix(fixer) {
return fixer.insertTextAfterRange([0, src.length], appendCRLF ? CRLF : LF);
}
});
else if (mode === "never" && endsWithNewline) {
const startLoc = sourceCode.getLocFromIndex(src.length - (src.endsWith(CRLF) ? 2 : 1));
const endLoc = sourceCode.getLocFromIndex(src.length);
context.report({
loc: {
start: startLoc,
end: endLoc
},
messageId: "unexpected",
fix(fixer) {
const start = /(?:\r?\n)+$/u.exec(src).index;
const end = src.length;
return fixer.replaceTextRange([start, end], "");
}
});
}
return {};
}
});
export { eol_last_default as t };