UNPKG

6.65 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const util = __importStar(require("../util"));
23exports.default = util.createRule({
24 name: 'func-call-spacing',
25 meta: {
26 type: 'layout',
27 docs: {
28 description: 'Require or disallow spacing between function identifiers and their invocations',
29 recommended: false,
30 extendsBaseRule: true,
31 },
32 fixable: 'whitespace',
33 schema: {
34 anyOf: [
35 {
36 type: 'array',
37 items: [
38 {
39 enum: ['never'],
40 },
41 ],
42 minItems: 0,
43 maxItems: 1,
44 },
45 {
46 type: 'array',
47 items: [
48 {
49 enum: ['always'],
50 },
51 {
52 type: 'object',
53 properties: {
54 allowNewlines: {
55 type: 'boolean',
56 },
57 },
58 additionalProperties: false,
59 },
60 ],
61 minItems: 0,
62 maxItems: 2,
63 },
64 ],
65 },
66 messages: {
67 unexpectedWhitespace: 'Unexpected whitespace between function name and paren.',
68 unexpectedNewline: 'Unexpected newline between function name and paren.',
69 missing: 'Missing space between function name and paren.',
70 },
71 },
72 defaultOptions: ['never', {}],
73 create(context, [option, config]) {
74 const sourceCode = context.getSourceCode();
75 const text = sourceCode.getText();
76 /**
77 * Check if open space is present in a function name
78 * @param {ASTNode} node node to evaluate
79 * @returns {void}
80 * @private
81 */
82 function checkSpacing(node) {
83 var _a;
84 const isOptionalCall = util.isOptionalCallExpression(node);
85 const closingParenToken = sourceCode.getLastToken(node);
86 const lastCalleeTokenWithoutPossibleParens = sourceCode.getLastToken((_a = node.typeParameters) !== null && _a !== void 0 ? _a : node.callee);
87 const openingParenToken = sourceCode.getFirstTokenBetween(lastCalleeTokenWithoutPossibleParens, closingParenToken, util.isOpeningParenToken);
88 if (!openingParenToken || openingParenToken.range[1] >= node.range[1]) {
89 // new expression with no parens...
90 return;
91 }
92 const lastCalleeToken = sourceCode.getTokenBefore(openingParenToken, util.isNotOptionalChainPunctuator);
93 const textBetweenTokens = text
94 .slice(lastCalleeToken.range[1], openingParenToken.range[0])
95 .replace(/\/\*.*?\*\//gu, '');
96 const hasWhitespace = /\s/u.test(textBetweenTokens);
97 const hasNewline = hasWhitespace && util.LINEBREAK_MATCHER.test(textBetweenTokens);
98 if (option === 'never') {
99 if (hasWhitespace) {
100 return context.report({
101 node,
102 loc: lastCalleeToken.loc.start,
103 messageId: 'unexpectedWhitespace',
104 fix(fixer) {
105 /*
106 * Only autofix if there is no newline
107 * https://github.com/eslint/eslint/issues/7787
108 */
109 if (!hasNewline &&
110 // don't fix optional calls
111 !isOptionalCall) {
112 return fixer.removeRange([
113 lastCalleeToken.range[1],
114 openingParenToken.range[0],
115 ]);
116 }
117 return null;
118 },
119 });
120 }
121 }
122 else if (isOptionalCall) {
123 // disallow:
124 // foo?. ();
125 // foo ?.();
126 // foo ?. ();
127 if (hasWhitespace || hasNewline) {
128 context.report({
129 node,
130 loc: lastCalleeToken.loc.start,
131 messageId: 'unexpectedWhitespace',
132 });
133 }
134 }
135 else {
136 if (!hasWhitespace) {
137 context.report({
138 node,
139 loc: lastCalleeToken.loc.start,
140 messageId: 'missing',
141 fix(fixer) {
142 return fixer.insertTextBefore(openingParenToken, ' ');
143 },
144 });
145 }
146 else if (!config.allowNewlines && hasNewline) {
147 context.report({
148 node,
149 loc: lastCalleeToken.loc.start,
150 messageId: 'unexpectedNewline',
151 fix(fixer) {
152 return fixer.replaceTextRange([lastCalleeToken.range[1], openingParenToken.range[0]], ' ');
153 },
154 });
155 }
156 }
157 }
158 return {
159 CallExpression: checkSpacing,
160 NewExpression: checkSpacing,
161 };
162 },
163});
164//# sourceMappingURL=func-call-spacing.js.map
\No newline at end of file