.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / onlyArrowFunctionsRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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 utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var onlyArrowFunctions_examples_1 = require("./code-examples/onlyArrowFunctions.examples");
24 var OPTION_ALLOW_DECLARATIONS = "allow-declarations";
25 var OPTION_ALLOW_NAMED_FUNCTIONS = "allow-named-functions";
26 var Rule = /** @class */ (function (_super) {
27     tslib_1.__extends(Rule, _super);
28     function Rule() {
29         return _super !== null && _super.apply(this, arguments) || this;
30     }
31     Rule.prototype.apply = function (sourceFile) {
32         return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
33     };
34     /* tslint:disable:object-literal-sort-keys */
35     Rule.metadata = {
36         ruleName: "only-arrow-functions",
37         description: "Disallows traditional (non-arrow) function expressions.",
38         rationale: "Traditional functions don't bind lexical scope, which can lead to unexpected behavior when accessing 'this'.",
39         descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Note that non-arrow functions are allowed if 'this' appears somewhere in its body\n            (as such functions cannot be converted to arrow functions).\n        "], ["\n            Note that non-arrow functions are allowed if 'this' appears somewhere in its body\n            (as such functions cannot be converted to arrow functions).\n        "]))),
40         optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            Two arguments may be optionally provided:\n\n            * `\"", "\"` allows standalone function declarations.\n            * `\"", "\"` allows the expression `function foo() {}` but not `function() {}`.\n        "], ["\n            Two arguments may be optionally provided:\n\n            * \\`\"", "\"\\` allows standalone function declarations.\n            * \\`\"", "\"\\` allows the expression \\`function foo() {}\\` but not \\`function() {}\\`.\n        "])), OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS),
41         options: {
42             type: "array",
43             items: {
44                 type: "string",
45                 enum: [OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS],
46             },
47             minLength: 0,
48             maxLength: 1,
49         },
50         optionExamples: [true, [true, OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS]],
51         type: "typescript",
52         typescriptOnly: false,
53         codeExamples: onlyArrowFunctions_examples_1.codeExamples,
54     };
55     /* tslint:enable:object-literal-sort-keys */
56     Rule.FAILURE_STRING = "non-arrow functions are forbidden";
57     return Rule;
58 }(Lint.Rules.AbstractRule));
59 exports.Rule = Rule;
60 function parseOptions(ruleArguments) {
61     return {
62         allowDeclarations: hasOption(OPTION_ALLOW_DECLARATIONS),
63         allowNamedFunctions: hasOption(OPTION_ALLOW_NAMED_FUNCTIONS),
64     };
65     function hasOption(name) {
66         return ruleArguments.indexOf(name) !== -1;
67     }
68 }
69 function walk(ctx) {
70     var sourceFile = ctx.sourceFile, _a = ctx.options, allowDeclarations = _a.allowDeclarations, allowNamedFunctions = _a.allowNamedFunctions;
71     return ts.forEachChild(sourceFile, function cb(node) {
72         switch (node.kind) {
73             case ts.SyntaxKind.FunctionDeclaration:
74                 if (allowDeclarations) {
75                     break;
76                 }
77             // falls through
78             case ts.SyntaxKind.FunctionExpression: {
79                 var f = node;
80                 if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
81                     ctx.addFailureAtNode(utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile), Rule.FAILURE_STRING);
82                 }
83             }
84         }
85         return ts.forEachChild(node, cb);
86     });
87 }
88 /** Generator functions and functions using `this` are allowed. */
89 function functionIsExempt(node) {
90     return (node.asteriskToken !== undefined ||
91         (node.parameters.length !== 0 && utils.isThisParameter(node.parameters[0])) ||
92         (node.body !== undefined && ts.forEachChild(node, usesThis) === true));
93 }
94 function usesThis(node) {
95     return (node.kind === ts.SyntaxKind.ThisKeyword ||
96         (!utils.hasOwnThisReference(node) && ts.forEachChild(node, usesThis)));
97 }
98 var templateObject_1, templateObject_2;