Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update literal paths #21

Merged
merged 15 commits into from
Jul 29, 2019
5 changes: 5 additions & 0 deletions __testfixtures__/literal.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Model, { attr } from 'ember-data/model';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, it seems we have a wrong import. It should be:

 import Model from 'ember-data/model';
 import attr from 'ember-data/attr';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't make the test fail, because attr is identified in the modules lookup.

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';
4 changes: 4 additions & 0 deletions __testfixtures__/literal.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this output to show the failing test. My bad, this isn't ready to go. I ran it on our project and had to manually collapse so we didn't have duplicate imports. I'll try to fix.

Copy link
Member

@dcyriller dcyriller Jul 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, let me know when you need another review.

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';
6 changes: 3 additions & 3 deletions bin/ember-data-codemod.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let cwd = process.cwd();
let pkgPath = path.join(cwd, 'package.json');

const DEFAULT_PATHS = [
'app',
'app'
'addon',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we have a typo here

'addon-test-support',
'tests',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.')
);
}
});
Expand Down
32 changes: 29 additions & 3 deletions transforms/globals-to-ember-data-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, modules);
updateOrCreateImportDeclarations(root, modules, mappings);

// Actually go through and replace each usage of `DS.whatever` with the
// imported binding (`whatever`).
Expand Down Expand Up @@ -157,6 +157,30 @@ function transform(file, api /*, options*/) {
return dsUsages.filter(isDSGlobal(globalDS)).paths();
}

/*
* loops through all modules and replaces literal path if necessary
* 'ember-data/model' -> '@ember-data/model'
*/
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
function findGlobalDSAliases(root, globalDS, mappings) {
let aliases = {};
Expand Down Expand Up @@ -436,7 +460,7 @@ function transform(file, api /*, options*/) {
return parent.node.id.name === local;
}

function updateOrCreateImportDeclarations(root, registry) {
function updateOrCreateImportDeclarations(root, registry, mappings) {
let body = root.get().value.program.body;

registry.modules.forEach(mod => {
Expand All @@ -448,7 +472,6 @@ function transform(file, api /*, options*/) {
let declaration = root.find(j.ImportDeclaration, {
source: { value: mod.source }
});

if (declaration.size() > 0) {
let specifier;

Expand All @@ -471,6 +494,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