Skip to content

Commit c07e1b3

Browse files
crisbetoalxhub
authored andcommitted
fix(migrations): resolve error in standalone migration (#56302)
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
1 parent 4c13309 commit c07e1b3

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,7 @@ function findImportLocation(
517517
* E.g. `declarations: [Foo]` or `declarations: SOME_VAR` would match this description,
518518
* but not `declarations: []`.
519519
*/
520-
function hasNgModuleMetadataElements(
521-
node: ts.Node,
522-
): node is ts.PropertyAssignment & {initializer: ts.ArrayLiteralExpression} {
520+
function hasNgModuleMetadataElements(node: ts.Node): node is ts.PropertyAssignment {
523521
return (
524522
ts.isPropertyAssignment(node) &&
525523
(!ts.isArrayLiteralExpression(node.initializer) || node.initializer.elements.length > 0)
@@ -788,7 +786,9 @@ function analyzeTestingModules(
788786

789787
const importsProp = findLiteralProperty(obj, 'imports');
790788
const importElements =
791-
importsProp && hasNgModuleMetadataElements(importsProp)
789+
importsProp &&
790+
hasNgModuleMetadataElements(importsProp) &&
791+
ts.isArrayLiteralExpression(importsProp.initializer)
792792
? importsProp.initializer.elements.filter((el) => {
793793
// Filter out calls since they may be a `ModuleWithProviders`.
794794
return (
@@ -847,7 +847,11 @@ function extractDeclarationsFromTestObject(
847847
const results: ts.ClassDeclaration[] = [];
848848
const declarations = findLiteralProperty(obj, 'declarations');
849849

850-
if (declarations && hasNgModuleMetadataElements(declarations)) {
850+
if (
851+
declarations &&
852+
hasNgModuleMetadataElements(declarations) &&
853+
ts.isArrayLiteralExpression(declarations.initializer)
854+
) {
851855
for (const element of declarations.initializer.elements) {
852856
const declaration = findClassDeclaration(element, typeChecker);
853857

0 commit comments

Comments
 (0)