@@ -685,47 +685,30 @@ export default function jasmineGlobals(fileInfo, api, options) {
685
685
j ( path ) . replaceWith ( j . objectExpression ( properties ) )
686
686
} )
687
687
688
- root
689
- . find ( j . VariableDeclarator )
690
- . filter ( ( path ) => {
691
- const { typeAnnotation } = path . value . id
692
- if ( ! typeAnnotation ) return false
693
-
694
- const { typeName } = typeAnnotation . typeAnnotation
688
+ const isJasmineSpyType = ( typeAnnotation ) =>
689
+ typeAnnotation &&
690
+ typeAnnotation . type === 'TSTypeReference' &&
691
+ typeAnnotation . typeName . type === 'TSQualifiedName' &&
692
+ typeAnnotation . typeName . left . name === 'jasmine' &&
693
+ ( typeAnnotation . typeName . right . name === 'Spy' ||
694
+ typeAnnotation . typeName . right . name === 'SpyObj' )
695
+
696
+ const transformJasmineTypeToJest = ( type ) => {
697
+ if ( ! type ) return type
698
+
699
+ const typeArgument = type . typeParameters ?. params [ 0 ]
700
+ const jestType = type . typeName . right . name === 'Spy' ? 'Mock' : 'Mocked'
701
+
702
+ return j . tsTypeReference (
703
+ j . tsQualifiedName ( j . identifier ( 'jest' ) , j . identifier ( jestType ) ) ,
704
+ typeArgument ? j . tsTypeParameterInstantiation ( [ typeArgument ] ) : null
705
+ )
706
+ }
695
707
696
- return (
697
- typeName &&
698
- typeName . type === 'TSQualifiedName' &&
699
- typeName . left . name === 'jasmine' &&
700
- ( typeName . right . name === 'Spy' || typeName . right . name === 'SpyObj' )
701
- )
702
- } )
703
- . forEach ( ( path ) => {
704
- const { typeAnnotation } = path . value . id
705
- const { typeName } = typeAnnotation . typeAnnotation
706
- const typeArgument = typeAnnotation . typeAnnotation . typeParameters ?. params [ 0 ]
707
-
708
- switch ( typeName . right . name ) {
709
- case 'Spy' : {
710
- path . value . id . typeAnnotation = j . tsTypeAnnotation (
711
- j . tsTypeReference (
712
- j . tsQualifiedName ( j . identifier ( 'jest' ) , j . identifier ( 'Mock' ) ) ,
713
- typeArgument ? j . tsTypeParameterInstantiation ( [ typeArgument ] ) : null
714
- )
715
- )
716
- break
717
- }
718
- case 'SpyObj' : {
719
- path . value . id . typeAnnotation = j . tsTypeAnnotation (
720
- j . tsTypeReference (
721
- j . tsQualifiedName ( j . identifier ( 'jest' ) , j . identifier ( 'Mocked' ) ) ,
722
- j . tsTypeParameterInstantiation ( [ typeArgument ] )
723
- )
724
- )
725
- break
726
- }
727
- }
728
- } )
708
+ root
709
+ . find ( j . TSTypeReference )
710
+ . filter ( ( path ) => isJasmineSpyType ( path . value ) )
711
+ . forEach ( ( path ) => path . replace ( transformJasmineTypeToJest ( path . value ) ) )
729
712
730
713
root
731
714
. find ( j . CallExpression , {
0 commit comments