项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

98 lines
3.1 KiB

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