linebreak-style.js 3.5 KB

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