noUnnecessaryCallbackWrapperRule.js 4.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. /* tslint:enable:object-literal-sort-keys */
  29. Rule.FAILURE_STRING = function (cbText) {
  30. return "No need to wrap '" + cbText + "' in another function. Just use it directly.";
  31. };
  32. Rule.prototype.apply = function (sourceFile) {
  33. return this.applyWithFunction(sourceFile, walk);
  34. };
  35. /* tslint:disable:object-literal-sort-keys */
  36. Rule.metadata = {
  37. ruleName: "no-unnecessary-callback-wrapper",
  38. 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."]))),
  39. optionsDescription: "Not configurable.",
  40. options: null,
  41. optionExamples: [true],
  42. 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 "]))),
  43. type: "style",
  44. typescriptOnly: false,
  45. };
  46. return Rule;
  47. }(Lint.Rules.AbstractRule));
  48. exports.Rule = Rule;
  49. function walk(ctx) {
  50. return ts.forEachChild(ctx.sourceFile, cb);
  51. function cb(node) {
  52. if (tsutils_1.isArrowFunction(node) && !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) &&
  53. tsutils_1.isCallExpression(node.body) && tsutils_1.isIdentifier(node.body.expression) &&
  54. isRedundantCallback(node.parameters, node.body.arguments, node.body.expression)) {
  55. var start = node.getStart(ctx.sourceFile);
  56. ctx.addFailure(start, node.end, Rule.FAILURE_STRING(node.body.expression.text), [
  57. Lint.Replacement.deleteFromTo(start, node.body.getStart(ctx.sourceFile)),
  58. Lint.Replacement.deleteFromTo(node.body.expression.end, node.end),
  59. ]);
  60. }
  61. else {
  62. return ts.forEachChild(node, cb);
  63. }
  64. }
  65. }
  66. function isRedundantCallback(parameters, args, expression) {
  67. if (parameters.length !== args.length) {
  68. return false;
  69. }
  70. for (var i = 0; i < parameters.length; ++i) {
  71. var _a = parameters[i], dotDotDotToken = _a.dotDotDotToken, name = _a.name;
  72. var arg = args[i];
  73. if (dotDotDotToken !== undefined) {
  74. if (!tsutils_1.isSpreadElement(arg)) {
  75. return false;
  76. }
  77. arg = arg.expression;
  78. }
  79. if (!tsutils_1.isIdentifier(name) || !tsutils_1.isIdentifier(arg) || name.text !== arg.text
  80. // If the invoked expression is one of the parameters, bail.
  81. || expression.text === name.text) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. var templateObject_1, templateObject_2;