.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUnnecessaryCallbackWrapperRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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 noUnnecessaryCallbackWrapper_examples_1 = require("./code-examples/noUnnecessaryCallbackWrapper.examples");
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     /* tslint:enable:object-literal-sort-keys */
30     Rule.FAILURE_STRING = function (cbText) {
31         return "No need to wrap '" + cbText + "' in another function. Just use it directly.";
32     };
33     Rule.prototype.apply = function (sourceFile) {
34         return this.applyWithFunction(sourceFile, walk);
35     };
36     /* tslint:disable:object-literal-sort-keys */
37     Rule.metadata = {
38         ruleName: "no-unnecessary-callback-wrapper",
39         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Replaces `x => f(x)` with just `f`.\n            To catch more cases, enable `only-arrow-functions` and `arrow-return-shorthand` too."], ["\n            Replaces \\`x => f(x)\\` with just \\`f\\`.\n            To catch more cases, enable \\`only-arrow-functions\\` and \\`arrow-return-shorthand\\` too."]))),
40         optionsDescription: "Not configurable.",
41         options: null,
42         optionExamples: [true],
43         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            There's generally no reason to wrap a function with a callback wrapper if it's directly called anyway.\n            Doing so creates extra inline lambdas that slow the runtime down.\n        "], ["\n            There's generally no reason to wrap a function with a callback wrapper if it's directly called anyway.\n            Doing so creates extra inline lambdas that slow the runtime down.\n        "]))),
44         type: "style",
45         typescriptOnly: false,
46         codeExamples: noUnnecessaryCallbackWrapper_examples_1.codeExamples,
47     };
48     return Rule;
49 }(Lint.Rules.AbstractRule));
50 exports.Rule = Rule;
51 function walk(ctx) {
52     return ts.forEachChild(ctx.sourceFile, cb);
53     function cb(node) {
54         if (tsutils_1.isArrowFunction(node) &&
55             !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) &&
56             tsutils_1.isCallExpression(node.body) &&
57             tsutils_1.isIdentifier(node.body.expression) &&
58             isRedundantCallback(node.parameters, node.body.arguments, node.body.expression)) {
59             var start = node.getStart(ctx.sourceFile);
60             ctx.addFailure(start, node.end, Rule.FAILURE_STRING(node.body.expression.text), [
61                 Lint.Replacement.deleteFromTo(start, node.body.getStart(ctx.sourceFile)),
62                 Lint.Replacement.deleteFromTo(node.body.expression.end, node.end),
63             ]);
64         }
65         else {
66             return ts.forEachChild(node, cb);
67         }
68     }
69 }
70 function isRedundantCallback(parameters, args, expression) {
71     if (parameters.length !== args.length) {
72         return false;
73     }
74     for (var i = 0; i < parameters.length; ++i) {
75         var _a = parameters[i], dotDotDotToken = _a.dotDotDotToken, name = _a.name;
76         var arg = args[i];
77         if (dotDotDotToken !== undefined) {
78             if (!tsutils_1.isSpreadElement(arg)) {
79                 return false;
80             }
81             arg = arg.expression;
82         }
83         if (!tsutils_1.isIdentifier(name) ||
84             !tsutils_1.isIdentifier(arg) ||
85             name.text !== arg.text ||
86             // If the invoked expression is one of the parameters, bail.
87             expression.text === name.text) {
88             return false;
89         }
90     }
91     return true;
92 }
93 var templateObject_1, templateObject_2;