Skip to content

Commit b68e861

Browse files
authored
Merge pull request #23 from simonihmig/fix-double-imports
Fix native-dom transform for multiple import statements
2 parents 174168b + bdbc628 commit b68e861

5 files changed

Lines changed: 30 additions & 22 deletions

File tree

__testfixtures__/native-dom/acceptance.input.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { currentURL, currentPath, currentRouteName, find, visit } from 'ember-native-dom-helpers';
1+
import { find, visit } from 'ember-native-dom-helpers';
2+
import { currentURL, currentPath, currentRouteName } from 'ember-native-dom-helpers';
23
import { test } from 'qunit';
34
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
45

__testfixtures__/native-dom/acceptance.output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { currentURL, currentRouteName, find, visit } from '@ember/test-helpers';
1+
import { find, visit, currentURL, currentRouteName } from '@ember/test-helpers';
22
import { currentPath } from 'ember-native-dom-helpers';
33
import { test } from 'qunit';
44
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { click, currentURL } from 'ember-native-dom-helpers';
2+
import { find, visit } from 'ember-native-dom-helpers';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { click, currentURL, find, visit } from '@ember/test-helpers';

lib/transforms/native-dom/migrate-imports.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,41 @@ function transform(file, api) {
4949
let j = api.jscodeshift;
5050
let root = j(source);
5151

52-
let nativeDomImportStatement = root.find(j.ImportDeclaration, {
52+
let nativeDomImportStatements = root.find(j.ImportDeclaration, {
5353
source: { value: 'ember-native-dom-helpers' }
5454
});
55-
if (nativeDomImportStatement.length === 0) {
55+
if (nativeDomImportStatements.length === 0) {
5656
return root.toSource({ quote: 'single' });
5757
}
5858

5959
let newImports = [];
60-
let oldSpecifiers = nativeDomImportStatement.get('specifiers');
61-
let newSpecifiers = [];
62-
oldSpecifiers.each(({ node: specifier }) => {
63-
let importedName = specifier.imported.name;
64-
if (importedName in importMigrationsLookup) {
65-
let mappedName = importMigrationsLookup[importedName];
66-
// @todo local != imported
67-
// let localName = specifier.local.name;
68-
newImports.push(mappedName);
69-
if (importedName !== mappedName) {
70-
renameCallee(j, root, importedName, mappedName);
60+
61+
nativeDomImportStatements.forEach((importStatement) => {
62+
let oldSpecifiers = importStatement.get('specifiers');
63+
64+
let newSpecifiers = [];
65+
oldSpecifiers.each(({ node: specifier }) => {
66+
let importedName = specifier.imported.name;
67+
if (importedName in importMigrationsLookup) {
68+
let mappedName = importMigrationsLookup[importedName];
69+
// @todo local != imported
70+
// let localName = specifier.local.name;
71+
newImports.push(mappedName);
72+
if (importedName !== mappedName) {
73+
renameCallee(j, root, importedName, mappedName);
74+
}
75+
} else {
76+
newSpecifiers.push(specifier);
7177
}
78+
});
79+
80+
if (newSpecifiers.length > 0) {
81+
oldSpecifiers.replace(newSpecifiers);
7282
} else {
73-
newSpecifiers.push(specifier);
83+
importStatement.prune();
7484
}
7585
});
7686

77-
if (newSpecifiers.length > 0) {
78-
oldSpecifiers.replace(newSpecifiers);
79-
} else {
80-
nativeDomImportStatement.remove();
81-
}
82-
8387
addImportStatement(newImports);
8488
writeImportStatements(j, root);
8589
return root.toSource({ quote: 'single' });

0 commit comments

Comments
 (0)