constructor-signature.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.ConstructorSignatureMigration = void 0;
  11. const ts = require("typescript");
  12. const migration_1 = require("../../update-tool/migration");
  13. const version_changes_1 = require("../../update-tool/version-changes");
  14. /**
  15. * List of diagnostic codes that refer to pre-emit diagnostics which indicate invalid
  16. * new expression or super call signatures. See the list of diagnostics here:
  17. *
  18. * https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json
  19. */
  20. const signatureErrorDiagnostics = [
  21. // Type not assignable error diagnostic.
  22. 2345,
  23. // Constructor argument length invalid diagnostics
  24. 2554, 2555, 2556, 2557,
  25. ];
  26. /**
  27. * Migration that visits every TypeScript new expression or super call and checks if
  28. * the parameter type signature is invalid and needs to be updated manually.
  29. */
  30. class ConstructorSignatureMigration extends migration_1.Migration {
  31. constructor() {
  32. super(...arguments);
  33. // Note that the data for this rule is not distinguished based on the target version because
  34. // we don't keep track of the new signature and don't want to update incrementally.
  35. // See: https://github.com/angular/components/pull/12970#issuecomment-418337566
  36. this.data = (0, version_changes_1.getAllChanges)(this.upgradeData.constructorChecks);
  37. // Only enable the migration rule if there is upgrade data.
  38. this.enabled = this.data.length !== 0;
  39. }
  40. visitNode(node) {
  41. if (ts.isSourceFile(node)) {
  42. this._visitSourceFile(node);
  43. }
  44. }
  45. /**
  46. * Method that will be called for each source file of the upgrade project. In order to
  47. * properly determine invalid constructor signatures, we take advantage of the pre-emit
  48. * diagnostics from TypeScript.
  49. *
  50. * By using the diagnostics, the migration can handle type assignability. Not using
  51. * diagnostics would mean that we need to use simple type equality checking which is
  52. * too strict. See related issue: https://github.com/Microsoft/TypeScript/issues/9879
  53. */
  54. _visitSourceFile(sourceFile) {
  55. // List of classes of which the constructor signature has changed.
  56. const diagnostics = ts
  57. .getPreEmitDiagnostics(this.program, sourceFile)
  58. .filter(diagnostic => signatureErrorDiagnostics.includes(diagnostic.code))
  59. .filter(diagnostic => diagnostic.start !== undefined);
  60. for (const diagnostic of diagnostics) {
  61. const node = findConstructorNode(diagnostic, sourceFile);
  62. if (!node) {
  63. continue;
  64. }
  65. const classType = this.typeChecker.getTypeAtLocation(node.expression);
  66. const className = classType.symbol && classType.symbol.name;
  67. const isNewExpression = ts.isNewExpression(node);
  68. // Determine the class names of the actual construct signatures because we cannot assume that
  69. // the diagnostic refers to a constructor of the actual expression. In case the constructor
  70. // is inherited, we need to detect that the owner-class of the constructor is added to the
  71. // constructor checks upgrade data. e.g. `class CustomCalendar extends MatCalendar {}`.
  72. const signatureClassNames = classType
  73. .getConstructSignatures()
  74. .map(signature => getClassDeclarationOfSignature(signature))
  75. .map(declaration => (declaration && declaration.name ? declaration.name.text : null))
  76. .filter(Boolean);
  77. // Besides checking the signature class names, we need to check the actual class name because
  78. // there can be classes without an explicit constructor.
  79. if (!this.data.includes(className) &&
  80. !signatureClassNames.some(name => this.data.includes(name))) {
  81. continue;
  82. }
  83. const classSignatures = classType
  84. .getConstructSignatures()
  85. .map(signature => getParameterTypesFromSignature(signature, this.typeChecker));
  86. const expressionName = isNewExpression ? `new ${className}` : 'super';
  87. const signatures = classSignatures
  88. .map(signature => signature.map(t => (t === null ? 'any' : this.typeChecker.typeToString(t))))
  89. .map(signature => `${expressionName}(${signature.join(', ')})`)
  90. .join(' or ');
  91. this.createFailureAtNode(node, `Found "${className}" constructed with ` +
  92. `an invalid signature. Please manually update the ${expressionName} expression to ` +
  93. `match the new signature${classSignatures.length > 1 ? 's' : ''}: ${signatures}`);
  94. }
  95. }
  96. }
  97. exports.ConstructorSignatureMigration = ConstructorSignatureMigration;
  98. /** Resolves the type for each parameter in the specified signature. */
  99. function getParameterTypesFromSignature(signature, typeChecker) {
  100. return signature
  101. .getParameters()
  102. .map(param => param.declarations ? typeChecker.getTypeAtLocation(param.declarations[0]) : null);
  103. }
  104. /**
  105. * Walks through each node of a source file in order to find a new-expression node or super-call
  106. * expression node that is captured by the specified diagnostic.
  107. */
  108. function findConstructorNode(diagnostic, sourceFile) {
  109. let resolvedNode = null;
  110. const _visitNode = (node) => {
  111. // Check whether the current node contains the diagnostic. If the node contains the diagnostic,
  112. // walk deeper in order to find all constructor expression nodes.
  113. if (node.getStart() <= diagnostic.start && node.getEnd() >= diagnostic.start) {
  114. if (ts.isNewExpression(node) ||
  115. (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.SuperKeyword)) {
  116. resolvedNode = node;
  117. }
  118. ts.forEachChild(node, _visitNode);
  119. }
  120. };
  121. ts.forEachChild(sourceFile, _visitNode);
  122. return resolvedNode;
  123. }
  124. /** Determines the class declaration of the specified construct signature. */
  125. function getClassDeclarationOfSignature(signature) {
  126. let node = signature.getDeclaration();
  127. // Handle signatures which don't have an actual declaration. This happens if a class
  128. // does not have an explicitly written constructor.
  129. if (!node) {
  130. return null;
  131. }
  132. while (!ts.isSourceFile((node = node.parent))) {
  133. if (ts.isClassDeclaration(node)) {
  134. return node;
  135. }
  136. }
  137. return null;
  138. }
  139. //# sourceMappingURL=constructor-signature.js.map