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.

lines-between-class-members.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @fileoverview Rule to check empty newline between class members
  3. * @author 薛定谔的猫<hh_2013@foxmail.com>
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "require or disallow an empty line between class members",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/lines-between-class-members"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. enum: ["always", "never"]
  23. },
  24. {
  25. type: "object",
  26. properties: {
  27. exceptAfterSingleLine: {
  28. type: "boolean"
  29. }
  30. },
  31. additionalProperties: false
  32. }
  33. ]
  34. },
  35. create(context) {
  36. const options = [];
  37. options[0] = context.options[0] || "always";
  38. options[1] = context.options[1] || { exceptAfterSingleLine: false };
  39. const ALWAYS_MESSAGE = "Expected blank line between class members.";
  40. const NEVER_MESSAGE = "Unexpected blank line between class members.";
  41. const sourceCode = context.getSourceCode();
  42. /**
  43. * Checks if there is padding between two tokens
  44. * @param {Token} first The first token
  45. * @param {Token} second The second token
  46. * @returns {boolean} True if there is at least a line between the tokens
  47. */
  48. function isPaddingBetweenTokens(first, second) {
  49. const comments = sourceCode.getCommentsBefore(second);
  50. const len = comments.length;
  51. // If there is no comments
  52. if (len === 0) {
  53. const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;
  54. return linesBetweenFstAndSnd >= 1;
  55. }
  56. // If there are comments
  57. let sumOfCommentLines = 0; // the numbers of lines of comments
  58. let prevCommentLineNum = -1; // line number of the end of the previous comment
  59. for (let i = 0; i < len; i++) {
  60. const commentLinesOfThisComment = comments[i].loc.end.line - comments[i].loc.start.line + 1;
  61. sumOfCommentLines += commentLinesOfThisComment;
  62. /*
  63. * If this comment and the previous comment are in the same line,
  64. * the count of comment lines is duplicated. So decrement sumOfCommentLines.
  65. */
  66. if (prevCommentLineNum === comments[i].loc.start.line) {
  67. sumOfCommentLines -= 1;
  68. }
  69. prevCommentLineNum = comments[i].loc.end.line;
  70. }
  71. /*
  72. * If the first block and the first comment are in the same line,
  73. * the count of comment lines is duplicated. So decrement sumOfCommentLines.
  74. */
  75. if (first.loc.end.line === comments[0].loc.start.line) {
  76. sumOfCommentLines -= 1;
  77. }
  78. /*
  79. * If the last comment and the second block are in the same line,
  80. * the count of comment lines is duplicated. So decrement sumOfCommentLines.
  81. */
  82. if (comments[len - 1].loc.end.line === second.loc.start.line) {
  83. sumOfCommentLines -= 1;
  84. }
  85. const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;
  86. return linesBetweenFstAndSnd - sumOfCommentLines >= 1;
  87. }
  88. return {
  89. ClassBody(node) {
  90. const body = node.body;
  91. for (let i = 0; i < body.length - 1; i++) {
  92. const curFirst = sourceCode.getFirstToken(body[i]);
  93. const curLast = sourceCode.getLastToken(body[i]);
  94. const nextFirst = sourceCode.getFirstToken(body[i + 1]);
  95. const isPadded = isPaddingBetweenTokens(curLast, nextFirst);
  96. const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
  97. const skip = !isMulti && options[1].exceptAfterSingleLine;
  98. if ((options[0] === "always" && !skip && !isPadded) ||
  99. (options[0] === "never" && isPadded)) {
  100. context.report({
  101. node: body[i + 1],
  102. message: isPadded ? NEVER_MESSAGE : ALWAYS_MESSAGE,
  103. fix(fixer) {
  104. return isPadded
  105. ? fixer.replaceTextRange([curLast.range[1], nextFirst.range[0]], "\n")
  106. : fixer.insertTextAfter(curLast, "\n");
  107. }
  108. });
  109. }
  110. }
  111. }
  112. };
  113. }
  114. };