lines-between-class-members.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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("./utils/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. * Return the last token among the consecutive tokens that have no exceed max line difference in between, before the first token in the next member.
  47. * @param {Token} prevLastToken The last token in the previous member node.
  48. * @param {Token} nextFirstToken The first token in the next member node.
  49. * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens.
  50. * @returns {Token} The last token among the consecutive tokens.
  51. */
  52. function findLastConsecutiveTokenAfter(prevLastToken, nextFirstToken, maxLine) {
  53. const after = sourceCode.getTokenAfter(prevLastToken, { includeComments: true });
  54. if (after !== nextFirstToken && after.loc.start.line - prevLastToken.loc.end.line <= maxLine) {
  55. return findLastConsecutiveTokenAfter(after, nextFirstToken, maxLine);
  56. }
  57. return prevLastToken;
  58. }
  59. /**
  60. * Return the first token among the consecutive tokens that have no exceed max line difference in between, after the last token in the previous member.
  61. * @param {Token} nextFirstToken The first token in the next member node.
  62. * @param {Token} prevLastToken The last token in the previous member node.
  63. * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens.
  64. * @returns {Token} The first token among the consecutive tokens.
  65. */
  66. function findFirstConsecutiveTokenBefore(nextFirstToken, prevLastToken, maxLine) {
  67. const before = sourceCode.getTokenBefore(nextFirstToken, { includeComments: true });
  68. if (before !== prevLastToken && nextFirstToken.loc.start.line - before.loc.end.line <= maxLine) {
  69. return findFirstConsecutiveTokenBefore(before, prevLastToken, maxLine);
  70. }
  71. return nextFirstToken;
  72. }
  73. /**
  74. * Checks if there is a token or comment between two tokens.
  75. * @param {Token} before The token before.
  76. * @param {Token} after The token after.
  77. * @returns {boolean} True if there is a token or comment between two tokens.
  78. */
  79. function hasTokenOrCommentBetween(before, after) {
  80. return sourceCode.getTokensBetween(before, after, { includeComments: true }).length !== 0;
  81. }
  82. return {
  83. ClassBody(node) {
  84. const body = node.body;
  85. for (let i = 0; i < body.length - 1; i++) {
  86. const curFirst = sourceCode.getFirstToken(body[i]);
  87. const curLast = sourceCode.getLastToken(body[i]);
  88. const nextFirst = sourceCode.getFirstToken(body[i + 1]);
  89. const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
  90. const skip = !isMulti && options[1].exceptAfterSingleLine;
  91. const beforePadding = findLastConsecutiveTokenAfter(curLast, nextFirst, 1);
  92. const afterPadding = findFirstConsecutiveTokenBefore(nextFirst, curLast, 1);
  93. const isPadded = afterPadding.loc.start.line - beforePadding.loc.end.line > 1;
  94. const hasTokenInPadding = hasTokenOrCommentBetween(beforePadding, afterPadding);
  95. const curLineLastToken = findLastConsecutiveTokenAfter(curLast, nextFirst, 0);
  96. if ((options[0] === "always" && !skip && !isPadded) ||
  97. (options[0] === "never" && isPadded)) {
  98. context.report({
  99. node: body[i + 1],
  100. messageId: isPadded ? "never" : "always",
  101. fix(fixer) {
  102. if (hasTokenInPadding) {
  103. return null;
  104. }
  105. return isPadded
  106. ? fixer.replaceTextRange([beforePadding.range[1], afterPadding.range[0]], "\n")
  107. : fixer.insertTextAfter(curLineLastToken, "\n");
  108. }
  109. });
  110. }
  111. }
  112. }
  113. };
  114. }
  115. };