.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / arrowParensRule.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 BAN_SINGLE_ARG_PARENS = "ban-single-arg-parens";
24 var Rule = /** @class */ (function (_super) {
25     tslib_1.__extends(Rule, _super);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     Rule.prototype.apply = function (sourceFile) {
30         return this.applyWithFunction(sourceFile, walk, {
31             banSingleArgParens: this.ruleArguments.indexOf(BAN_SINGLE_ARG_PARENS) !== -1,
32         });
33     };
34     /* tslint:disable:object-literal-sort-keys */
35     Rule.metadata = {
36         ruleName: "arrow-parens",
37         description: "Requires parentheses around the parameters of arrow function definitions.",
38         hasFix: true,
39         rationale: "Maintains stylistic consistency with other arrow function definitions.",
40         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            If `", "` is specified, then arrow functions with one parameter\n            must not have parentheses if removing them is allowed by TypeScript."], ["\n            If \\`", "\\` is specified, then arrow functions with one parameter\n            must not have parentheses if removing them is allowed by TypeScript."])), BAN_SINGLE_ARG_PARENS),
41         options: {
42             type: "string",
43             enum: [BAN_SINGLE_ARG_PARENS],
44         },
45         optionExamples: [true, [true, BAN_SINGLE_ARG_PARENS]],
46         type: "formatting",
47         typescriptOnly: false,
48     };
49     /* tslint:enable:object-literal-sort-keys */
50     Rule.FAILURE_STRING_MISSING = "Parentheses are required around the parameters of an arrow function definition";
51     Rule.FAILURE_STRING_EXISTS = "Parentheses are prohibited around the parameter in this single parameter arrow function";
52     return Rule;
53 }(Lint.Rules.AbstractRule));
54 exports.Rule = Rule;
55 function walk(ctx) {
56     function cb(node) {
57         if (tsutils_1.isArrowFunction(node) && parensAreOptional(node)) {
58             var openParen = tsutils_1.getChildOfKind(node, ts.SyntaxKind.OpenParenToken);
59             if (openParen === undefined) {
60                 if (!ctx.options.banSingleArgParens) {
61                     var parameter = node.parameters[0];
62                     var start = parameter.getStart(ctx.sourceFile);
63                     var end = parameter.end;
64                     ctx.addFailure(start, end, Rule.FAILURE_STRING_MISSING, [
65                         Lint.Replacement.appendText(start, "("),
66                         Lint.Replacement.appendText(end, ")"),
67                     ]);
68                 }
69             }
70             else if (ctx.options.banSingleArgParens) {
71                 var closeParen = tsutils_1.getChildOfKind(node, ts.SyntaxKind.CloseParenToken);
72                 var charBeforeOpenParen = ctx.sourceFile.text.substring(openParen.pos - 1, openParen.pos);
73                 var replaceValue = charBeforeOpenParen.match(/[a-z]/i) !== null ? " " : "";
74                 ctx.addFailureAtNode(node.parameters[0], Rule.FAILURE_STRING_EXISTS, [
75                     Lint.Replacement.replaceFromTo(openParen.pos, node.parameters[0].getStart(ctx.sourceFile), replaceValue),
76                     Lint.Replacement.deleteFromTo(node.parameters[0].end, closeParen.end),
77                 ]);
78             }
79         }
80         return ts.forEachChild(node, cb);
81     }
82     return ts.forEachChild(ctx.sourceFile, cb);
83 }
84 function parensAreOptional(node) {
85     return (node.parameters.length === 1 &&
86         node.typeParameters === undefined &&
87         node.type === undefined &&
88         isSimpleParameter(node.parameters[0]));
89 }
90 function isSimpleParameter(parameter) {
91     return (parameter.name.kind === ts.SyntaxKind.Identifier &&
92         parameter.dotDotDotToken === undefined &&
93         parameter.initializer === undefined &&
94         parameter.questionToken === undefined &&
95         parameter.type === undefined);
96 }
97 var templateObject_1;