]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/eol-last.js
2 * @fileoverview Require or disallow newline at the end of files
3 * @author Nodeca Team <https://github.com/nodeca>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 /** @type {import('../shared/types').Rule} */
17 description
: "Require or disallow newline at the end of files",
19 url
: "https://eslint.org/docs/latest/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
.sourceCode
,
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 const secondLastLine
= sourceCode
.lines
[sourceCode
.lines
.length
- 2];
91 // File is newline-terminated, but shouldn't be
95 start
: { line
: sourceCode
.lines
.length
- 1, column
: secondLastLine
.length
},
96 end
: { line
: sourceCode
.lines
.length
, column
: 0 }
98 messageId
: "unexpected",
100 const finalEOLs
= /(?:\r?\n)+$/u,
101 match
= finalEOLs
.exec(sourceCode
.text
),
103 end
= sourceCode
.text
.length
;
105 return fixer
.replaceTextRange([start
, end
], "");