Skip to content

Commit b2d1c84

Browse files
ssutarChris Garrett
authored andcommitted
Merge the import statements (#116)
1 parent f2b7bde commit b2d1c84

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

transforms/ember-object/__testfixtures__/decorators.output.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { attribute, className, classNames, tagName, layout as templateLayout } from "@ember-decorators/component";
22
import { observes as watcher, on } from "@ember-decorators/object";
3-
import { action, computed } from "@ember/object";
43
import { inject as controller } from "@ember/controller";
54
import { inject as service } from "@ember/service";
65

@@ -16,7 +15,7 @@ import {
1615
alias,
1716
} from "@ember/object/computed";
1817

19-
import { get, set } from "@ember/object";
18+
import { get, set, action, computed } from "@ember/object";
2019
import layout from "components/templates/foo";
2120
import { someActionUtil } from "some/action/util";
2221
import NUMERIC_CONSTANT from "constants/numbers";

transforms/helpers/import-helper.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ function getDecoratorPathSpecifiers(j, root, decoratorsToImport = []) {
251251
);
252252
}
253253

254+
/**
255+
* Get existing import statement matching the import path
256+
*
257+
* @param {Object} j - jscodeshift lib reference
258+
* @param {File} root
259+
* @param {String} importPath
260+
* @returns {Object}
261+
*/
262+
function getExistingImportForPath(j, root, importPath) {
263+
const decoratorImports = root.find(j.ImportDeclaration, {
264+
source: {
265+
value: importPath
266+
}
267+
});
268+
269+
if (decoratorImports.length) {
270+
return decoratorImports.get();
271+
}
272+
}
273+
254274
/**
255275
* Create the import statements for decorators
256276
*
@@ -276,9 +296,20 @@ function createDecoratorImportDeclarations(j, root, decoratorsToImport = []) {
276296
// Create import statement replacing the existing ones with specifiers importing from ember-decorators namespace
277297
decoratorPathsImported.forEach(decoratorPath => {
278298
const specifiers = decoratorPathSpecifierMap[decoratorPath];
279-
firstDeclaration.insertBefore(
280-
createImportDeclaration(j, specifiers, decoratorPath)
281-
);
299+
const existingImport = getExistingImportForPath(j, root, decoratorPath);
300+
if (existingImport) {
301+
let existingSpecifiers = get(existingImport, "value.specifiers");
302+
if (existingSpecifiers) {
303+
existingImport.value.specifiers = [].concat(
304+
existingSpecifiers,
305+
specifiers
306+
);
307+
}
308+
} else {
309+
firstDeclaration.insertBefore(
310+
createImportDeclaration(j, specifiers, decoratorPath)
311+
);
312+
}
282313
});
283314

284315
// Create new import declarations

0 commit comments

Comments
 (0)