method-call-arguments.js 3.0 KB

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