.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / spaceBeforeFunctionParenRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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"],
25     type: "string",
26 };
27 var Rule = /** @class */ (function (_super) {
28     tslib_1.__extends(Rule, _super);
29     function Rule() {
30         return _super !== null && _super.apply(this, arguments) || this;
31     }
32     Rule.prototype.apply = function (sourceFile) {
33         return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0]));
34     };
35     Rule.metadata = {
36         description: "Require or disallow a space before function parenthesis",
37         hasFix: true,
38         optionExamples: [
39             true,
40             [true, "always"],
41             [true, "never"],
42             [true, { anonymous: "always", named: "never", asyncArrow: "always" }],
43         ],
44         options: {
45             properties: {
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,
51             },
52             type: "object",
53         },
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",
56         type: "style",
57         typescriptOnly: false,
58     };
59     Rule.INVALID_WHITESPACE_ERROR = "Spaces before function parens are disallowed";
60     Rule.MISSING_WHITESPACE_ERROR = "Missing whitespace before function parens";
61     return Rule;
62 }(Lint.Rules.AbstractRule));
63 exports.Rule = Rule;
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];
70         options[optionName] =
71             typeof json === "object" ? json[optionName] : json === undefined ? "always" : json;
72     }
73     return options;
74 }
75 function walk(ctx) {
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) {
80             check(node, option);
81         }
82         ts.forEachChild(node, cb);
83     });
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) {
88             return;
89         }
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));
94         }
95         else if (!hasSpace && option === "always") {
96             var pos = openParen.getStart();
97             ctx.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, Lint.Replacement.appendText(pos, " "));
98         }
99     }
100 }
101 function getOption(node, options) {
102     switch (node.kind) {
103         case ts.SyntaxKind.ArrowFunction:
104             return !hasTypeParameters(node) &&
105                 tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword)
106                 ? options.asyncArrow
107                 : undefined;
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 !== "";
116             return hasName
117                 ? options.named
118                 : !hasTypeParameters(node)
119                     ? options.anonymous
120                     : undefined;
121         }
122         case ts.SyntaxKind.MethodDeclaration:
123         case ts.SyntaxKind.MethodSignature:
124         case ts.SyntaxKind.GetAccessor:
125         case ts.SyntaxKind.SetAccessor:
126             return options.method;
127         default:
128             return undefined;
129     }
130 }
131 function hasTypeParameters(node) {
132     return node.typeParameters !== undefined;
133 }
134 var templateObject_1;