4 * Copyright 2017 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
27 return _super !== null && _super.apply(this, arguments) || this;
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.";
33 Rule.prototype.apply = function (sourceFile) {
34 return this.applyWithFunction(sourceFile, walk);
36 /* tslint:disable:object-literal-sort-keys */
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.",
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 "]))),
45 typescriptOnly: false,
46 codeExamples: noUnnecessaryCallbackWrapper_examples_1.codeExamples,
49 }(Lint.Rules.AbstractRule));
52 return ts.forEachChild(ctx.sourceFile, cb);
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),
66 return ts.forEachChild(node, cb);
70 function isRedundantCallback(parameters, args, expression) {
71 if (parameters.length !== args.length) {
74 for (var i = 0; i < parameters.length; ++i) {
75 var _a = parameters[i], dotDotDotToken = _a.dotDotDotToken, name = _a.name;
77 if (dotDotDotToken !== undefined) {
78 if (!tsutils_1.isSpreadElement(arg)) {
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) {
93 var templateObject_1, templateObject_2;