Skip to content

Commit

Permalink
refactor to do in registry modules loop instead of own funciton
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Jul 24, 2019
1 parent 6341cf1 commit 0329327
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions transforms/globals-to-ember-data-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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)
}
});
}
Expand Down

0 comments on commit 0329327

Please sign in to comment.