1 | /**
|
---|
2 | * @fileoverview Rule to enforce a single linebreak style.
|
---|
3 | * @author Erik Mueller
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 | // Requirements
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | const astUtils = require("./utils/ast-utils");
|
---|
14 |
|
---|
15 | //------------------------------------------------------------------------------
|
---|
16 | // Rule Definition
|
---|
17 | //------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | /** @type {import('../shared/types').Rule} */
|
---|
20 | module.exports = {
|
---|
21 | meta: {
|
---|
22 | deprecated: true,
|
---|
23 | replacedBy: [],
|
---|
24 | type: "layout",
|
---|
25 |
|
---|
26 | docs: {
|
---|
27 | description: "Enforce consistent linebreak style",
|
---|
28 | recommended: false,
|
---|
29 | url: "https://eslint.org/docs/latest/rules/linebreak-style"
|
---|
30 | },
|
---|
31 |
|
---|
32 | fixable: "whitespace",
|
---|
33 |
|
---|
34 | schema: [
|
---|
35 | {
|
---|
36 | enum: ["unix", "windows"]
|
---|
37 | }
|
---|
38 | ],
|
---|
39 | messages: {
|
---|
40 | expectedLF: "Expected linebreaks to be 'LF' but found 'CRLF'.",
|
---|
41 | expectedCRLF: "Expected linebreaks to be 'CRLF' but found 'LF'."
|
---|
42 | }
|
---|
43 | },
|
---|
44 |
|
---|
45 | create(context) {
|
---|
46 | const sourceCode = context.sourceCode;
|
---|
47 |
|
---|
48 | //--------------------------------------------------------------------------
|
---|
49 | // Helpers
|
---|
50 | //--------------------------------------------------------------------------
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Builds a fix function that replaces text at the specified range in the source text.
|
---|
54 | * @param {int[]} range The range to replace
|
---|
55 | * @param {string} text The text to insert.
|
---|
56 | * @returns {Function} Fixer function
|
---|
57 | * @private
|
---|
58 | */
|
---|
59 | function createFix(range, text) {
|
---|
60 | return function(fixer) {
|
---|
61 | return fixer.replaceTextRange(range, text);
|
---|
62 | };
|
---|
63 | }
|
---|
64 |
|
---|
65 | //--------------------------------------------------------------------------
|
---|
66 | // Public
|
---|
67 | //--------------------------------------------------------------------------
|
---|
68 |
|
---|
69 | return {
|
---|
70 | Program: function checkForLinebreakStyle(node) {
|
---|
71 | const linebreakStyle = context.options[0] || "unix",
|
---|
72 | expectedLF = linebreakStyle === "unix",
|
---|
73 | expectedLFChars = expectedLF ? "\n" : "\r\n",
|
---|
74 | source = sourceCode.getText(),
|
---|
75 | pattern = astUtils.createGlobalLinebreakMatcher();
|
---|
76 | let match;
|
---|
77 |
|
---|
78 | let i = 0;
|
---|
79 |
|
---|
80 | while ((match = pattern.exec(source)) !== null) {
|
---|
81 | i++;
|
---|
82 | if (match[0] === expectedLFChars) {
|
---|
83 | continue;
|
---|
84 | }
|
---|
85 |
|
---|
86 | const index = match.index;
|
---|
87 | const range = [index, index + match[0].length];
|
---|
88 |
|
---|
89 | context.report({
|
---|
90 | node,
|
---|
91 | loc: {
|
---|
92 | start: {
|
---|
93 | line: i,
|
---|
94 | column: sourceCode.lines[i - 1].length
|
---|
95 | },
|
---|
96 | end: {
|
---|
97 | line: i + 1,
|
---|
98 | column: 0
|
---|
99 | }
|
---|
100 | },
|
---|
101 | messageId: expectedLF ? "expectedLF" : "expectedCRLF",
|
---|
102 | fix: createFix(range, expectedLFChars)
|
---|
103 | });
|
---|
104 | }
|
---|
105 | }
|
---|
106 | };
|
---|
107 | }
|
---|
108 | };
|
---|