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.

yield-star-spacing.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "require or disallow spacing around the `*` in `yield*` expressions",
  14. category: "ECMAScript 6",
  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. },
  37. create(context) {
  38. const sourceCode = context.getSourceCode();
  39. const mode = (function(option) {
  40. if (!option || typeof option === "string") {
  41. return {
  42. before: { before: true, after: false },
  43. after: { before: false, after: true },
  44. both: { before: true, after: true },
  45. neither: { before: false, after: false }
  46. }[option || "after"];
  47. }
  48. return option;
  49. }(context.options[0]));
  50. /**
  51. * Checks the spacing between two tokens before or after the star token.
  52. * @param {string} side Either "before" or "after".
  53. * @param {Token} leftToken `function` keyword token if side is "before", or
  54. * star token if side is "after".
  55. * @param {Token} rightToken Star token if side is "before", or identifier
  56. * token if side is "after".
  57. * @returns {void}
  58. */
  59. function checkSpacing(side, leftToken, rightToken) {
  60. if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken) !== mode[side]) {
  61. const after = leftToken.value === "*";
  62. const spaceRequired = mode[side];
  63. const node = after ? leftToken : rightToken;
  64. const type = spaceRequired ? "Missing" : "Unexpected";
  65. const message = "{{type}} space {{side}} *.";
  66. context.report({
  67. node,
  68. message,
  69. data: {
  70. type,
  71. side
  72. },
  73. fix(fixer) {
  74. if (spaceRequired) {
  75. if (after) {
  76. return fixer.insertTextAfter(node, " ");
  77. }
  78. return fixer.insertTextBefore(node, " ");
  79. }
  80. return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
  81. }
  82. });
  83. }
  84. }
  85. /**
  86. * Enforces the spacing around the star if node is a yield* expression.
  87. * @param {ASTNode} node A yield expression node.
  88. * @returns {void}
  89. */
  90. function checkExpression(node) {
  91. if (!node.delegate) {
  92. return;
  93. }
  94. const tokens = sourceCode.getFirstTokens(node, 3);
  95. const yieldToken = tokens[0];
  96. const starToken = tokens[1];
  97. const nextToken = tokens[2];
  98. checkSpacing("before", yieldToken, starToken);
  99. checkSpacing("after", starToken, nextToken);
  100. }
  101. return {
  102. YieldExpression: checkExpression
  103. };
  104. }
  105. };