From 61f273b5eb8470f646b15c7b8af3d114dfcf3188 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Tue, 23 Jul 2019 22:28:47 -0700 Subject: [PATCH 01/15] Update literal paths --- __testfixtures__/literal.input.js | 4 ++++ __testfixtures__/literal.output.js | 5 +++++ bin/ember-data-codemod.js | 2 +- transforms/globals-to-ember-data-imports.js | 23 ++++++++++++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 __testfixtures__/literal.input.js create mode 100644 __testfixtures__/literal.output.js diff --git a/__testfixtures__/literal.input.js b/__testfixtures__/literal.input.js new file mode 100644 index 0000000..33a1297 --- /dev/null +++ b/__testfixtures__/literal.input.js @@ -0,0 +1,4 @@ +import Model, { attrs } from 'ember-data/model'; +import { hasMany, belongsTo } from 'ember-data/relationships'; +import JSONAPIAdapter from 'ember-data/adapters/json-api'; +import { InvalidError, ServerError, TimeoutError, NotFoundError } from 'ember-data/adapter/error'; diff --git a/__testfixtures__/literal.output.js b/__testfixtures__/literal.output.js new file mode 100644 index 0000000..d02c969 --- /dev/null +++ b/__testfixtures__/literal.output.js @@ -0,0 +1,5 @@ +import Model, { attrs } from '@ember-data/model'; +import { hasMany, belongsTo } from '@ember-data/model'; +import JSONAPIAdapter from '@ember-data/adapter/json-api'; +import { InvalidError, ServerError, TimeoutError, NotFoundError } from '@ember-data/adapter/error'; + diff --git a/bin/ember-data-codemod.js b/bin/ember-data-codemod.js index c1c123f..b1654c0 100644 --- a/bin/ember-data-codemod.js +++ b/bin/ember-data-codemod.js @@ -89,7 +89,7 @@ function buildReport() { // Find all of the temporary logs from the worker processes, which contain a // serialized JSON array on each line. - glob('ember-modules-codemod.tmp.*', (err, logs) => { + glob('ember-data-codemod.tmp.*', (err, logs) => { // If no worker found an unexpected value, nothing to report. if (!logs) { return; diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index 97064e9..31d50bb 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -64,6 +64,9 @@ function transform(file, api /*, options*/) { findReplacement(mappings) ); + // Update literal paths based on mappings from 'ember-data/model' to '@ember-data/model' + updateLiteralPaths(root, mappings, modules) + // Now that we've identified all of the replacements that we need to do, we'll // make sure to either add new `import` declarations, or update existing ones // to add new named exports or the default export. @@ -157,6 +160,25 @@ function transform(file, api /*, options*/) { return dsUsages.filter(isDSGlobal(globalDS)).paths(); } + function updateLiteralPaths(root, mappings, registry) { + return registry.modules.map((module) => { + let foundMapping = mappings[module.local]; + if (foundMapping) { + let newSource = foundMapping.source; + root.find(j.ImportDeclaration, { + source: { + type: 'Literal', + value: module.source + } + }) + .find(j.Literal) + .forEach((importLiteral) => { + j(importLiteral).replaceWith(j.literal(newSource)) + }); + } + }) + } + // Find destructured global aliases for fields on the DS global function findGlobalDSAliases(root, globalDS, mappings) { let aliases = {}; @@ -448,7 +470,6 @@ function transform(file, api /*, options*/) { let declaration = root.find(j.ImportDeclaration, { source: { value: mod.source } }); - if (declaration.size() > 0) { let specifier; From eed8f1e7ecc877982ab8f4aae088772d153e9279 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Tue, 23 Jul 2019 22:38:48 -0700 Subject: [PATCH 02/15] ensure replace if source is different --- __testfixtures__/literal.input.js | 3 ++- __testfixtures__/literal.output.js | 3 ++- transforms/globals-to-ember-data-imports.js | 24 ++++++++++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/__testfixtures__/literal.input.js b/__testfixtures__/literal.input.js index 33a1297..04b212f 100644 --- a/__testfixtures__/literal.input.js +++ b/__testfixtures__/literal.input.js @@ -1,4 +1,5 @@ -import Model, { attrs } from 'ember-data/model'; +import Model, { attr } from 'ember-data/model'; import { hasMany, belongsTo } from 'ember-data/relationships'; import JSONAPIAdapter from 'ember-data/adapters/json-api'; import { InvalidError, ServerError, TimeoutError, NotFoundError } from 'ember-data/adapter/error'; +import Transform from '@ember-data/serializer/transform'; diff --git a/__testfixtures__/literal.output.js b/__testfixtures__/literal.output.js index d02c969..05aeafd 100644 --- a/__testfixtures__/literal.output.js +++ b/__testfixtures__/literal.output.js @@ -1,5 +1,6 @@ -import Model, { attrs } from '@ember-data/model'; +import Model, { attr } from '@ember-data/model'; import { hasMany, belongsTo } from '@ember-data/model'; import JSONAPIAdapter from '@ember-data/adapter/json-api'; import { InvalidError, ServerError, TimeoutError, NotFoundError } from '@ember-data/adapter/error'; +import Transform from '@ember-data/serializer/transform'; diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index 31d50bb..eef12e6 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -160,21 +160,25 @@ function transform(file, api /*, options*/) { return dsUsages.filter(isDSGlobal(globalDS)).paths(); } + /* + */ function updateLiteralPaths(root, mappings, registry) { return registry.modules.map((module) => { let foundMapping = mappings[module.local]; if (foundMapping) { let newSource = foundMapping.source; - root.find(j.ImportDeclaration, { - source: { - type: 'Literal', - value: module.source - } - }) - .find(j.Literal) - .forEach((importLiteral) => { - j(importLiteral).replaceWith(j.literal(newSource)) - }); + if (module.source !== newSource) { + root.find(j.ImportDeclaration, { + source: { + type: 'Literal', + value: module.source + } + }) + .find(j.Literal) + .forEach((importLiteral) => { + j(importLiteral).replaceWith(j.literal(newSource)) + }); + } } }) } From 5acf6c94cfc18fca30fa6aea974f824123adf2ae Mon Sep 17 00:00:00 2001 From: snewcomer Date: Tue, 23 Jul 2019 22:39:39 -0700 Subject: [PATCH 03/15] add note --- transforms/globals-to-ember-data-imports.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index eef12e6..ae10ac4 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -161,6 +161,8 @@ function transform(file, api /*, options*/) { } /* + * loops through all modules and replaces literal path if necessary + * 'ember-data/model' -> '@ember-data/model' */ function updateLiteralPaths(root, mappings, registry) { return registry.modules.map((module) => { From 6341cf19d613d10fec93ad107b8d09f39390ef98 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Tue, 23 Jul 2019 22:40:15 -0700 Subject: [PATCH 04/15] remove space; --- __testfixtures__/literal.output.js | 1 - 1 file changed, 1 deletion(-) diff --git a/__testfixtures__/literal.output.js b/__testfixtures__/literal.output.js index 05aeafd..b8bc129 100644 --- a/__testfixtures__/literal.output.js +++ b/__testfixtures__/literal.output.js @@ -3,4 +3,3 @@ import { hasMany, belongsTo } from '@ember-data/model'; import JSONAPIAdapter from '@ember-data/adapter/json-api'; import { InvalidError, ServerError, TimeoutError, NotFoundError } from '@ember-data/adapter/error'; import Transform from '@ember-data/serializer/transform'; - From 03293272ce5236757074918698cdc4a498f048d4 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Tue, 23 Jul 2019 22:47:08 -0700 Subject: [PATCH 05/15] refactor to do in registry modules loop instead of own funciton --- transforms/globals-to-ember-data-imports.js | 44 ++++++++++----------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index ae10ac4..200db0f 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -64,13 +64,10 @@ function transform(file, api /*, options*/) { findReplacement(mappings) ); - // Update literal paths based on mappings from 'ember-data/model' to '@ember-data/model' - updateLiteralPaths(root, mappings, modules) - // Now that we've identified all of the replacements that we need to do, we'll // make sure to either add new `import` declarations, or update existing ones // to add new named exports or the default export. - updateOrCreateImportDeclarations(root, modules); + updateOrCreateImportDeclarations(root, mappings, modules); // Actually go through and replace each usage of `DS.whatever` with the // imported binding (`whatever`). @@ -164,25 +161,23 @@ function transform(file, api /*, options*/) { * loops through all modules and replaces literal path if necessary * 'ember-data/model' -> '@ember-data/model' */ - function updateLiteralPaths(root, mappings, registry) { - return registry.modules.map((module) => { - let foundMapping = mappings[module.local]; - if (foundMapping) { - let newSource = foundMapping.source; - if (module.source !== newSource) { - root.find(j.ImportDeclaration, { - source: { - type: 'Literal', - value: module.source - } - }) - .find(j.Literal) - .forEach((importLiteral) => { - j(importLiteral).replaceWith(j.literal(newSource)) - }); - } + function updateLiteralPaths(root, module, mappings) { + let foundMapping = mappings[module.local]; + if (foundMapping) { + let newSource = foundMapping.source; + if (module.source !== newSource) { + root.find(j.ImportDeclaration, { + source: { + type: 'Literal', + value: module.source + } + }) + .find(j.Literal) + .forEach((importLiteral) => { + j(importLiteral).replaceWith(j.literal(newSource)) + }); } - }) + } } // Find destructured global aliases for fields on the DS global @@ -464,7 +459,7 @@ function transform(file, api /*, options*/) { return parent.node.id.name === local; } - function updateOrCreateImportDeclarations(root, registry) { + function updateOrCreateImportDeclarations(root, mappings, registry) { let body = root.get().value.program.body; registry.modules.forEach(mod => { @@ -498,6 +493,9 @@ function transform(file, api /*, options*/) { delete body[1].comments; mod.node = importStatement; } + } else { + // Update literal paths based on mappings from 'ember-data/model' to '@ember-data/model' + updateLiteralPaths(root, mod, mappings) } }); } From 6febc3a65b45a9f3227337a096774c7c4f064de5 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Tue, 23 Jul 2019 22:48:55 -0700 Subject: [PATCH 06/15] switch order of args --- transforms/globals-to-ember-data-imports.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index 200db0f..3562072 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -67,7 +67,7 @@ function transform(file, api /*, options*/) { // Now that we've identified all of the replacements that we need to do, we'll // make sure to either add new `import` declarations, or update existing ones // to add new named exports or the default export. - updateOrCreateImportDeclarations(root, mappings, modules); + updateOrCreateImportDeclarations(root, modules, mappings); // Actually go through and replace each usage of `DS.whatever` with the // imported binding (`whatever`). @@ -459,7 +459,7 @@ function transform(file, api /*, options*/) { return parent.node.id.name === local; } - function updateOrCreateImportDeclarations(root, mappings, registry) { + function updateOrCreateImportDeclarations(root, registry, mappings) { let body = root.get().value.program.body; registry.modules.forEach(mod => { From 47d5d20ff847be5f0d5ebc4afdd8e83f81518f07 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Wed, 24 Jul 2019 07:06:17 -0700 Subject: [PATCH 07/15] fix lint --- transforms/globals-to-ember-data-imports.js | 23 +++++++++++---------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index 3562072..db137f2 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -166,16 +166,17 @@ function transform(file, api /*, options*/) { if (foundMapping) { let newSource = foundMapping.source; if (module.source !== newSource) { - root.find(j.ImportDeclaration, { - source: { - type: 'Literal', - value: module.source - } - }) - .find(j.Literal) - .forEach((importLiteral) => { - j(importLiteral).replaceWith(j.literal(newSource)) - }); + root + .find(j.ImportDeclaration, { + source: { + type: 'Literal', + value: module.source + } + }) + .find(j.Literal) + .forEach(importLiteral => { + j(importLiteral).replaceWith(j.literal(newSource)); + }); } } } @@ -495,7 +496,7 @@ function transform(file, api /*, options*/) { } } else { // Update literal paths based on mappings from 'ember-data/model' to '@ember-data/model' - updateLiteralPaths(root, mod, mappings) + updateLiteralPaths(root, mod, mappings); } }); } From e3d00f85b75307112a76962edffc6d62076e4364 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Wed, 24 Jul 2019 09:51:45 -0700 Subject: [PATCH 08/15] udpate successful message --- bin/ember-data-codemod.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/ember-data-codemod.js b/bin/ember-data-codemod.js index b1654c0..5ebabc1 100644 --- a/bin/ember-data-codemod.js +++ b/bin/ember-data-codemod.js @@ -11,7 +11,7 @@ let cwd = process.cwd(); let pkgPath = path.join(cwd, 'package.json'); const DEFAULT_PATHS = [ - 'app', + 'app' 'addon', 'addon-test-support', 'tests', @@ -143,7 +143,7 @@ function buildReport() { ); } else { console.log( - chalk.green('\nDone! All uses of the Ember global have been updated.') + chalk.green('\nDone! All uses of the Ember global and Ember Data imports have been updated.') ); } }); From 5ee072918bf70ec6af6d76059f5c91fb84168416 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Wed, 24 Jul 2019 10:38:21 -0700 Subject: [PATCH 09/15] actually... --- __testfixtures__/literal.output.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/__testfixtures__/literal.output.js b/__testfixtures__/literal.output.js index b8bc129..ac6f414 100644 --- a/__testfixtures__/literal.output.js +++ b/__testfixtures__/literal.output.js @@ -1,5 +1,4 @@ -import Model, { attr } from '@ember-data/model'; -import { hasMany, belongsTo } from '@ember-data/model'; +import Model, { attr, belongsTo, hasMany } from '@ember-data/model'; import JSONAPIAdapter from '@ember-data/adapter/json-api'; import { InvalidError, ServerError, TimeoutError, NotFoundError } from '@ember-data/adapter/error'; import Transform from '@ember-data/serializer/transform'; From 2699708fd2fa5d1f1b728d5e06bb006c096b1840 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Wed, 24 Jul 2019 10:52:46 -0700 Subject: [PATCH 10/15] fix --- bin/ember-data-codemod.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ember-data-codemod.js b/bin/ember-data-codemod.js index 5ebabc1..135483c 100644 --- a/bin/ember-data-codemod.js +++ b/bin/ember-data-codemod.js @@ -11,7 +11,7 @@ let cwd = process.cwd(); let pkgPath = path.join(cwd, 'package.json'); const DEFAULT_PATHS = [ - 'app' + 'app', 'addon', 'addon-test-support', 'tests', From 258d8d8195ba6028dc60ec8b85e8a59e236a9467 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Wed, 24 Jul 2019 23:16:19 -0700 Subject: [PATCH 11/15] Ugly way first --- __testfixtures__/literal.output.js | 2 +- transforms/globals-to-ember-data-imports.js | 60 +++++++++++++++++++-- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/__testfixtures__/literal.output.js b/__testfixtures__/literal.output.js index ac6f414..1e4771a 100644 --- a/__testfixtures__/literal.output.js +++ b/__testfixtures__/literal.output.js @@ -1,4 +1,4 @@ -import Model, { attr, belongsTo, hasMany } from '@ember-data/model'; +import Model, { attr, hasMany, belongsTo } from '@ember-data/model'; import JSONAPIAdapter from '@ember-data/adapter/json-api'; import { InvalidError, ServerError, TimeoutError, NotFoundError } from '@ember-data/adapter/error'; import Transform from '@ember-data/serializer/transform'; diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index db137f2..ca84745 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -161,8 +161,9 @@ function transform(file, api /*, options*/) { * loops through all modules and replaces literal path if necessary * 'ember-data/model' -> '@ember-data/model' */ - function updateLiteralPaths(root, module, mappings) { + function updateExistingLiteralPaths(root, module, mappings) { let foundMapping = mappings[module.local]; + if (foundMapping) { let newSource = foundMapping.source; if (module.source !== newSource) { @@ -181,6 +182,52 @@ function transform(file, api /*, options*/) { } } + /* + * After modifying existing sources to their new paths, we need + * to make sure we clean up duplicate imports + */ + function cleanupDuplicateLiteralPaths() { + const imports = {}; + root.find(j.ImportDeclaration) + .forEach(nodePath => { + let value = nodePath.value && nodePath.value.source.value; + + if (!(value in imports)) { + // add to found imports and we wont modify + imports[value] = nodePath; + } else { + // get all specifiers and add to existing import + // then delete this nodePath + let specifiers = nodePath.value && nodePath.value.specifiers; + let existingNodePath = imports[value]; + + specifiers.forEach((spec) => { + let local = spec.local; + let imported = spec.imported; + + if (imported === 'default') { + let specifier = j.importDefaultSpecifier(j.identifier(local)); + // default imports go at front + existingNodePath.get('specifiers').unshift(specifier); + } else if (imported && local) { + let specifier = j.importSpecifier( + j.identifier(imported.name), + j.identifier(local.name) + ); + existingNodePath.get('specifiers').push(specifier); + } else { + let specifier = j.importSpecifier( + j.identifier(local.name) + ); + existingNodePath.get('specifiers').push(specifier); + } + }); + + nodePath.prune() + } + }); + } + // Find destructured global aliases for fields on the DS global function findGlobalDSAliases(root, globalDS, mappings) { let aliases = {}; @@ -477,6 +524,7 @@ function transform(file, api /*, options*/) { if (imported === 'default') { specifier = j.importDefaultSpecifier(j.identifier(local)); + // default imports go at front declaration.get('specifiers').unshift(specifier); } else { specifier = j.importSpecifier( @@ -494,11 +542,15 @@ function transform(file, api /*, options*/) { delete body[1].comments; mod.node = importStatement; } - } else { - // Update literal paths based on mappings from 'ember-data/model' to '@ember-data/model' - updateLiteralPaths(root, mod, mappings); } + + // Update literal paths based on mappings from 'ember-data/model' to '@ember-data/model' + // by pushing into existing declaration specifiers + updateExistingLiteralPaths(root, mod, mappings); }); + + // // then remove old duplicate specifier if found + cleanupDuplicateLiteralPaths(); } function findExistingModules(root) { From ec848350980a2b4d93c90efa822f0790be015c43 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Thu, 25 Jul 2019 07:52:53 -0700 Subject: [PATCH 12/15] fix lint --- bin/ember-data-codemod.js | 4 +- transforms/globals-to-ember-data-imports.js | 69 ++++++++++----------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/bin/ember-data-codemod.js b/bin/ember-data-codemod.js index 135483c..14ee9a7 100644 --- a/bin/ember-data-codemod.js +++ b/bin/ember-data-codemod.js @@ -143,7 +143,9 @@ function buildReport() { ); } else { console.log( - chalk.green('\nDone! All uses of the Ember global and Ember Data imports have been updated.') + chalk.green( + '\nDone! All uses of the Ember global and Ember Data imports have been updated.' + ) ); } }); diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index ca84745..94f7d40 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -188,44 +188,41 @@ function transform(file, api /*, options*/) { */ function cleanupDuplicateLiteralPaths() { const imports = {}; - root.find(j.ImportDeclaration) - .forEach(nodePath => { - let value = nodePath.value && nodePath.value.source.value; + root.find(j.ImportDeclaration).forEach(nodePath => { + let value = nodePath.value && nodePath.value.source.value; - if (!(value in imports)) { - // add to found imports and we wont modify - imports[value] = nodePath; - } else { - // get all specifiers and add to existing import - // then delete this nodePath - let specifiers = nodePath.value && nodePath.value.specifiers; - let existingNodePath = imports[value]; - - specifiers.forEach((spec) => { - let local = spec.local; - let imported = spec.imported; - - if (imported === 'default') { - let specifier = j.importDefaultSpecifier(j.identifier(local)); - // default imports go at front - existingNodePath.get('specifiers').unshift(specifier); - } else if (imported && local) { - let specifier = j.importSpecifier( - j.identifier(imported.name), - j.identifier(local.name) - ); - existingNodePath.get('specifiers').push(specifier); - } else { - let specifier = j.importSpecifier( - j.identifier(local.name) - ); - existingNodePath.get('specifiers').push(specifier); - } - }); + if (!(value in imports)) { + // add to found imports and we wont modify + imports[value] = nodePath; + } else { + // get all specifiers and add to existing import + // then delete this nodePath + let specifiers = nodePath.value && nodePath.value.specifiers; + let existingNodePath = imports[value]; - nodePath.prune() - } - }); + specifiers.forEach(spec => { + let local = spec.local; + let imported = spec.imported; + + if (imported === 'default') { + let specifier = j.importDefaultSpecifier(j.identifier(local)); + // default imports go at front + existingNodePath.get('specifiers').unshift(specifier); + } else if (imported && local) { + let specifier = j.importSpecifier( + j.identifier(imported.name), + j.identifier(local.name) + ); + existingNodePath.get('specifiers').push(specifier); + } else { + let specifier = j.importSpecifier(j.identifier(local.name)); + existingNodePath.get('specifiers').push(specifier); + } + }); + + nodePath.prune(); + } + }); } // Find destructured global aliases for fields on the DS global From 03133a6f2e98edd38b2db9f141fe7d61acdc6d5d Mon Sep 17 00:00:00 2001 From: snewcomer Date: Thu, 25 Jul 2019 07:58:24 -0700 Subject: [PATCH 13/15] cleanup --- transforms/globals-to-ember-data-imports.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index 94f7d40..8f69c0a 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -187,18 +187,19 @@ function transform(file, api /*, options*/) { * to make sure we clean up duplicate imports */ function cleanupDuplicateLiteralPaths() { - const imports = {}; + const uniqueImports = {}; + root.find(j.ImportDeclaration).forEach(nodePath => { let value = nodePath.value && nodePath.value.source.value; - if (!(value in imports)) { - // add to found imports and we wont modify - imports[value] = nodePath; + if (!(value in uniqueImports)) { + // add to found uniqueImports and we wont modify + uniqueImports[value] = nodePath; } else { // get all specifiers and add to existing import // then delete this nodePath let specifiers = nodePath.value && nodePath.value.specifiers; - let existingNodePath = imports[value]; + let existingNodePath = uniqueImports[value]; specifiers.forEach(spec => { let local = spec.local; @@ -546,7 +547,7 @@ function transform(file, api /*, options*/) { updateExistingLiteralPaths(root, mod, mappings); }); - // // then remove old duplicate specifier if found + // then remove old duplicate specifier if found cleanupDuplicateLiteralPaths(); } From 6192d25f75624d4976ee505a7c46a19156517bf1 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Thu, 25 Jul 2019 08:05:27 -0700 Subject: [PATCH 14/15] moar cleanup --- transforms/globals-to-ember-data-imports.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/transforms/globals-to-ember-data-imports.js b/transforms/globals-to-ember-data-imports.js index 8f69c0a..0952f14 100644 --- a/transforms/globals-to-ember-data-imports.js +++ b/transforms/globals-to-ember-data-imports.js @@ -190,7 +190,8 @@ function transform(file, api /*, options*/) { const uniqueImports = {}; root.find(j.ImportDeclaration).forEach(nodePath => { - let value = nodePath.value && nodePath.value.source.value; + let node = nodePath.node; + let value = node.source && node.source.value; if (!(value in uniqueImports)) { // add to found uniqueImports and we wont modify @@ -198,7 +199,7 @@ function transform(file, api /*, options*/) { } else { // get all specifiers and add to existing import // then delete this nodePath - let specifiers = nodePath.value && nodePath.value.specifiers; + let specifiers = node.specifiers; let existingNodePath = uniqueImports[value]; specifiers.forEach(spec => { From 6313ddb794f41002dbf0af2d8b5667bfc4efd57c Mon Sep 17 00:00:00 2001 From: snewcomer Date: Mon, 29 Jul 2019 06:45:11 -0700 Subject: [PATCH 15/15] update to correct import --- __testfixtures__/literal.input.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/__testfixtures__/literal.input.js b/__testfixtures__/literal.input.js index 04b212f..894abb2 100644 --- a/__testfixtures__/literal.input.js +++ b/__testfixtures__/literal.input.js @@ -1,4 +1,5 @@ -import Model, { attr } from 'ember-data/model'; +import Model from 'ember-data/model'; +import attr from 'ember-data/attr'; import { hasMany, belongsTo } from 'ember-data/relationships'; import JSONAPIAdapter from 'ember-data/adapters/json-api'; import { InvalidError, ServerError, TimeoutError, NotFoundError } from 'ember-data/adapter/error';