2 * @fileoverview Rule to enforce a single linebreak style.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "enforce consistent linebreak style",
24 category: "Stylistic Issues",
26 url: "https://eslint.org/docs/rules/linebreak-style"
29 fixable: "whitespace",
33 enum: ["unix", "windows"]
37 expectedLF: "Expected linebreaks to be 'LF' but found 'CRLF'.",
38 expectedCRLF: "Expected linebreaks to be 'CRLF' but found 'LF'."
43 const sourceCode = context.getSourceCode();
45 //--------------------------------------------------------------------------
47 //--------------------------------------------------------------------------
50 * Builds a fix function that replaces text at the specified range in the source text.
51 * @param {int[]} range The range to replace
52 * @param {string} text The text to insert.
53 * @returns {Function} Fixer function
56 function createFix(range, text) {
57 return function(fixer) {
58 return fixer.replaceTextRange(range, text);
62 //--------------------------------------------------------------------------
64 //--------------------------------------------------------------------------
67 Program: function checkForLinebreakStyle(node) {
68 const linebreakStyle = context.options[0] || "unix",
69 expectedLF = linebreakStyle === "unix",
70 expectedLFChars = expectedLF ? "\n" : "\r\n",
71 source = sourceCode.getText(),
72 pattern = astUtils.createGlobalLinebreakMatcher();
77 while ((match = pattern.exec(source)) !== null) {
79 if (match[0] === expectedLFChars) {
83 const index = match.index;
84 const range = [index, index + match[0].length];
91 column: sourceCode.lines[i - 1].length
98 messageId: expectedLF ? "expectedLF" : "expectedCRLF",
99 fix: createFix(range, expectedLFChars)