Skip to content

Commit 1277cb6

Browse files
committed
feat(compare): handle Qualified names to not crash
1 parent 667c20f commit 1277cb6

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

scripts/compare/type-comparator.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-depth */
22
/* eslint-disable complexity */
33
/* eslint-disable max-lines-per-function */
4-
import { ArrayTypeNode, EnumDeclaration, Identifier, IndexedAccessTypeNode, IntersectionTypeNode, LiteralTypeNode, Node, ts, TupleTypeNode, TypeLiteralNode, TypeNode, TypeQueryNode, TypeReferenceNode, UnionTypeNode } from 'ts-morph';
4+
import { ArrayTypeNode, EnumDeclaration, IndexedAccessTypeNode, IntersectionTypeNode, LiteralTypeNode, Node, ts, TupleTypeNode, TypeLiteralNode, TypeNode, TypeQueryNode, TypeReferenceNode, UnionTypeNode } from 'ts-morph';
55
import { handleInterfaceTypeReferences } from './handle-interfaces';
66
import { compareAndCorrectMembers, orderMembers } from './interface-comparator';
77
import { currentTargetSourceFile, isImportedType } from './shared';
@@ -65,17 +65,23 @@ function isUnknownTypeNode(typeNode: TypeNode): boolean {
6565
}
6666

6767
function handleTargetTypeReference(targetTypeReference: TypeReferenceNode, sourceNode: TypeNode): boolean {
68+
// Skip QualifiedNames
69+
if (Node.isQualifiedName(targetTypeReference.getTypeName())) {
70+
return true;
71+
}
72+
6873
if (targetTypeReference.getTypeName().getText() === 'ReturnType') {
6974
return handleIndexedReturnType(targetTypeReference, sourceNode);
7075
}
76+
7177
if (targetTypeReference.getTypeName().getText() === 'Record') {
7278
const recordEquals = handleRecord(targetTypeReference, sourceNode);
7379
if (recordEquals) {
7480
return true;
7581
}
7682
}
7783

78-
const targetDefinitionNode = (targetTypeReference.getTypeName() as Identifier).getDefinitionNodes()[0];
84+
const targetDefinitionNode = getDeclarationNode(targetTypeReference);
7985
if (Node.isTypeAliasDeclaration(targetDefinitionNode)) {
8086
// If the target is a type alias, check the type node
8187
// Example test case 'external template literal types'
@@ -190,7 +196,7 @@ function handleTargetUnion(targetUnion: UnionTypeNode, sourceNode: TypeNode): bo
190196
// If type references are TypeAliases, expand them
191197
// Example test case 'combined union return type'
192198
for (const typeReferenceMember of typeReferenceMembers) {
193-
const definition = (typeReferenceMember.getTypeName() as Identifier).getDefinitionNodes()[0];
199+
const definition = getDeclarationNode(typeReferenceMember);
194200
if (Node.isTypeAliasDeclaration(definition)) {
195201
const typeNode = definition.getTypeNode();
196202
if (typeNode) {
@@ -391,7 +397,7 @@ function handleRecord(targetTypeReference: TypeReferenceNode, sourceNode: TypeNo
391397

392398
// Target=Record, Source=TypeReference
393399
// Example test case 'numeric interface same as record'
394-
const sourceDefinition = (sourceNode.getTypeName() as Identifier).getDefinitionNodes()[0];
400+
const sourceDefinition = getDeclarationNode(sourceNode);
395401
if (!Node.isInterfaceDeclaration(sourceDefinition)) {
396402
return false;
397403
}
@@ -426,3 +432,10 @@ export function getText(typeNode: TypeNode): string {
426432

427433
return text.replace(/import\(".+?\)\./, '');
428434
}
435+
436+
function getDeclarationNode(typeNode: TypeReferenceNode): Node {
437+
const typeName = typeNode.getTypeName();
438+
const identifier = Node.isQualifiedName(typeName) ? typeName.getRight() : typeName;
439+
440+
return identifier.getDefinitionNodes()[0];
441+
}

0 commit comments

Comments
 (0)