@@ -183,35 +183,54 @@ export function transformSpies(node: ts.Node, refactorCtx: RefactorContext): ts.
183183 }
184184 }
185185
186- const jasmineMethodName = getJasmineMethodName ( node ) ;
187- switch ( jasmineMethodName ) {
188- case 'createSpy' :
189- addVitestValueImport ( pendingVitestValueImports , 'vi' ) ;
190- reporter . reportTransformation (
191- sourceFile ,
192- node ,
193- 'Transformed `jasmine.createSpy()` to `vi.fn()`.' ,
194- ) ;
195-
196- // jasmine.createSpy(name, originalFn) -> vi.fn(originalFn)
197- return createViCallExpression ( 'fn' , node . arguments . length > 1 ? [ node . arguments [ 1 ] ] : [ ] ) ;
198- case 'spyOnAllFunctions' : {
199- reporter . reportTransformation (
200- sourceFile ,
201- node ,
202- 'Found unsupported `jasmine.spyOnAllFunctions()`.' ,
203- ) ;
204- const category = 'spyOnAllFunctions' ;
205- reporter . recordTodo ( category , sourceFile , node ) ;
206- addTodoComment ( node , category ) ;
186+ if ( getJasmineMethodName ( node ) === 'spyOnAllFunctions' ) {
187+ reporter . reportTransformation (
188+ sourceFile ,
189+ node ,
190+ 'Found unsupported `jasmine.spyOnAllFunctions()`.' ,
191+ ) ;
192+ const category = 'spyOnAllFunctions' ;
193+ reporter . recordTodo ( category , sourceFile , node ) ;
194+ addTodoComment ( node , category ) ;
207195
208- return node ;
209- }
196+ return node ;
210197 }
211198
212199 return node ;
213200}
214201
202+ export function transformCreateSpy (
203+ node : ts . Node ,
204+ { reporter, sourceFile, pendingVitestValueImports } : RefactorContext ,
205+ ) : ts . Node {
206+ if ( ! isJasmineCallExpression ( node , 'createSpy' ) ) {
207+ return node ;
208+ }
209+
210+ addVitestValueImport ( pendingVitestValueImports , 'vi' ) ;
211+ reporter . reportTransformation (
212+ sourceFile ,
213+ node ,
214+ 'Transformed `jasmine.createSpy()` to `vi.fn()`.' ,
215+ ) ;
216+
217+ const spyName = node . arguments [ 0 ] ;
218+ const viFnCallExpression = createViCallExpression (
219+ 'fn' ,
220+ node . arguments . length > 1 ? [ node . arguments [ 1 ] ] : [ ] ,
221+ ) ;
222+
223+ // jasmine.createSpy() -> vi.fn()
224+ // jasmine.createSpy(name, originalFn) -> vi.fn(originalFn).mockName(name)
225+ return ! spyName
226+ ? viFnCallExpression
227+ : ts . factory . createCallExpression (
228+ createPropertyAccess ( viFnCallExpression , 'mockName' ) ,
229+ undefined ,
230+ [ node . arguments [ 0 ] ] ,
231+ ) ;
232+ }
233+
215234export function transformCreateSpyObj (
216235 node : ts . Node ,
217236 { sourceFile, reporter, pendingVitestValueImports } : RefactorContext ,
@@ -428,12 +447,56 @@ function transformMostRecentArgs(
428447 return createPropertyAccess ( mockProperty , 'lastCall' ) ;
429448}
430449
450+ function transformThisFor (
451+ node : ts . Node ,
452+ { sourceFile, reporter, pendingVitestValueImports } : RefactorContext ,
453+ ) : ts . Node {
454+ // Check 1: Is the node is a call expression?
455+ if ( ! ts . isCallExpression ( node ) || ! ts . isPropertyAccessExpression ( node . expression ) ) {
456+ return node ;
457+ }
458+
459+ // Check 2: Is it a call to `.thisFor`?
460+ const thisForPae = node . expression ;
461+ if (
462+ ! ts . isIdentifier ( thisForPae . name ) ||
463+ thisForPae . name . text !== 'thisFor' ||
464+ ! ts . isPropertyAccessExpression ( thisForPae . expression )
465+ ) {
466+ return node ;
467+ }
468+
469+ // Check 3: Can we get the spy identifier from `spy.calls`?
470+ const spyIdentifier = getSpyIdentifierFromCalls ( thisForPae . expression ) ;
471+ if ( ! spyIdentifier ) {
472+ return node ;
473+ }
474+
475+ // If all checks pass, perform the transformation.
476+ reporter . reportTransformation (
477+ sourceFile ,
478+ node ,
479+ 'Transformed `spy.calls.thisFor(index)` to `vi.mocked(spy).mock.contexts[index]`.' ,
480+ ) ;
481+ const mockProperty = createMockedSpyMockProperty ( spyIdentifier , pendingVitestValueImports ) ;
482+
483+ return ts . factory . createElementAccessExpression (
484+ createPropertyAccess ( mockProperty , 'contexts' ) ,
485+ node . arguments [ 0 ] ?? 0 ,
486+ ) ;
487+ }
488+
431489export function transformSpyCallInspection ( node : ts . Node , refactorCtx : RefactorContext ) : ts . Node {
432490 const mostRecentArgsTransformed = transformMostRecentArgs ( node , refactorCtx ) ;
433491 if ( mostRecentArgsTransformed !== node ) {
434492 return mostRecentArgsTransformed ;
435493 }
436494
495+ const thisForTransformed = transformThisFor ( node , refactorCtx ) ;
496+ if ( thisForTransformed !== node ) {
497+ return thisForTransformed ;
498+ }
499+
437500 if ( ! ts . isCallExpression ( node ) || ! ts . isPropertyAccessExpression ( node . expression ) ) {
438501 return node ;
439502 }
0 commit comments