@@ -2,6 +2,22 @@ module.exports = function(file, api, options) {
22 const j = api . jscodeshift ;
33 const root = j ( file . source ) ;
44
5+ function ensureImportWithSpecifiers ( { source, specifiers, anchor, positionMethod } ) {
6+ let importStatement = ensureImport ( source , anchor , positionMethod ) ;
7+ let combinedSpecifiers = new Set ( specifiers ) ;
8+
9+ importStatement
10+ . find ( j . ImportSpecifier )
11+ . forEach ( i => combinedSpecifiers . add ( i . node . imported . name ) )
12+ . remove ( ) ;
13+
14+ importStatement . get ( 'specifiers' ) . replace (
15+ Array . from ( combinedSpecifiers )
16+ . sort ( )
17+ . map ( s => j . importSpecifier ( j . identifier ( s ) ) )
18+ ) ;
19+ }
20+
521 function ensureImport ( source , anchor , method = 'insertAfter' ) {
622 let desiredImport = root . find ( j . ImportDeclaration , { source : { value : source } } ) ;
723 if ( desiredImport . size ( ) > 0 ) {
@@ -41,14 +57,12 @@ module.exports = function(file, api, options) {
4157 return ;
4258 }
4359
44- let qunitImports = ensureImport ( 'qunit' , 'ember-qunit' , 'insertBefore' ) ;
45- qunitImports . find ( j . ImportSpecifier ) . forEach ( p => specifiers . add ( p . node . imported . name ) ) ;
46-
47- qunitImports . get ( 'specifiers' ) . replace (
48- Array . from ( specifiers )
49- . sort ( )
50- . map ( s => j . importSpecifier ( j . identifier ( s ) ) )
51- ) ;
60+ ensureImportWithSpecifiers ( {
61+ source : 'qunit' ,
62+ anchor : 'ember-qunit' ,
63+ positionMethod : 'insertBefore' ,
64+ specifiers,
65+ } ) ;
5266 }
5367
5468 function updateToNewEmberQUnitImports ( ) {
@@ -97,9 +111,6 @@ module.exports = function(file, api, options) {
97111
98112 function updateEmberTestHelperImports ( ) {
99113 let specifiers = new Set ( ) ;
100- let emberTestHelpersImport = root . find ( j . ImportDeclaration , {
101- source : { value : 'ember-test-helpers' } ,
102- } ) ;
103114
104115 [ 'render' , 'clearRender' ] . forEach ( type => {
105116 let usages = findTestHelperUsageOf ( root , type ) ;
@@ -108,31 +119,12 @@ module.exports = function(file, api, options) {
108119 }
109120 } ) ;
110121
111- root
112- . find ( j . ImportDeclaration , { source : { value : 'ember-test-helpers/wait' } } )
113- . forEach ( p => {
114- specifiers . add ( 'settled' ) ;
115- return p ;
116- } )
117- . remove ( ) ;
118-
119122 if ( specifiers . size > 0 ) {
120- if ( emberTestHelpersImport . size ( ) > 0 ) {
121- // collect existing imports
122- emberTestHelpersImport
123- . find ( j . ImportSpecifier )
124- . forEach ( p => specifiers . add ( p . node . imported . name ) )
125- . remove ( ) ;
126- } else {
127- // Add new `import from 'ember-test-helpers'` node
128- emberTestHelpersImport = ensureImport ( 'ember-test-helpers' , 'ember-qunit' ) ;
129- }
130-
131- emberTestHelpersImport . get ( 'specifiers' ) . replace (
132- Array . from ( specifiers )
133- . sort ( )
134- . map ( s => j . importSpecifier ( j . identifier ( s ) ) )
135- ) ;
123+ ensureImportWithSpecifiers ( {
124+ source : 'ember-test-helpers' ,
125+ anchor : 'ember-qunit' ,
126+ specifiers,
127+ } ) ;
136128 }
137129 }
138130
@@ -548,10 +540,27 @@ module.exports = function(file, api, options) {
548540 . forEach ( replacement ) ;
549541 }
550542
551- function updateWaitCalls ( ) {
552- root . find ( j . CallExpression , { callee : { name : 'wait' } } ) . forEach ( p => {
553- p . node . callee . name = 'settled' ;
543+ function updateWaitUsage ( ) {
544+ let waitImport = root . find ( j . ImportDeclaration , {
545+ source : { value : 'ember-test-helpers/wait' } ,
554546 } ) ;
547+
548+ if ( waitImport . size ( ) > 0 ) {
549+ let importedName ;
550+
551+ ensureImportWithSpecifiers ( {
552+ source : 'ember-test-helpers' ,
553+ anchor : 'ember-qunit' ,
554+ specifiers : [ 'settled' ] ,
555+ } ) ;
556+
557+ waitImport . find ( j . ImportDefaultSpecifier ) . forEach ( p => ( importedName = p . node . local . name ) ) ;
558+ waitImport . remove ( ) ;
559+
560+ root . find ( j . CallExpression , { callee : { name : importedName } } ) . forEach ( p => {
561+ p . node . callee . name = 'settled' ;
562+ } ) ;
563+ }
555564 }
556565
557566 const printOptions = options . printOptions || { quote : 'single' } ;
@@ -567,8 +576,9 @@ module.exports = function(file, api, options) {
567576 updateInjectCalls ( ) ;
568577 } else {
569578 updateEmberTestHelperImports ( ) ;
570- updateWaitCalls ( ) ;
571579 }
572580
581+ updateWaitUsage ( ) ;
582+
573583 return root . toSource ( printOptions ) ;
574584} ;
0 commit comments