Skip to content

Commit

Permalink
fix(migrations): resolve error in standalone migration (#56302)
Browse files Browse the repository at this point in the history
This is related to an issue that was reported internally. We were assuming that `hasNgModuleMetadataElements` will return true only for property assignments initialized to arrays, but that's not the case.

These changes update the type and our assertions to more accurately reflect the AST and to avoid the error.

PR Close #56302
  • Loading branch information
crisbeto authored and alxhub committed Jun 7, 2024
1 parent 4c13309 commit c07e1b3
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,7 @@ function findImportLocation(
* E.g. `declarations: [Foo]` or `declarations: SOME_VAR` would match this description,
* but not `declarations: []`.
*/
function hasNgModuleMetadataElements(
node: ts.Node,
): node is ts.PropertyAssignment & {initializer: ts.ArrayLiteralExpression} {
function hasNgModuleMetadataElements(node: ts.Node): node is ts.PropertyAssignment {
return (
ts.isPropertyAssignment(node) &&
(!ts.isArrayLiteralExpression(node.initializer) || node.initializer.elements.length > 0)
Expand Down Expand Up @@ -788,7 +786,9 @@ function analyzeTestingModules(

const importsProp = findLiteralProperty(obj, 'imports');
const importElements =
importsProp && hasNgModuleMetadataElements(importsProp)
importsProp &&
hasNgModuleMetadataElements(importsProp) &&
ts.isArrayLiteralExpression(importsProp.initializer)
? importsProp.initializer.elements.filter((el) => {
// Filter out calls since they may be a `ModuleWithProviders`.
return (
Expand Down Expand Up @@ -847,7 +847,11 @@ function extractDeclarationsFromTestObject(
const results: ts.ClassDeclaration[] = [];
const declarations = findLiteralProperty(obj, 'declarations');

if (declarations && hasNgModuleMetadataElements(declarations)) {
if (
declarations &&
hasNgModuleMetadataElements(declarations) &&
ts.isArrayLiteralExpression(declarations.initializer)
) {
for (const element of declarations.initializer.elements) {
const declaration = findClassDeclaration(element, typeChecker);

Expand Down

0 comments on commit c07e1b3

Please sign in to comment.