2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Require or disallow newline at the end of files
|
|
|
|
* @author Nodeca Team <https://github.com/nodeca>
|
2024-08-21 06:34:30 +00:00
|
|
|
* @deprecated in ESLint v8.53.0
|
2024-08-09 12:04:48 +00:00
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
2024-08-21 06:34:30 +00:00
|
|
|
deprecated: true,
|
|
|
|
replacedBy: [],
|
2024-08-09 12:04:48 +00:00
|
|
|
type: "layout",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Require or disallow newline at the end of files",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/eol-last"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fixable: "whitespace",
|
|
|
|
|
|
|
|
schema: [
|
|
|
|
{
|
|
|
|
enum: ["always", "never", "unix", "windows"]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
missing: "Newline required at end of file but not found.",
|
|
|
|
unexpected: "Newline not allowed at end of file."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create(context) {
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// Public
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return {
|
|
|
|
Program: function checkBadEOF(node) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const sourceCode = context.sourceCode,
|
2024-08-09 12:04:48 +00:00
|
|
|
src = sourceCode.getText(),
|
|
|
|
lastLine = sourceCode.lines[sourceCode.lines.length - 1],
|
|
|
|
location = {
|
|
|
|
column: lastLine.length,
|
|
|
|
line: sourceCode.lines.length
|
|
|
|
},
|
|
|
|
LF = "\n",
|
|
|
|
CRLF = `\r${LF}`,
|
|
|
|
endsWithNewline = src.endsWith(LF);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Empty source is always valid: No content in file so we don't
|
|
|
|
* need to lint for a newline on the last line of content.
|
|
|
|
*/
|
|
|
|
if (!src.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mode = context.options[0] || "always",
|
|
|
|
appendCRLF = false;
|
|
|
|
|
|
|
|
if (mode === "unix") {
|
|
|
|
|
|
|
|
// `"unix"` should behave exactly as `"always"`
|
|
|
|
mode = "always";
|
|
|
|
}
|
|
|
|
if (mode === "windows") {
|
|
|
|
|
|
|
|
// `"windows"` should behave exactly as `"always"`, but append CRLF in the fixer for backwards compatibility
|
|
|
|
mode = "always";
|
|
|
|
appendCRLF = true;
|
|
|
|
}
|
|
|
|
if (mode === "always" && !endsWithNewline) {
|
|
|
|
|
|
|
|
// File is not newline-terminated, but should be
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
loc: location,
|
|
|
|
messageId: "missing",
|
|
|
|
fix(fixer) {
|
|
|
|
return fixer.insertTextAfterRange([0, src.length], appendCRLF ? CRLF : LF);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (mode === "never" && endsWithNewline) {
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
const secondLastLine = sourceCode.lines[sourceCode.lines.length - 2];
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
// File is newline-terminated, but shouldn't be
|
|
|
|
context.report({
|
|
|
|
node,
|
2024-08-21 06:34:30 +00:00
|
|
|
loc: {
|
|
|
|
start: { line: sourceCode.lines.length - 1, column: secondLastLine.length },
|
|
|
|
end: { line: sourceCode.lines.length, column: 0 }
|
|
|
|
},
|
2024-08-09 12:04:48 +00:00
|
|
|
messageId: "unexpected",
|
|
|
|
fix(fixer) {
|
|
|
|
const finalEOLs = /(?:\r?\n)+$/u,
|
|
|
|
match = finalEOLs.exec(sourceCode.text),
|
|
|
|
start = match.index,
|
|
|
|
end = sourceCode.text.length;
|
|
|
|
|
|
|
|
return fixer.replaceTextRange([start, end], "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|