@@ -126,25 +126,6 @@ module.exports = function(file, api, options) {
126126 }
127127 }
128128
129- function updateEmberTestHelperImports ( ) {
130- let specifiers = new Set ( ) ;
131-
132- [ 'render' , 'clearRender' ] . forEach ( type => {
133- let usages = findTestHelperUsageOf ( root , type ) ;
134- if ( usages . size ( ) > 0 ) {
135- specifiers . add ( type ) ;
136- }
137- } ) ;
138-
139- if ( specifiers . size > 0 ) {
140- ensureImportWithSpecifiers ( {
141- source : 'ember-test-helpers' ,
142- anchor : 'ember-qunit' ,
143- specifiers,
144- } ) ;
145- }
146- }
147-
148129 function findTestHelperUsageOf ( collection , property ) {
149130 return collection . find ( j . ExpressionStatement , {
150131 expression : {
@@ -268,7 +249,14 @@ module.exports = function(file, api, options) {
268249 let customMethodBeforeEachBody , customMethodBeforeEachExpression ;
269250
270251 options . properties . forEach ( property => {
271- updateGetOwnerThisUsage ( property . value ) ;
252+ if ( setupType ) {
253+ let expressionCollection = j ( property . value ) ;
254+
255+ updateGetOwnerThisUsage ( expressionCollection ) ;
256+ updateLookupCalls ( expressionCollection ) ;
257+ updateRegisterCalls ( expressionCollection ) ;
258+ updateInjectCalls ( expressionCollection ) ;
259+ }
272260
273261 if ( isLifecycleHook ( property ) ) {
274262 needsHooks = true ;
@@ -342,10 +330,13 @@ module.exports = function(file, api, options) {
342330 function processExpressionForRenderingTest ( testExpression ) {
343331 // mark the test function as an async function
344332 let testExpressionCollection = j ( testExpression ) ;
333+ let specifiers = new Set ( ) ;
345334
346335 // Transform to await render() or await clearRender()
347336 [ 'render' , 'clearRender' ] . forEach ( type => {
348337 findTestHelperUsageOf ( testExpressionCollection , type ) . forEach ( p => {
338+ specifiers . add ( type ) ;
339+
349340 let expression = p . get ( 'expression' ) ;
350341
351342 let awaitExpression = j . awaitExpression (
@@ -356,6 +347,12 @@ module.exports = function(file, api, options) {
356347 } ) ;
357348 } ) ;
358349
350+ ensureImportWithSpecifiers ( {
351+ source : 'ember-test-helpers' ,
352+ anchor : 'ember-qunit' ,
353+ specifiers,
354+ } ) ;
355+
359356 // Migrate `this._element` -> `this.element`
360357 testExpressionCollection
361358 . find ( j . MemberExpression , {
@@ -445,39 +442,43 @@ module.exports = function(file, api, options) {
445442 let programPath = root . get ( 'program' ) ;
446443 let bodyPath = programPath . get ( 'body' ) ;
447444
448- let bodyReplacement = [ ] ;
449445 let currentModuleCallbackBody , currentTestType , currentSubject , currentHasCustomSubject ;
450446 bodyPath . each ( expressionPath => {
451447 let expression = expressionPath . node ;
452448 if ( isModuleDefinition ( expressionPath ) ) {
453449 let result = createModule ( expressionPath ) ;
454- bodyReplacement . push ( result [ 0 ] ) ;
450+ expressionPath . replace ( result [ 0 ] ) ;
455451 currentModuleCallbackBody = result [ 1 ] ;
456452 currentTestType = result [ 2 ] ;
457453 currentSubject = result [ 3 ] ;
458454 currentHasCustomSubject = result [ 4 ] ;
459455 } else if ( currentModuleCallbackBody ) {
456+ // calling `path.replace()` essentially just removes
457+ expressionPath . replace ( ) ;
460458 currentModuleCallbackBody . push ( expression ) ;
461459
462460 let isTest = j . match ( expression , { expression : { callee : { name : 'test' } } } ) ;
463461 if ( isTest ) {
464- updateGetOwnerThisUsage ( expression . expression . arguments [ 1 ] ) ;
462+ let expressionCollection = j ( expression ) ;
463+ updateLookupCalls ( expressionCollection ) ;
464+ updateRegisterCalls ( expressionCollection ) ;
465+ updateInjectCalls ( expressionCollection ) ;
466+ // passing the specific function callback here, because `getOwner` is only
467+ // transformed if the call site's scope is the same as the expression passed
468+ updateGetOwnerThisUsage ( j ( expression . expression . arguments [ 1 ] ) ) ;
469+
465470 if ( currentTestType === 'setupRenderingTest' ) {
466471 processExpressionForRenderingTest ( expression ) ;
467472 } else if ( currentTestType === 'setupTest' && ! currentHasCustomSubject ) {
468473 processSubject ( expression , currentSubject ) ;
469474 }
470475 }
471- } else {
472- bodyReplacement . push ( expression ) ;
473476 }
474477 } ) ;
475-
476- bodyPath . replace ( bodyReplacement ) ;
477478 }
478479
479- function updateLookupCalls ( ) {
480- root
480+ function updateLookupCalls ( ctx ) {
481+ ctx
481482 . find ( j . MemberExpression , {
482483 object : {
483484 object : { type : 'ThisExpression' } ,
@@ -491,8 +492,8 @@ module.exports = function(file, api, options) {
491492 } ) ;
492493 }
493494
494- function updateRegisterCalls ( ) {
495- root
495+ function updateRegisterCalls ( ctx ) {
496+ ctx
496497 . find ( j . MemberExpression , {
497498 object : {
498499 object : { type : 'ThisExpression' } ,
@@ -505,7 +506,7 @@ module.exports = function(file, api, options) {
505506 path . replace ( j . memberExpression ( thisDotOwner , path . value . property ) ) ;
506507 } ) ;
507508
508- root
509+ ctx
509510 . find ( j . MemberExpression , {
510511 object : { type : 'ThisExpression' } ,
511512 property : { name : 'register' } ,
@@ -516,8 +517,8 @@ module.exports = function(file, api, options) {
516517 } ) ;
517518 }
518519
519- function updateInjectCalls ( ) {
520- root
520+ function updateInjectCalls ( ctx ) {
521+ ctx
521522 . find ( j . CallExpression , {
522523 callee : {
523524 type : 'MemberExpression' ,
@@ -565,8 +566,8 @@ module.exports = function(file, api, options) {
565566 } ) ;
566567 }
567568
568- function updateGetOwnerThisUsage ( expression ) {
569- let expressionCollection = j ( expression ) ;
569+ function updateGetOwnerThisUsage ( expressionCollection ) {
570+ let expression = expressionCollection . get ( ) . node ;
570571 let thisDotOwner = j . memberExpression ( j . thisExpression ( ) , j . identifier ( 'owner' ) ) ;
571572
572573 function replacement ( path ) {
@@ -625,11 +626,7 @@ module.exports = function(file, api, options) {
625626
626627 moveQUnitImportsFromEmberQUnit ( ) ;
627628 updateToNewEmberQUnitImports ( ) ;
628- updateEmberTestHelperImports ( ) ;
629629 updateModuleForToNestedModule ( ) ;
630- updateLookupCalls ( ) ;
631- updateRegisterCalls ( ) ;
632- updateInjectCalls ( ) ;
633630 updateWaitUsage ( ) ;
634631
635632 return root . toSource ( printOptions ) ;
0 commit comments