func-call-spacing.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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("./utils/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. * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee.
  70. * @param {Token} rightToken Tha first token of the arguments. this is the opening parenthesis that encloses the arguments.
  71. * @returns {void}
  72. * @private
  73. */
  74. function checkSpacing(node, leftToken, rightToken) {
  75. const textBetweenTokens = text.slice(leftToken.range[1], rightToken.range[0]).replace(/\/\*.*?\*\//gu, "");
  76. const hasWhitespace = /\s/u.test(textBetweenTokens);
  77. const hasNewline = hasWhitespace && astUtils.LINEBREAK_MATCHER.test(textBetweenTokens);
  78. /*
  79. * never allowNewlines hasWhitespace hasNewline message
  80. * F F F F Missing space between function name and paren.
  81. * F F F T (Invalid `!hasWhitespace && hasNewline`)
  82. * F F T T Unexpected newline between function name and paren.
  83. * F F T F (OK)
  84. * F T T F (OK)
  85. * F T T T (OK)
  86. * F T F T (Invalid `!hasWhitespace && hasNewline`)
  87. * F T F F Missing space between function name and paren.
  88. * T T F F (Invalid `never && allowNewlines`)
  89. * T T F T (Invalid `!hasWhitespace && hasNewline`)
  90. * T T T T (Invalid `never && allowNewlines`)
  91. * T T T F (Invalid `never && allowNewlines`)
  92. * T F T F Unexpected space between function name and paren.
  93. * T F T T Unexpected space between function name and paren.
  94. * T F F T (Invalid `!hasWhitespace && hasNewline`)
  95. * T F F F (OK)
  96. *
  97. * T T Unexpected space between function name and paren.
  98. * F F Missing space between function name and paren.
  99. * F F T Unexpected newline between function name and paren.
  100. */
  101. if (never && hasWhitespace) {
  102. context.report({
  103. node,
  104. loc: leftToken.loc.start,
  105. messageId: "unexpected",
  106. fix(fixer) {
  107. /*
  108. * Only autofix if there is no newline
  109. * https://github.com/eslint/eslint/issues/7787
  110. */
  111. if (!hasNewline) {
  112. return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
  113. }
  114. return null;
  115. }
  116. });
  117. } else if (!never && !hasWhitespace) {
  118. context.report({
  119. node,
  120. loc: leftToken.loc.start,
  121. messageId: "missing",
  122. fix(fixer) {
  123. return fixer.insertTextBefore(rightToken, " ");
  124. }
  125. });
  126. } else if (!never && !allowNewlines && hasNewline) {
  127. context.report({
  128. node,
  129. loc: leftToken.loc.start,
  130. messageId: "unexpected",
  131. fix(fixer) {
  132. return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ");
  133. }
  134. });
  135. }
  136. }
  137. return {
  138. "CallExpression, NewExpression"(node) {
  139. const lastToken = sourceCode.getLastToken(node);
  140. const lastCalleeToken = sourceCode.getLastToken(node.callee);
  141. const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken);
  142. const prevToken = parenToken && sourceCode.getTokenBefore(parenToken);
  143. // Parens in NewExpression are optional
  144. if (!(parenToken && parenToken.range[1] < node.range[1])) {
  145. return;
  146. }
  147. checkSpacing(node, prevToken, parenToken);
  148. },
  149. ImportExpression(node) {
  150. const leftToken = sourceCode.getFirstToken(node);
  151. const rightToken = sourceCode.getTokenAfter(leftToken);
  152. checkSpacing(node, leftToken, rightToken);
  153. }
  154. };
  155. }
  156. };