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

Don't create import of existing declaration #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test/expected-output/existing-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import EmberObject from "@ember/object";
import EmberHelper from "@ember/component/helper";
import Ember from 'ember';

const Helper = EmberHelper || EmberObject;

export default Helper.extend({

});
7 changes: 7 additions & 0 deletions test/input/existing-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Ember from 'ember';

const Helper = Ember.Helper || Ember.Object;

export default Helper.extend({

});
28 changes: 25 additions & 3 deletions transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function transform(file, api, options) {
// used as the root of a property lookup. If they match one of the provided
// mappings, save it off for replacement later.
let replacements = findUsageOfEmberGlobal(root)
.map(findReplacement(mappings));
.map(findReplacement(root, mappings));

// 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
Expand Down Expand Up @@ -103,12 +103,34 @@ function transform(file, api, options) {
.paths();
}

function matchingVariableNotAlias(root, name) {
let matchingDeclarations = root.find(j.VariableDeclarator, {
id: {
type: 'Identifier',
name,
},
});

let matchingAliases = matchingDeclarations.filter((element) => {
let isExpression =
element.value.init.type === 'MemberExpression';
let isEmberGlobal =
isExpression && element.value.init.object.name === 'Ember';
let isAlias =
isEmberGlobal && element.value.init.property.name === name;

return isAlias;
});

return matchingDeclarations.size() !== matchingAliases.size();
}

/**
* Returns a function that can be used to map an array of MemberExpression
* nodes into Replacement instances. Does the actual work of verifying if the
* `Ember` identifier used in the MemberExpression is actually replaceable.
*/
function findReplacement(mappings) {
function findReplacement(root, mappings) {
return function(path) {
// Expand the full set of property lookups. For example, we don't want
// just "Ember.computed"—we want "Ember.computed.or" as well.
Expand Down Expand Up @@ -142,7 +164,7 @@ function transform(file, api, options) {
if (!mod.local) {
// Ember.computed.or => or
let local = propertyPath.split(".").slice(-1)[0];
if (includes(RESERVED, local)) {
if (matchingVariableNotAlias(root, local) || includes(RESERVED, local)) {
local = `Ember${local}`;
}
mod.local = local;
Expand Down