Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

linebreak-style.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @fileoverview Rule to enforce a single linebreak style.
  3. * @author Erik Mueller
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "enforce consistent linebreak style",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/linebreak-style"
  21. },
  22. fixable: "whitespace",
  23. schema: [
  24. {
  25. enum: ["unix", "windows"]
  26. }
  27. ]
  28. },
  29. create(context) {
  30. const EXPECTED_LF_MSG = "Expected linebreaks to be 'LF' but found 'CRLF'.",
  31. EXPECTED_CRLF_MSG = "Expected linebreaks to be 'CRLF' but found 'LF'.";
  32. const sourceCode = context.getSourceCode();
  33. //--------------------------------------------------------------------------
  34. // Helpers
  35. //--------------------------------------------------------------------------
  36. /**
  37. * Builds a fix function that replaces text at the specified range in the source text.
  38. * @param {int[]} range The range to replace
  39. * @param {string} text The text to insert.
  40. * @returns {Function} Fixer function
  41. * @private
  42. */
  43. function createFix(range, text) {
  44. return function(fixer) {
  45. return fixer.replaceTextRange(range, text);
  46. };
  47. }
  48. //--------------------------------------------------------------------------
  49. // Public
  50. //--------------------------------------------------------------------------
  51. return {
  52. Program: function checkForlinebreakStyle(node) {
  53. const linebreakStyle = context.options[0] || "unix",
  54. expectedLF = linebreakStyle === "unix",
  55. expectedLFChars = expectedLF ? "\n" : "\r\n",
  56. source = sourceCode.getText(),
  57. pattern = astUtils.createGlobalLinebreakMatcher();
  58. let match;
  59. let i = 0;
  60. while ((match = pattern.exec(source)) !== null) {
  61. i++;
  62. if (match[0] === expectedLFChars) {
  63. continue;
  64. }
  65. const index = match.index;
  66. const range = [index, index + match[0].length];
  67. context.report({
  68. node,
  69. loc: {
  70. line: i,
  71. column: sourceCode.lines[i - 1].length
  72. },
  73. message: expectedLF ? EXPECTED_LF_MSG : EXPECTED_CRLF_MSG,
  74. fix: createFix(range, expectedLFChars)
  75. });
  76. }
  77. }
  78. };
  79. }
  80. };