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.

rest-spread-spacing.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @fileoverview Enforce spacing between rest and spread operators and their expressions.
  3. * @author Kai Cataldo
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "enforce spacing between rest and spread operators and their expressions",
  14. category: "ECMAScript 6",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/rest-spread-spacing"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. {
  21. enum: ["always", "never"]
  22. }
  23. ]
  24. },
  25. create(context) {
  26. const sourceCode = context.getSourceCode(),
  27. alwaysSpace = context.options[0] === "always";
  28. //--------------------------------------------------------------------------
  29. // Helpers
  30. //--------------------------------------------------------------------------
  31. /**
  32. * Checks whitespace between rest/spread operators and their expressions
  33. * @param {ASTNode} node - The node to check
  34. * @returns {void}
  35. */
  36. function checkWhiteSpace(node) {
  37. const operator = sourceCode.getFirstToken(node),
  38. nextToken = sourceCode.getTokenAfter(operator),
  39. hasWhitespace = sourceCode.isSpaceBetweenTokens(operator, nextToken);
  40. let type;
  41. switch (node.type) {
  42. case "SpreadElement":
  43. type = "spread";
  44. if (node.parent.type === "ObjectExpression") {
  45. type += " property";
  46. }
  47. break;
  48. case "RestElement":
  49. type = "rest";
  50. if (node.parent.type === "ObjectPattern") {
  51. type += " property";
  52. }
  53. break;
  54. case "ExperimentalSpreadProperty":
  55. type = "spread property";
  56. break;
  57. case "ExperimentalRestProperty":
  58. type = "rest property";
  59. break;
  60. default:
  61. return;
  62. }
  63. if (alwaysSpace && !hasWhitespace) {
  64. context.report({
  65. node,
  66. loc: {
  67. line: operator.loc.end.line,
  68. column: operator.loc.end.column
  69. },
  70. message: "Expected whitespace after {{type}} operator.",
  71. data: {
  72. type
  73. },
  74. fix(fixer) {
  75. return fixer.replaceTextRange([operator.range[1], nextToken.range[0]], " ");
  76. }
  77. });
  78. } else if (!alwaysSpace && hasWhitespace) {
  79. context.report({
  80. node,
  81. loc: {
  82. line: operator.loc.end.line,
  83. column: operator.loc.end.column
  84. },
  85. message: "Unexpected whitespace after {{type}} operator.",
  86. data: {
  87. type
  88. },
  89. fix(fixer) {
  90. return fixer.removeRange([operator.range[1], nextToken.range[0]]);
  91. }
  92. });
  93. }
  94. }
  95. //--------------------------------------------------------------------------
  96. // Public
  97. //--------------------------------------------------------------------------
  98. return {
  99. SpreadElement: checkWhiteSpace,
  100. RestElement: checkWhiteSpace,
  101. ExperimentalSpreadProperty: checkWhiteSpace,
  102. ExperimentalRestProperty: checkWhiteSpace
  103. };
  104. }
  105. };