Skip to content

Commit ecf7b72

Browse files
committed
different line imports
1 parent 3e27084 commit ecf7b72

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
import { hbs as h, hbs } from 'ember-cli-htmlbars';
1+
import { hbs } from 'ember-cli-htmlbars';
2+
import { hbs as h } from 'ember-cli-htmlbars';

transforms/convert-htmlbars-import/index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)