Skip to content

Commit a746c6e

Browse files
committed
non-standard imported names
1 parent 8d923ae commit a746c6e

4 files changed

Lines changed: 28 additions & 7 deletions

File tree

transforms/convert-htmlbars-import/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ember-cli-htmlbars-inline-precompile-codemod path/of/files/ or/some**/*
1616

1717
<!--FIXTURES_TOC_START-->
1818
* [convert-htmlbars-import](#convert-htmlbars-import)
19+
* [non-standard-imported-name](#non-standard-imported-name)
1920
<!--FIXTURES_TOC_END-->
2021

2122
<!--FIXTURES_CONTENT_START-->
@@ -32,5 +33,19 @@ import hbs from 'htmlbars-inline-precompile';
3233
```js
3334
import { hbs } from 'ember-cli-htmlbars';
3435

36+
```
37+
---
38+
<a id="non-standard-imported-name">**non-standard-imported-name**</a>
39+
40+
**Input** (<small>[non-standard-imported-name.input.js](transforms/convert-htmlbars-import/__testfixtures__/non-standard-imported-name.input.js)</small>):
41+
```js
42+
import h from 'htmlbars-inline-precompile';
43+
44+
```
45+
46+
**Output** (<small>[non-standard-imported-name.output.js](transforms/convert-htmlbars-import/__testfixtures__/non-standard-imported-name.output.js)</small>):
47+
```js
48+
import { hbs as h } from 'ember-cli-htmlbars';
49+
3550
```
3651
<!--FIXTURES_CONTENT_END-->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import h from 'htmlbars-inline-precompile';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { hbs as h } from 'ember-cli-htmlbars';

transforms/convert-htmlbars-import/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ function writeImportStatement(j, root) {
2121

2222
// if any imports from 'htmlbars-inline-precompile' exists, remove it and replace with a new import
2323
if (inlinePrecompileImportStatement.length !== 0) {
24-
inlinePrecompileImportStatement.remove();
24+
// setting up import information
25+
let namedIdentifier = inlinePrecompileImportStatement.find(j.Identifier).get(0).node.name;
26+
if (namedIdentifier !== 'hbs') {
27+
namedIdentifier = `hbs as ${namedIdentifier}`;
28+
}
29+
const identifier = j.identifier(namedIdentifier);
30+
const variableId = j.importSpecifier(identifier);
2531

26-
// setting up 'hbs' as an identifier for import statement
27-
const hbs = 'hbs';
28-
const hbsAsIdentifier = j.identifier(hbs);
29-
const variableId = j.importSpecifier(hbsAsIdentifier);
32+
// remove imports from 'htmlbars-inline-precompile'
33+
inlinePrecompileImportStatement.remove();
3034

3135
// finding all 'ember-cli-htmlbars' import statements
3236
const emberCliImportStatement = root.find(j.ImportDeclaration, {
@@ -35,15 +39,15 @@ function writeImportStatement(j, root) {
3539
},
3640
});
3741

38-
// if no imports from 'ember-cli-htmlbars' exists, write import { hbs } from 'ember-cli-htmlbars';
42+
// if no imports from 'ember-cli-htmlbars' exists, write one;
3943
if (emberCliImportStatement.length === 0) {
4044
const importStatement = j.importDeclaration([variableId], j.literal('ember-cli-htmlbars'));
4145
body.unshift(importStatement);
4246
}
4347
// if any imports from 'ember-cli-htmlbars' already exists, include hbs
4448
else {
4549
let existingSpecifiers = emberCliImportStatement.get('specifiers');
46-
if (existingSpecifiers.filter(exSp => exSp.value.imported.name === hbs).length === 0) {
50+
if (existingSpecifiers.filter(exSp => exSp.value.imported.name === 'hbs').length === 0) {
4751
existingSpecifiers.push(variableId);
4852
}
4953
}

0 commit comments

Comments
 (0)