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.

func-call-spacing.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @fileoverview Rule to control spacing within function calls
  3. * @author Matt DuVall <http://www.mattduvall.com>
  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: "require or disallow spacing between function identifiers and their invocations",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/func-call-spacing"
  21. },
  22. fixable: "whitespace",
  23. schema: {
  24. anyOf: [
  25. {
  26. type: "array",
  27. items: [
  28. {
  29. enum: ["never"]
  30. }
  31. ],
  32. minItems: 0,
  33. maxItems: 1
  34. },
  35. {
  36. type: "array",
  37. items: [
  38. {
  39. enum: ["always"]
  40. },
  41. {
  42. type: "object",
  43. properties: {
  44. allowNewlines: {
  45. type: "boolean"
  46. }
  47. },
  48. additionalProperties: false
  49. }
  50. ],
  51. minItems: 0,
  52. maxItems: 2
  53. }
  54. ]
  55. },
  56. messages: {
  57. unexpected: "Unexpected newline between function name and paren.",
  58. missing: "Missing space between function name and paren."
  59. }
  60. },
  61. create(context) {
  62. const never = context.options[0] !== "always";
  63. const allowNewlines = !never && context.options[1] && context.options[1].allowNewlines;
  64. const sourceCode = context.getSourceCode();
  65. const text = sourceCode.getText();
  66. /**
  67. * Check if open space is present in a function name
  68. * @param {ASTNode} node node to evaluate
  69. * @returns {void}
  70. * @private
  71. */
  72. function checkSpacing(node) {
  73. const lastToken = sourceCode.getLastToken(node);
  74. const lastCalleeToken = sourceCode.getLastToken(node.callee);
  75. const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken);
  76. const prevToken = parenToken && sourceCode.getTokenBefore(parenToken);
  77. // Parens in NewExpression are optional
  78. if (!(parenToken && parenToken.range[1] < node.range[1])) {
  79. return;
  80. }
  81. const textBetweenTokens = text.slice(prevToken.range[1], parenToken.range[0]).replace(/\/\*.*?\*\//g, "");
  82. const hasWhitespace = /\s/.test(textBetweenTokens);
  83. const hasNewline = hasWhitespace && astUtils.LINEBREAK_MATCHER.test(textBetweenTokens);
  84. /*
  85. * never allowNewlines hasWhitespace hasNewline message
  86. * F F F F Missing space between function name and paren.
  87. * F F F T (Invalid `!hasWhitespace && hasNewline`)
  88. * F F T T Unexpected newline between function name and paren.
  89. * F F T F (OK)
  90. * F T T F (OK)
  91. * F T T T (OK)
  92. * F T F T (Invalid `!hasWhitespace && hasNewline`)
  93. * F T F F Missing space between function name and paren.
  94. * T T F F (Invalid `never && allowNewlines`)
  95. * T T F T (Invalid `!hasWhitespace && hasNewline`)
  96. * T T T T (Invalid `never && allowNewlines`)
  97. * T T T F (Invalid `never && allowNewlines`)
  98. * T F T F Unexpected space between function name and paren.
  99. * T F T T Unexpected space between function name and paren.
  100. * T F F T (Invalid `!hasWhitespace && hasNewline`)
  101. * T F F F (OK)
  102. *
  103. * T T Unexpected space between function name and paren.
  104. * F F Missing space between function name and paren.
  105. * F F T Unexpected newline between function name and paren.
  106. */
  107. if (never && hasWhitespace) {
  108. context.report({
  109. node,
  110. loc: lastCalleeToken.loc.start,
  111. messageId: "unexpected",
  112. fix(fixer) {
  113. /*
  114. * Only autofix if there is no newline
  115. * https://github.com/eslint/eslint/issues/7787
  116. */
  117. if (!hasNewline) {
  118. return fixer.removeRange([prevToken.range[1], parenToken.range[0]]);
  119. }
  120. return null;
  121. }
  122. });
  123. } else if (!never && !hasWhitespace) {
  124. context.report({
  125. node,
  126. loc: lastCalleeToken.loc.start,
  127. messageId: "missing",
  128. fix(fixer) {
  129. return fixer.insertTextBefore(parenToken, " ");
  130. }
  131. });
  132. } else if (!never && !allowNewlines && hasNewline) {
  133. context.report({
  134. node,
  135. loc: lastCalleeToken.loc.start,
  136. messageId: "unexpected",
  137. fix(fixer) {
  138. return fixer.replaceTextRange([prevToken.range[1], parenToken.range[0]], " ");
  139. }
  140. });
  141. }
  142. }
  143. return {
  144. CallExpression: checkSpacing,
  145. NewExpression: checkSpacing
  146. };
  147. }
  148. };