noUnnecessaryCallbackWrapperRule.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. type: "style",
  43. typescriptOnly: false,
  44. };
  45. return Rule;
  46. }(Lint.Rules.AbstractRule));
  47. exports.Rule = Rule;
  48. function walk(ctx) {
  49. return ts.forEachChild(ctx.sourceFile, cb);
  50. function cb(node) {
  51. if (tsutils_1.isArrowFunction(node) && !tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) &&
  52. tsutils_1.isCallExpression(node.body) && tsutils_1.isIdentifier(node.body.expression) &&
  53. isRedundantCallback(node.parameters, node.body.arguments, node.body.expression)) {
  54. var start = node.getStart(ctx.sourceFile);
  55. ctx.addFailure(start, node.end, Rule.FAILURE_STRING(node.body.expression.text), [
  56. Lint.Replacement.deleteFromTo(start, node.body.getStart(ctx.sourceFile)),
  57. Lint.Replacement.deleteFromTo(node.body.expression.end, node.end),
  58. ]);
  59. }
  60. else {
  61. return ts.forEachChild(node, cb);
  62. }
  63. }
  64. }
  65. function isRedundantCallback(parameters, args, expression) {
  66. if (parameters.length !== args.length) {
  67. return false;
  68. }
  69. for (var i = 0; i < parameters.length; ++i) {
  70. var _a = parameters[i], dotDotDotToken = _a.dotDotDotToken, name = _a.name;
  71. var arg = args[i];
  72. if (dotDotDotToken !== undefined) {
  73. if (!tsutils_1.isSpreadElement(arg)) {
  74. return false;
  75. }
  76. arg = arg.expression;
  77. }
  78. if (!tsutils_1.isIdentifier(name) || !tsutils_1.isIdentifier(arg) || name.text !== arg.text
  79. // If the invoked expression is one of the parameters, bail.
  80. || expression.text === name.text) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. var templateObject_1;