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.

space-before-function-paren.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @fileoverview Rule to validate spacing before function paren.
  3. * @author Mathias Schreck <https://github.com/lo1tuma>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "enforce consistent spacing before `function` definition opening parenthesis",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/space-before-function-paren"
  21. },
  22. fixable: "whitespace",
  23. schema: [
  24. {
  25. oneOf: [
  26. {
  27. enum: ["always", "never"]
  28. },
  29. {
  30. type: "object",
  31. properties: {
  32. anonymous: {
  33. enum: ["always", "never", "ignore"]
  34. },
  35. named: {
  36. enum: ["always", "never", "ignore"]
  37. },
  38. asyncArrow: {
  39. enum: ["always", "never", "ignore"]
  40. }
  41. },
  42. additionalProperties: false
  43. }
  44. ]
  45. }
  46. ]
  47. },
  48. create(context) {
  49. const sourceCode = context.getSourceCode();
  50. const baseConfig = typeof context.options[0] === "string" ? context.options[0] : "always";
  51. const overrideConfig = typeof context.options[0] === "object" ? context.options[0] : {};
  52. /**
  53. * Determines whether a function has a name.
  54. * @param {ASTNode} node The function node.
  55. * @returns {boolean} Whether the function has a name.
  56. */
  57. function isNamedFunction(node) {
  58. if (node.id) {
  59. return true;
  60. }
  61. const parent = node.parent;
  62. return parent.type === "MethodDefinition" ||
  63. (parent.type === "Property" &&
  64. (
  65. parent.kind === "get" ||
  66. parent.kind === "set" ||
  67. parent.method
  68. )
  69. );
  70. }
  71. /**
  72. * Gets the config for a given function
  73. * @param {ASTNode} node The function node
  74. * @returns {string} "always", "never", or "ignore"
  75. */
  76. function getConfigForFunction(node) {
  77. if (node.type === "ArrowFunctionExpression") {
  78. // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
  79. if (node.async && astUtils.isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 }))) {
  80. return overrideConfig.asyncArrow || baseConfig;
  81. }
  82. } else if (isNamedFunction(node)) {
  83. return overrideConfig.named || baseConfig;
  84. // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
  85. } else if (!node.generator) {
  86. return overrideConfig.anonymous || baseConfig;
  87. }
  88. return "ignore";
  89. }
  90. /**
  91. * Checks the parens of a function node
  92. * @param {ASTNode} node A function node
  93. * @returns {void}
  94. */
  95. function checkFunction(node) {
  96. const functionConfig = getConfigForFunction(node);
  97. if (functionConfig === "ignore") {
  98. return;
  99. }
  100. const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
  101. const leftToken = sourceCode.getTokenBefore(rightToken);
  102. const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);
  103. if (hasSpacing && functionConfig === "never") {
  104. context.report({
  105. node,
  106. loc: leftToken.loc.end,
  107. message: "Unexpected space before function parentheses.",
  108. fix: fixer => fixer.removeRange([leftToken.range[1], rightToken.range[0]])
  109. });
  110. } else if (!hasSpacing && functionConfig === "always") {
  111. context.report({
  112. node,
  113. loc: leftToken.loc.end,
  114. message: "Missing space before function parentheses.",
  115. fix: fixer => fixer.insertTextAfter(leftToken, " ")
  116. });
  117. }
  118. }
  119. return {
  120. ArrowFunctionExpression: checkFunction,
  121. FunctionDeclaration: checkFunction,
  122. FunctionExpression: checkFunction
  123. };
  124. }
  125. };