lines-between-class-members.js 5.0 KB

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