[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Require or disallow newline at the end of files
|
---|
| 3 | * @author Nodeca Team <https://github.com/nodeca>
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Rule Definition
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | /** @type {import('../shared/types').Rule} */
|
---|
| 13 | module.exports = {
|
---|
| 14 | meta: {
|
---|
| 15 | deprecated: true,
|
---|
| 16 | replacedBy: [],
|
---|
| 17 | type: "layout",
|
---|
| 18 |
|
---|
| 19 | docs: {
|
---|
| 20 | description: "Require or disallow newline at the end of files",
|
---|
| 21 | recommended: false,
|
---|
| 22 | url: "https://eslint.org/docs/latest/rules/eol-last"
|
---|
| 23 | },
|
---|
| 24 |
|
---|
| 25 | fixable: "whitespace",
|
---|
| 26 |
|
---|
| 27 | schema: [
|
---|
| 28 | {
|
---|
| 29 | enum: ["always", "never", "unix", "windows"]
|
---|
| 30 | }
|
---|
| 31 | ],
|
---|
| 32 |
|
---|
| 33 | messages: {
|
---|
| 34 | missing: "Newline required at end of file but not found.",
|
---|
| 35 | unexpected: "Newline not allowed at end of file."
|
---|
| 36 | }
|
---|
| 37 | },
|
---|
| 38 | create(context) {
|
---|
| 39 |
|
---|
| 40 | //--------------------------------------------------------------------------
|
---|
| 41 | // Public
|
---|
| 42 | //--------------------------------------------------------------------------
|
---|
| 43 |
|
---|
| 44 | return {
|
---|
| 45 | Program: function checkBadEOF(node) {
|
---|
| 46 | const sourceCode = context.sourceCode,
|
---|
| 47 | src = sourceCode.getText(),
|
---|
| 48 | lastLine = sourceCode.lines[sourceCode.lines.length - 1],
|
---|
| 49 | location = {
|
---|
| 50 | column: lastLine.length,
|
---|
| 51 | line: sourceCode.lines.length
|
---|
| 52 | },
|
---|
| 53 | LF = "\n",
|
---|
| 54 | CRLF = `\r${LF}`,
|
---|
| 55 | endsWithNewline = src.endsWith(LF);
|
---|
| 56 |
|
---|
| 57 | /*
|
---|
| 58 | * Empty source is always valid: No content in file so we don't
|
---|
| 59 | * need to lint for a newline on the last line of content.
|
---|
| 60 | */
|
---|
| 61 | if (!src.length) {
|
---|
| 62 | return;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | let mode = context.options[0] || "always",
|
---|
| 66 | appendCRLF = false;
|
---|
| 67 |
|
---|
| 68 | if (mode === "unix") {
|
---|
| 69 |
|
---|
| 70 | // `"unix"` should behave exactly as `"always"`
|
---|
| 71 | mode = "always";
|
---|
| 72 | }
|
---|
| 73 | if (mode === "windows") {
|
---|
| 74 |
|
---|
| 75 | // `"windows"` should behave exactly as `"always"`, but append CRLF in the fixer for backwards compatibility
|
---|
| 76 | mode = "always";
|
---|
| 77 | appendCRLF = true;
|
---|
| 78 | }
|
---|
| 79 | if (mode === "always" && !endsWithNewline) {
|
---|
| 80 |
|
---|
| 81 | // File is not newline-terminated, but should be
|
---|
| 82 | context.report({
|
---|
| 83 | node,
|
---|
| 84 | loc: location,
|
---|
| 85 | messageId: "missing",
|
---|
| 86 | fix(fixer) {
|
---|
| 87 | return fixer.insertTextAfterRange([0, src.length], appendCRLF ? CRLF : LF);
|
---|
| 88 | }
|
---|
| 89 | });
|
---|
| 90 | } else if (mode === "never" && endsWithNewline) {
|
---|
| 91 |
|
---|
| 92 | const secondLastLine = sourceCode.lines[sourceCode.lines.length - 2];
|
---|
| 93 |
|
---|
| 94 | // File is newline-terminated, but shouldn't be
|
---|
| 95 | context.report({
|
---|
| 96 | node,
|
---|
| 97 | loc: {
|
---|
| 98 | start: { line: sourceCode.lines.length - 1, column: secondLastLine.length },
|
---|
| 99 | end: { line: sourceCode.lines.length, column: 0 }
|
---|
| 100 | },
|
---|
| 101 | messageId: "unexpected",
|
---|
| 102 | fix(fixer) {
|
---|
| 103 | const finalEOLs = /(?:\r?\n)+$/u,
|
---|
| 104 | match = finalEOLs.exec(sourceCode.text),
|
---|
| 105 | start = match.index,
|
---|
| 106 | end = sourceCode.text.length;
|
---|
| 107 |
|
---|
| 108 | return fixer.replaceTextRange([start, end], "");
|
---|
| 109 | }
|
---|
| 110 | });
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | };
|
---|
| 114 | }
|
---|
| 115 | };
|
---|