From 4a644bef4d78e7a087f9a35b1e2cfbb5439415bf Mon Sep 17 00:00:00 2001 From: Craig Bilner Date: Wed, 1 Feb 2017 17:59:56 +0000 Subject: [PATCH] Add tests Add alias check --- test/expected-output/existing-alias.js | 9 +++++++++ test/input/existing-alias.js | 7 +++++++ transform.js | 28 +++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/expected-output/existing-alias.js create mode 100644 test/input/existing-alias.js diff --git a/test/expected-output/existing-alias.js b/test/expected-output/existing-alias.js new file mode 100644 index 0000000..5f4258e --- /dev/null +++ b/test/expected-output/existing-alias.js @@ -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({ + +}); diff --git a/test/input/existing-alias.js b/test/input/existing-alias.js new file mode 100644 index 0000000..401533c --- /dev/null +++ b/test/input/existing-alias.js @@ -0,0 +1,7 @@ +import Ember from 'ember'; + +const Helper = Ember.Helper || Ember.Object; + +export default Helper.extend({ + +}); diff --git a/transform.js b/transform.js index c293fce..67f2c2c 100644 --- a/transform.js +++ b/transform.js @@ -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 @@ -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. @@ -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;