1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
"use strict";
/**
* @license
* Copyright 2016 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ts = require("typescript");
var Lint = require("../index");
var ALWAYS_OR_NEVER = {
enum: ["always", "never"],
type: "string",
};
var Rule = (function (_super) {
tslib_1.__extends(Rule, _super);
function Rule() {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0]));
};
return Rule;
}(Lint.Rules.AbstractRule));
Rule.metadata = {
description: "Require or disallow a space before function parenthesis",
hasFix: true,
optionExamples: [
true,
[true, "always"],
[true, "never"],
[true, { anonymous: "always", named: "never", asyncArrow: "always" }],
],
options: {
properties: {
anonymous: ALWAYS_OR_NEVER,
asyncArrow: ALWAYS_OR_NEVER,
constructor: ALWAYS_OR_NEVER,
method: ALWAYS_OR_NEVER,
named: ALWAYS_OR_NEVER,
},
type: "object",
},
optionsDescription: (_a = ["\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 "], _a.raw = ["\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 "], Lint.Utils.dedent(_a)),
ruleName: "space-before-function-paren",
type: "style",
typescriptOnly: false,
};
Rule.INVALID_WHITESPACE_ERROR = "Spaces before function parens are disallowed";
Rule.MISSING_WHITESPACE_ERROR = "Missing whitespace before function parens";
exports.Rule = Rule;
var optionNames = ["anonymous", "asyncArrow", "constructor", "method", "named"];
function parseOptions(json) {
// Need to specify constructor or it will be Object
var options = { constructor: undefined };
for (var _i = 0, optionNames_1 = optionNames; _i < optionNames_1.length; _i++) {
var optionName = optionNames_1[_i];
options[optionName] = typeof json === "object" ? json[optionName] : json === undefined ? "always" : json;
}
return options;
}
function walk(ctx) {
var options = ctx.options, sourceFile = ctx.sourceFile;
ts.forEachChild(sourceFile, function cb(node) {
var option = getOption(node, options);
if (option !== undefined) {
var openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken);
var hasSpace = ts.isWhiteSpaceLike(sourceFile.text.charCodeAt(openParen.end - 2));
if (hasSpace && option === "never") {
var pos = openParen.getStart() - 1;
ctx.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, Lint.Replacement.deleteText(pos, 1));
}
else if (!hasSpace && option === "always") {
var pos = openParen.getStart();
ctx.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, Lint.Replacement.appendText(pos, " "));
}
}
ts.forEachChild(node, cb);
});
}
function getOption(node, options) {
switch (node.kind) {
case ts.SyntaxKind.ArrowFunction:
return Lint.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) ? options.asyncArrow : undefined;
case ts.SyntaxKind.Constructor:
return options.constructor;
case ts.SyntaxKind.FunctionDeclaration:
return options.named;
case ts.SyntaxKind.FunctionExpression:
return node.name !== undefined ? options.named : options.anonymous;
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
return options.method;
default:
return undefined;
}
}
var _a;
|