4 * Copyright 2018 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var ALWAYS_OR_NEVER = {
24 enum: ["always", "never"],
27 var Rule = /** @class */ (function (_super) {
28 tslib_1.__extends(Rule, _super);
30 return _super !== null && _super.apply(this, arguments) || this;
32 Rule.prototype.apply = function (sourceFile) {
33 return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0]));
36 description: "Require or disallow a space before function parenthesis",
42 [true, { anonymous: "always", named: "never", asyncArrow: "always" }],
46 anonymous: ALWAYS_OR_NEVER,
47 asyncArrow: ALWAYS_OR_NEVER,
48 constructor: ALWAYS_OR_NEVER,
49 method: ALWAYS_OR_NEVER,
50 named: ALWAYS_OR_NEVER,
54 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One argument which is an object which may contain the keys `anonymous`, `named`, and `asyncArrow`\n These should be set to either `\"always\"` or `\"never\"`.\n\n * `\"anonymous\"` checks before the opening paren in anonymous functions\n * `\"named\"` checks before the opening paren in named functions\n * `\"asyncArrow\"` checks before the opening paren in async arrow functions\n * `\"method\"` checks before the opening paren in class methods\n * `\"constructor\"` checks before the opening paren in class constructors\n "], ["\n One argument which is an object which may contain the keys \\`anonymous\\`, \\`named\\`, and \\`asyncArrow\\`\n These should be set to either \\`\"always\"\\` or \\`\"never\"\\`.\n\n * \\`\"anonymous\"\\` checks before the opening paren in anonymous functions\n * \\`\"named\"\\` checks before the opening paren in named functions\n * \\`\"asyncArrow\"\\` checks before the opening paren in async arrow functions\n * \\`\"method\"\\` checks before the opening paren in class methods\n * \\`\"constructor\"\\` checks before the opening paren in class constructors\n "]))),
55 ruleName: "space-before-function-paren",
57 typescriptOnly: false,
59 Rule.INVALID_WHITESPACE_ERROR = "Spaces before function parens are disallowed";
60 Rule.MISSING_WHITESPACE_ERROR = "Missing whitespace before function parens";
62 }(Lint.Rules.AbstractRule));
64 var optionNames = ["anonymous", "asyncArrow", "constructor", "method", "named"];
65 function parseOptions(json) {
66 // Need to specify constructor or it will be Object
67 var options = { constructor: undefined };
68 for (var _i = 0, optionNames_1 = optionNames; _i < optionNames_1.length; _i++) {
69 var optionName = optionNames_1[_i];
71 typeof json === "object" ? json[optionName] : json === undefined ? "always" : json;
76 var options = ctx.options, sourceFile = ctx.sourceFile;
77 ts.forEachChild(sourceFile, function cb(node) {
78 var option = getOption(node, options);
79 if (option !== undefined) {
82 ts.forEachChild(node, cb);
84 function check(node, option) {
85 var openParen = tsutils_1.getChildOfKind(node, ts.SyntaxKind.OpenParenToken, sourceFile);
86 // openParen may be missing for an async arrow function `async x => ...`.
87 if (openParen === undefined) {
90 var hasSpace = Lint.isWhiteSpace(sourceFile.text.charCodeAt(openParen.end - 2));
91 if (hasSpace && option === "never") {
92 var pos = openParen.getStart() - 1;
93 ctx.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, Lint.Replacement.deleteText(pos, 1));
95 else if (!hasSpace && option === "always") {
96 var pos = openParen.getStart();
97 ctx.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, Lint.Replacement.appendText(pos, " "));
101 function getOption(node, options) {
103 case ts.SyntaxKind.ArrowFunction:
104 return !hasTypeParameters(node) &&
105 tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword)
108 case ts.SyntaxKind.Constructor:
109 return options.constructor;
110 case ts.SyntaxKind.FunctionDeclaration:
111 // name is optional for function declaration which is default export (TS will emit error in other cases).
112 // Can be handled in the same way as function expression.
113 case ts.SyntaxKind.FunctionExpression: {
114 var functionName = node.name;
115 var hasName = functionName !== undefined && functionName.text !== "";
118 : !hasTypeParameters(node)
122 case ts.SyntaxKind.MethodDeclaration:
123 case ts.SyntaxKind.MethodSignature:
124 case ts.SyntaxKind.GetAccessor:
125 case ts.SyntaxKind.SetAccessor:
126 return options.method;
131 function hasTypeParameters(node) {
132 return node.typeParameters !== undefined;
134 var templateObject_1;