method-call-arguments-rule.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.io/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const ts = require("typescript");
  11. const migration_rule_1 = require("../../update-tool/migration-rule");
  12. const upgrade_data_1 = require("../upgrade-data");
  13. /**
  14. * Rule that visits every TypeScript method call expression and checks if the
  15. * argument count is invalid and needs to be *manually* updated.
  16. */
  17. class MethodCallArgumentsRule extends migration_rule_1.MigrationRule {
  18. constructor() {
  19. super(...arguments);
  20. /** Change data that upgrades to the specified target version. */
  21. this.data = upgrade_data_1.getVersionUpgradeData(this, 'methodCallChecks');
  22. // Only enable the migration rule if there is upgrade data.
  23. this.ruleEnabled = this.data.length !== 0;
  24. }
  25. visitNode(node) {
  26. if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
  27. this._checkPropertyAccessMethodCall(node);
  28. }
  29. }
  30. _checkPropertyAccessMethodCall(node) {
  31. const propertyAccess = node.expression;
  32. if (!ts.isIdentifier(propertyAccess.name)) {
  33. return;
  34. }
  35. const hostType = this.typeChecker.getTypeAtLocation(propertyAccess.expression);
  36. const hostTypeName = hostType.symbol && hostType.symbol.name;
  37. const methodName = propertyAccess.name.text;
  38. if (!hostTypeName) {
  39. return;
  40. }
  41. // TODO(devversion): Revisit the implementation of this upgrade rule. It seems difficult
  42. // and ambiguous to maintain the data for this rule. e.g. consider a method which has the
  43. // same amount of arguments but just had a type change. In that case we could still add
  44. // new entries to the upgrade data that match the current argument length to just show
  45. // a failure message, but adding that data becomes painful if the method has optional
  46. // parameters and it would mean that the error message would always show up, even if the
  47. // argument is in some cases still assignable to the new parameter type. We could re-use
  48. // the logic we have in the constructor-signature checks to check for assignability and
  49. // to make the upgrade data less verbose.
  50. const failure = this.data.filter(data => data.method === methodName && data.className === hostTypeName)
  51. .map(data => data.invalidArgCounts.find(f => f.count === node.arguments.length))[0];
  52. if (!failure) {
  53. return;
  54. }
  55. this.createFailureAtNode(node, `Found call to "${hostTypeName + '.' + methodName}" ` +
  56. `with ${failure.count} arguments. Message: ${failure.message}`);
  57. }
  58. }
  59. exports.MethodCallArgumentsRule = MethodCallArgumentsRule;
  60. //# sourceMappingURL=method-call-arguments-rule.js.map