source: imaps-frontend/node_modules/eslint/lib/rules/func-call-spacing.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 9.8 KB
RevLine 
[d565449]1/**
2 * @fileoverview Rule to control spacing within function calls
3 * @author Matt DuVall <http://www.mattduvall.com>
4 * @deprecated in ESLint v8.53.0
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Requirements
11//------------------------------------------------------------------------------
12
13const astUtils = require("./utils/ast-utils");
14
15//------------------------------------------------------------------------------
16// Rule Definition
17//------------------------------------------------------------------------------
18
19/** @type {import('../shared/types').Rule} */
20module.exports = {
21 meta: {
22 deprecated: true,
23 replacedBy: [],
24 type: "layout",
25
26 docs: {
27 description: "Require or disallow spacing between function identifiers and their invocations",
28 recommended: false,
29 url: "https://eslint.org/docs/latest/rules/func-call-spacing"
30 },
31
32 fixable: "whitespace",
33
34 schema: {
35 anyOf: [
36 {
37 type: "array",
38 items: [
39 {
40 enum: ["never"]
41 }
42 ],
43 minItems: 0,
44 maxItems: 1
45 },
46 {
47 type: "array",
48 items: [
49 {
50 enum: ["always"]
51 },
52 {
53 type: "object",
54 properties: {
55 allowNewlines: {
56 type: "boolean"
57 }
58 },
59 additionalProperties: false
60 }
61 ],
62 minItems: 0,
63 maxItems: 2
64 }
65 ]
66 },
67
68 messages: {
69 unexpectedWhitespace: "Unexpected whitespace between function name and paren.",
70 unexpectedNewline: "Unexpected newline between function name and paren.",
71 missing: "Missing space between function name and paren."
72 }
73 },
74
75 create(context) {
76
77 const never = context.options[0] !== "always";
78 const allowNewlines = !never && context.options[1] && context.options[1].allowNewlines;
79 const sourceCode = context.sourceCode;
80 const text = sourceCode.getText();
81
82 /**
83 * Check if open space is present in a function name
84 * @param {ASTNode} node node to evaluate
85 * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee.
86 * @param {Token} rightToken Tha first token of the arguments. this is the opening parenthesis that encloses the arguments.
87 * @returns {void}
88 * @private
89 */
90 function checkSpacing(node, leftToken, rightToken) {
91 const textBetweenTokens = text.slice(leftToken.range[1], rightToken.range[0]).replace(/\/\*.*?\*\//gu, "");
92 const hasWhitespace = /\s/u.test(textBetweenTokens);
93 const hasNewline = hasWhitespace && astUtils.LINEBREAK_MATCHER.test(textBetweenTokens);
94
95 /*
96 * never allowNewlines hasWhitespace hasNewline message
97 * F F F F Missing space between function name and paren.
98 * F F F T (Invalid `!hasWhitespace && hasNewline`)
99 * F F T T Unexpected newline between function name and paren.
100 * F F T F (OK)
101 * F T T F (OK)
102 * F T T T (OK)
103 * F T F T (Invalid `!hasWhitespace && hasNewline`)
104 * F T F F Missing space between function name and paren.
105 * T T F F (Invalid `never && allowNewlines`)
106 * T T F T (Invalid `!hasWhitespace && hasNewline`)
107 * T T T T (Invalid `never && allowNewlines`)
108 * T T T F (Invalid `never && allowNewlines`)
109 * T F T F Unexpected space between function name and paren.
110 * T F T T Unexpected space between function name and paren.
111 * T F F T (Invalid `!hasWhitespace && hasNewline`)
112 * T F F F (OK)
113 *
114 * T T Unexpected space between function name and paren.
115 * F F Missing space between function name and paren.
116 * F F T Unexpected newline between function name and paren.
117 */
118
119 if (never && hasWhitespace) {
120 context.report({
121 node,
122 loc: {
123 start: leftToken.loc.end,
124 end: {
125 line: rightToken.loc.start.line,
126 column: rightToken.loc.start.column - 1
127 }
128 },
129 messageId: "unexpectedWhitespace",
130 fix(fixer) {
131
132 // Don't remove comments.
133 if (sourceCode.commentsExistBetween(leftToken, rightToken)) {
134 return null;
135 }
136
137 // If `?.` exists, it doesn't hide no-unexpected-multiline errors
138 if (node.optional) {
139 return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], "?.");
140 }
141
142 /*
143 * Only autofix if there is no newline
144 * https://github.com/eslint/eslint/issues/7787
145 */
146 if (hasNewline) {
147 return null;
148 }
149 return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
150 }
151 });
152 } else if (!never && !hasWhitespace) {
153 context.report({
154 node,
155 loc: {
156 start: {
157 line: leftToken.loc.end.line,
158 column: leftToken.loc.end.column - 1
159 },
160 end: rightToken.loc.start
161 },
162 messageId: "missing",
163 fix(fixer) {
164 if (node.optional) {
165 return null; // Not sure if inserting a space to either before/after `?.` token.
166 }
167 return fixer.insertTextBefore(rightToken, " ");
168 }
169 });
170 } else if (!never && !allowNewlines && hasNewline) {
171 context.report({
172 node,
173 loc: {
174 start: leftToken.loc.end,
175 end: rightToken.loc.start
176 },
177 messageId: "unexpectedNewline",
178 fix(fixer) {
179
180 /*
181 * Only autofix if there is no newline
182 * https://github.com/eslint/eslint/issues/7787
183 * But if `?.` exists, it doesn't hide no-unexpected-multiline errors
184 */
185 if (!node.optional) {
186 return null;
187 }
188
189 // Don't remove comments.
190 if (sourceCode.commentsExistBetween(leftToken, rightToken)) {
191 return null;
192 }
193
194 const range = [leftToken.range[1], rightToken.range[0]];
195 const qdToken = sourceCode.getTokenAfter(leftToken);
196
197 if (qdToken.range[0] === leftToken.range[1]) {
198 return fixer.replaceTextRange(range, "?. ");
199 }
200 if (qdToken.range[1] === rightToken.range[0]) {
201 return fixer.replaceTextRange(range, " ?.");
202 }
203 return fixer.replaceTextRange(range, " ?. ");
204 }
205 });
206 }
207 }
208
209 return {
210 "CallExpression, NewExpression"(node) {
211 const lastToken = sourceCode.getLastToken(node);
212 const lastCalleeToken = sourceCode.getLastToken(node.callee);
213 const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken);
214 const prevToken = parenToken && sourceCode.getTokenBefore(parenToken, astUtils.isNotQuestionDotToken);
215
216 // Parens in NewExpression are optional
217 if (!(parenToken && parenToken.range[1] < node.range[1])) {
218 return;
219 }
220
221 checkSpacing(node, prevToken, parenToken);
222 },
223
224 ImportExpression(node) {
225 const leftToken = sourceCode.getFirstToken(node);
226 const rightToken = sourceCode.getTokenAfter(leftToken);
227
228 checkSpacing(node, leftToken, rightToken);
229 }
230 };
231
232 }
233};
Note: See TracBrowser for help on using the repository browser.