@@ -22,14 +22,12 @@ function writeImportStatement(j, root, importsToReplace) {
2222
2323 // if import statement exists, remove it and replace with a new import
2424 if ( importStatement . length !== 0 ) {
25- replaceWithNewImport ( j , root , importStatement ) ;
25+ replaceImports ( j , root , importStatement ) ;
2626 }
2727 } ) ;
2828}
2929
30- function replaceWithNewImport ( j , root , oldImport ) {
31- let body = root . get ( ) . value . program . body ;
32-
30+ function replaceImports ( j , root , oldImport ) {
3331 // setting up new import information
3432 let namedIdentifier = oldImport . find ( j . Identifier ) . get ( 0 ) . node . name ;
3533 if ( namedIdentifier !== 'hbs' ) {
@@ -48,16 +46,29 @@ function replaceWithNewImport(j, root, oldImport) {
4846 } ,
4947 } ) ;
5048
51- // if no imports from 'ember-cli-htmlbars' exists, write one ;
49+ // if no imports from 'ember-cli-htmlbars' exists, write a new import delcaration ;
5250 if ( emberCliImportStatement . length === 0 ) {
53- const importStatement = j . importDeclaration ( [ variableId ] , j . literal ( 'ember-cli-htmlbars' ) ) ;
54- body . unshift ( importStatement ) ;
51+ createNewImport ( j , root , variableId ) ;
5552 }
56- // if any imports from 'ember-cli-htmlbars' already exists, include hbs
53+ // if any imports from 'ember-cli-htmlbars' exist
5754 else {
5855 let existingSpecifiers = emberCliImportStatement . get ( 'specifiers' ) ;
59- if ( existingSpecifiers . filter ( exSp => exSp . value . imported . name === 'hbs' ) . length === 0 ) {
56+
57+ // if 'hbs' is already being imported from 'ember-cli-htmlbars', write a new import declaration
58+ if ( existingSpecifiers . filter ( exSp => exSp . value . imported . name . includes ( 'hbs' ) ) . length > 0 ) {
59+ createNewImport ( j , root , variableId ) ;
60+ }
61+ // otherwise, add hbs import to existing 'ember-cli-htmlbars' import declaration
62+ else {
6063 existingSpecifiers . push ( variableId ) ;
6164 }
6265 }
6366}
67+
68+ function createNewImport ( j , root , variableId , importLiteral = 'ember-cli-htmlbars' ) {
69+ let body = root . get ( ) . value . program . body ;
70+
71+ // write a new import delcaration
72+ const importStatement = j . importDeclaration ( [ variableId ] , j . literal ( importLiteral ) ) ;
73+ body . unshift ( importStatement ) ;
74+ }
0 commit comments