@@ -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 ) ;
@@ -117,22 +128,11 @@ module.exports = function(file, api, options) {
117128 . remove ( ) ;
118129
119130 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- ) ;
131+ ensureImportWithSpecifiers ( {
132+ source : 'ember-test-helpers' ,
133+ anchor : 'ember-qunit' ,
134+ specifiers,
135+ } ) ;
136136 }
137137 }
138138
0 commit comments