yield-star-spacing.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @fileoverview Rule to check the spacing around the * in yield* expressions.
  3. * @author Bryan Smith
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "Require or disallow spacing around the `*` in `yield*` expressions",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/yield-star-spacing"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. {
  21. oneOf: [
  22. {
  23. enum: ["before", "after", "both", "neither"]
  24. },
  25. {
  26. type: "object",
  27. properties: {
  28. before: { type: "boolean" },
  29. after: { type: "boolean" }
  30. },
  31. additionalProperties: false
  32. }
  33. ]
  34. }
  35. ],
  36. messages: {
  37. missingBefore: "Missing space before *.",
  38. missingAfter: "Missing space after *.",
  39. unexpectedBefore: "Unexpected space before *.",
  40. unexpectedAfter: "Unexpected space after *."
  41. }
  42. },
  43. create(context) {
  44. const sourceCode = context.getSourceCode();
  45. const mode = (function(option) {
  46. if (!option || typeof option === "string") {
  47. return {
  48. before: { before: true, after: false },
  49. after: { before: false, after: true },
  50. both: { before: true, after: true },
  51. neither: { before: false, after: false }
  52. }[option || "after"];
  53. }
  54. return option;
  55. }(context.options[0]));
  56. /**
  57. * Checks the spacing between two tokens before or after the star token.
  58. * @param {string} side Either "before" or "after".
  59. * @param {Token} leftToken `function` keyword token if side is "before", or
  60. * star token if side is "after".
  61. * @param {Token} rightToken Star token if side is "before", or identifier
  62. * token if side is "after".
  63. * @returns {void}
  64. */
  65. function checkSpacing(side, leftToken, rightToken) {
  66. if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken) !== mode[side]) {
  67. const after = leftToken.value === "*";
  68. const spaceRequired = mode[side];
  69. const node = after ? leftToken : rightToken;
  70. let messageId = "";
  71. if (spaceRequired) {
  72. messageId = side === "before" ? "missingBefore" : "missingAfter";
  73. } else {
  74. messageId = side === "before" ? "unexpectedBefore" : "unexpectedAfter";
  75. }
  76. context.report({
  77. node,
  78. messageId,
  79. fix(fixer) {
  80. if (spaceRequired) {
  81. if (after) {
  82. return fixer.insertTextAfter(node, " ");
  83. }
  84. return fixer.insertTextBefore(node, " ");
  85. }
  86. return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
  87. }
  88. });
  89. }
  90. }
  91. /**
  92. * Enforces the spacing around the star if node is a yield* expression.
  93. * @param {ASTNode} node A yield expression node.
  94. * @returns {void}
  95. */
  96. function checkExpression(node) {
  97. if (!node.delegate) {
  98. return;
  99. }
  100. const tokens = sourceCode.getFirstTokens(node, 3);
  101. const yieldToken = tokens[0];
  102. const starToken = tokens[1];
  103. const nextToken = tokens[2];
  104. checkSpacing("before", yieldToken, starToken);
  105. checkSpacing("after", starToken, nextToken);
  106. }
  107. return {
  108. YieldExpression: checkExpression
  109. };
  110. }
  111. };