2 * @fileoverview Rule to control spacing within function calls
3 * @author Matt DuVall <http://www.mattduvall.com>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "require or disallow spacing between function identifiers and their invocations",
24 category: "Stylistic Issues",
26 url: "https://eslint.org/docs/rules/func-call-spacing"
29 fixable: "whitespace",
56 additionalProperties: false
66 unexpected: "Unexpected newline between function name and paren.",
67 missing: "Missing space between function name and paren."
73 const never = context.options[0] !== "always";
74 const allowNewlines = !never && context.options[1] && context.options[1].allowNewlines;
75 const sourceCode = context.getSourceCode();
76 const text = sourceCode.getText();
79 * Check if open space is present in a function name
80 * @param {ASTNode} node node to evaluate
81 * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee.
82 * @param {Token} rightToken Tha first token of the arguments. this is the opening parenthesis that encloses the arguments.
86 function checkSpacing(node, leftToken, rightToken) {
87 const textBetweenTokens = text.slice(leftToken.range[1], rightToken.range[0]).replace(/\/\*.*?\*\//gu, "");
88 const hasWhitespace = /\s/u.test(textBetweenTokens);
89 const hasNewline = hasWhitespace && astUtils.LINEBREAK_MATCHER.test(textBetweenTokens);
92 * never allowNewlines hasWhitespace hasNewline message
93 * F F F F Missing space between function name and paren.
94 * F F F T (Invalid `!hasWhitespace && hasNewline`)
95 * F F T T Unexpected newline between function name and paren.
99 * F T F T (Invalid `!hasWhitespace && hasNewline`)
100 * F T F F Missing space between function name and paren.
101 * T T F F (Invalid `never && allowNewlines`)
102 * T T F T (Invalid `!hasWhitespace && hasNewline`)
103 * T T T T (Invalid `never && allowNewlines`)
104 * T T T F (Invalid `never && allowNewlines`)
105 * T F T F Unexpected space between function name and paren.
106 * T F T T Unexpected space between function name and paren.
107 * T F F T (Invalid `!hasWhitespace && hasNewline`)
110 * T T Unexpected space between function name and paren.
111 * F F Missing space between function name and paren.
112 * F F T Unexpected newline between function name and paren.
115 if (never && hasWhitespace) {
118 loc: leftToken.loc.start,
119 messageId: "unexpected",
123 * Only autofix if there is no newline
124 * https://github.com/eslint/eslint/issues/7787
127 return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
133 } else if (!never && !hasWhitespace) {
136 loc: leftToken.loc.start,
137 messageId: "missing",
139 return fixer.insertTextBefore(rightToken, " ");
142 } else if (!never && !allowNewlines && hasNewline) {
145 loc: leftToken.loc.start,
146 messageId: "unexpected",
148 return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ");
155 "CallExpression, NewExpression"(node) {
156 const lastToken = sourceCode.getLastToken(node);
157 const lastCalleeToken = sourceCode.getLastToken(node.callee);
158 const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken);
159 const prevToken = parenToken && sourceCode.getTokenBefore(parenToken);
161 // Parens in NewExpression are optional
162 if (!(parenToken && parenToken.range[1] < node.range[1])) {
166 checkSpacing(node, prevToken, parenToken);
169 ImportExpression(node) {
170 const leftToken = sourceCode.getFirstToken(node);
171 const rightToken = sourceCode.getTokenAfter(leftToken);
173 checkSpacing(node, leftToken, rightToken);