]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/func-call-spacing.js
132a5833143844259f8301576c2698af5e385df2
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 unexpectedWhitespace
: "Unexpected whitespace between function name and paren.",
67 unexpectedNewline
: "Unexpected newline between function name and paren.",
68 missing
: "Missing space between function name and paren."
74 const never
= context
.options
[0] !== "always";
75 const allowNewlines
= !never
&& context
.options
[1] && context
.options
[1].allowNewlines
;
76 const sourceCode
= context
.getSourceCode();
77 const text
= sourceCode
.getText();
80 * Check if open space is present in a function name
81 * @param {ASTNode} node node to evaluate
82 * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee.
83 * @param {Token} rightToken Tha first token of the arguments. this is the opening parenthesis that encloses the arguments.
87 function checkSpacing(node
, leftToken
, rightToken
) {
88 const textBetweenTokens
= text
.slice(leftToken
.range
[1], rightToken
.range
[0]).replace(/\/\*.*?\*\//gu, "");
89 const hasWhitespace
= /\s/u.test(textBetweenTokens
);
90 const hasNewline
= hasWhitespace
&& astUtils
.LINEBREAK_MATCHER
.test(textBetweenTokens
);
93 * never allowNewlines hasWhitespace hasNewline message
94 * F F F F Missing space between function name and paren.
95 * F F F T (Invalid `!hasWhitespace && hasNewline`)
96 * F F T T Unexpected newline between function name and paren.
100 * F T F T (Invalid `!hasWhitespace && hasNewline`)
101 * F T F F Missing space between function name and paren.
102 * T T F F (Invalid `never && allowNewlines`)
103 * T T F T (Invalid `!hasWhitespace && hasNewline`)
104 * T T T T (Invalid `never && allowNewlines`)
105 * T T T F (Invalid `never && allowNewlines`)
106 * T F T F Unexpected space between function name and paren.
107 * T F T T Unexpected space between function name and paren.
108 * T F F T (Invalid `!hasWhitespace && hasNewline`)
111 * T T Unexpected space between function name and paren.
112 * F F Missing space between function name and paren.
113 * F F T Unexpected newline between function name and paren.
116 if (never
&& hasWhitespace
) {
120 start
: leftToken
.loc
.end
,
122 line
: rightToken
.loc
.start
.line
,
123 column
: rightToken
.loc
.start
.column
- 1
126 messageId
: "unexpectedWhitespace",
129 // Don't remove comments.
130 if (sourceCode
.commentsExistBetween(leftToken
, rightToken
)) {
134 // If `?.` exists, it doesn't hide no-unexpected-multiline errors
136 return fixer
.replaceTextRange([leftToken
.range
[1], rightToken
.range
[0]], "?.");
140 * Only autofix if there is no newline
141 * https://github.com/eslint/eslint/issues/7787
146 return fixer
.removeRange([leftToken
.range
[1], rightToken
.range
[0]]);
149 } else if (!never
&& !hasWhitespace
) {
154 line
: leftToken
.loc
.end
.line
,
155 column
: leftToken
.loc
.end
.column
- 1
157 end
: rightToken
.loc
.start
159 messageId
: "missing",
162 return null; // Not sure if inserting a space to either before/after `?.` token.
164 return fixer
.insertTextBefore(rightToken
, " ");
167 } else if (!never
&& !allowNewlines
&& hasNewline
) {
171 start
: leftToken
.loc
.end
,
172 end
: rightToken
.loc
.start
174 messageId
: "unexpectedNewline",
178 * Only autofix if there is no newline
179 * https://github.com/eslint/eslint/issues/7787
180 * But if `?.` exists, it doesn't hide no-unexpected-multiline errors
182 if (!node
.optional
) {
186 // Don't remove comments.
187 if (sourceCode
.commentsExistBetween(leftToken
, rightToken
)) {
191 const range
= [leftToken
.range
[1], rightToken
.range
[0]];
192 const qdToken
= sourceCode
.getTokenAfter(leftToken
);
194 if (qdToken
.range
[0] === leftToken
.range
[1]) {
195 return fixer
.replaceTextRange(range
, "?. ");
197 if (qdToken
.range
[1] === rightToken
.range
[0]) {
198 return fixer
.replaceTextRange(range
, " ?.");
200 return fixer
.replaceTextRange(range
, " ?. ");
207 "CallExpression, NewExpression"(node
) {
208 const lastToken
= sourceCode
.getLastToken(node
);
209 const lastCalleeToken
= sourceCode
.getLastToken(node
.callee
);
210 const parenToken
= sourceCode
.getFirstTokenBetween(lastCalleeToken
, lastToken
, astUtils
.isOpeningParenToken
);
211 const prevToken
= parenToken
&& sourceCode
.getTokenBefore(parenToken
, astUtils
.isNotQuestionDotToken
);
213 // Parens in NewExpression are optional
214 if (!(parenToken
&& parenToken
.range
[1] < node
.range
[1])) {
218 checkSpacing(node
, prevToken
, parenToken
);
221 ImportExpression(node
) {
222 const leftToken
= sourceCode
.getFirstToken(node
);
223 const rightToken
= sourceCode
.getTokenAfter(leftToken
);
225 checkSpacing(node
, leftToken
, rightToken
);