lines-between-class-members.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. default: false
  30. }
  31. },
  32. additionalProperties: false
  33. }
  34. ],
  35. messages: {
  36. never: "Unexpected blank line between class members.",
  37. always: "Expected blank line between class members."
  38. }
  39. },
  40. create(context) {
  41. const options = [];
  42. options[0] = context.options[0] || "always";
  43. options[1] = context.options[1] || { exceptAfterSingleLine: false };
  44. const sourceCode = context.getSourceCode();
  45. /**
  46. * Checks if there is padding between two tokens
  47. * @param {Token} first The first token
  48. * @param {Token} second The second token
  49. * @returns {boolean} True if there is at least a line between the tokens
  50. */
  51. function isPaddingBetweenTokens(first, second) {
  52. const comments = sourceCode.getCommentsBefore(second);
  53. const len = comments.length;
  54. // If there is no comments
  55. if (len === 0) {
  56. const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;
  57. return linesBetweenFstAndSnd >= 1;
  58. }
  59. // If there are comments
  60. let sumOfCommentLines = 0; // the numbers of lines of comments
  61. let prevCommentLineNum = -1; // line number of the end of the previous comment
  62. for (let i = 0; i < len; i++) {
  63. const commentLinesOfThisComment = comments[i].loc.end.line - comments[i].loc.start.line + 1;
  64. sumOfCommentLines += commentLinesOfThisComment;
  65. /*
  66. * If this comment and the previous comment are in the same line,
  67. * the count of comment lines is duplicated. So decrement sumOfCommentLines.
  68. */
  69. if (prevCommentLineNum === comments[i].loc.start.line) {
  70. sumOfCommentLines -= 1;
  71. }
  72. prevCommentLineNum = comments[i].loc.end.line;
  73. }
  74. /*
  75. * If the first block and the first comment are in the same line,
  76. * the count of comment lines is duplicated. So decrement sumOfCommentLines.
  77. */
  78. if (first.loc.end.line === comments[0].loc.start.line) {
  79. sumOfCommentLines -= 1;
  80. }
  81. /*
  82. * If the last comment and the second block are in the same line,
  83. * the count of comment lines is duplicated. So decrement sumOfCommentLines.
  84. */
  85. if (comments[len - 1].loc.end.line === second.loc.start.line) {
  86. sumOfCommentLines -= 1;
  87. }
  88. const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;
  89. return linesBetweenFstAndSnd - sumOfCommentLines >= 1;
  90. }
  91. return {
  92. ClassBody(node) {
  93. const body = node.body;
  94. for (let i = 0; i < body.length - 1; i++) {
  95. const curFirst = sourceCode.getFirstToken(body[i]);
  96. const curLast = sourceCode.getLastToken(body[i]);
  97. const nextFirst = sourceCode.getFirstToken(body[i + 1]);
  98. const isPadded = isPaddingBetweenTokens(curLast, nextFirst);
  99. const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
  100. const skip = !isMulti && options[1].exceptAfterSingleLine;
  101. if ((options[0] === "always" && !skip && !isPadded) ||
  102. (options[0] === "never" && isPadded)) {
  103. context.report({
  104. node: body[i + 1],
  105. messageId: isPadded ? "never" : "always",
  106. fix(fixer) {
  107. return isPadded
  108. ? fixer.replaceTextRange([curLast.range[1], nextFirst.range[0]], "\n")
  109. : fixer.insertTextAfter(curLast, "\n");
  110. }
  111. });
  112. }
  113. }
  114. }
  115. };
  116. }
  117. };