2 * @fileoverview Require or disallow newline at the end of files
3 * @author Nodeca Team <https://github.com/nodeca>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "require or disallow newline at the end of files",
17 category: "Stylistic Issues",
19 url: "https://eslint.org/docs/rules/eol-last"
22 fixable: "whitespace",
26 enum: ["always", "never", "unix", "windows"]
31 missing: "Newline required at end of file but not found.",
32 unexpected: "Newline not allowed at end of file."
37 //--------------------------------------------------------------------------
39 //--------------------------------------------------------------------------
42 Program: function checkBadEOF(node) {
43 const sourceCode = context.getSourceCode(),
44 src = sourceCode.getText(),
45 lastLine = sourceCode.lines[sourceCode.lines.length - 1],
47 column: lastLine.length,
48 line: sourceCode.lines.length
52 endsWithNewline = src.endsWith(LF);
55 * Empty source is always valid: No content in file so we don't
56 * need to lint for a newline on the last line of content.
62 let mode = context.options[0] || "always",
65 if (mode === "unix") {
67 // `"unix"` should behave exactly as `"always"`
70 if (mode === "windows") {
72 // `"windows"` should behave exactly as `"always"`, but append CRLF in the fixer for backwards compatibility
76 if (mode === "always" && !endsWithNewline) {
78 // File is not newline-terminated, but should be
84 return fixer.insertTextAfterRange([0, src.length], appendCRLF ? CRLF : LF);
87 } else if (mode === "never" && endsWithNewline) {
89 // File is newline-terminated, but shouldn't be
93 messageId: "unexpected",
95 const finalEOLs = /(?:\r?\n)+$/u,
96 match = finalEOLs.exec(sourceCode.text),
98 end = sourceCode.text.length;
100 return fixer.replaceTextRange([start, end], "");