Skip to content

Commit

Permalink
Merge pull request #116 from ember-cli/more-failure-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Jun 2, 2020
2 parents 3a5ab11 + bf0cefe commit f69d769
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
73 changes: 73 additions & 0 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,79 @@ describe('when used with @babel/preset-env', () => {

expect(actual).toEqual(`export default Ember.Application.extend({});`);
});

it('does not have issues with ember-google-maps style helper', () => {
let source = `
import { computed, getProperties } from '@ember/object';
import ObjectProxy from '@ember/object/proxy';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
let ObjectPromiseProxy = ObjectProxy.extend(PromiseProxyMixin);
let position = computed('lat', 'lng', function() {
const { lat, lng } = getProperties(this, 'lat', 'lng');
return (lat && lng) ? new google.maps.LatLng(lat, lng) : undefined;
});
function position2() {
return computed('lat', 'lng', function() {
const { lat, lng } = getProperties(this, 'lat', 'lng');
return (lat && lng) ? new google.maps.LatLng(lat, lng) : undefined;
});
}
function computedPromise(...args) {
let func = args.pop();
return computed(...args, function() {
return ObjectPromiseProxy.create({
promise: func.apply(this)
});
});
}
export { computedPromise, position };
`;

let actual = transformWithPresetEnv(source);

expect(actual).toEqual(
`var ObjectPromiseProxy = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin);
var position = Ember.computed('lat', 'lng', function () {
var _Ember$getProperties = Ember.getProperties(this, 'lat', 'lng'),
lat = _Ember$getProperties.lat,
lng = _Ember$getProperties.lng;
return lat && lng ? new google.maps.LatLng(lat, lng) : undefined;
});
function position2() {
return Ember.computed('lat', 'lng', function () {
var _Ember$getProperties2 = Ember.getProperties(this, 'lat', 'lng'),
lat = _Ember$getProperties2.lat,
lng = _Ember$getProperties2.lng;
return lat && lng ? new google.maps.LatLng(lat, lng) : undefined;
});
}
function computedPromise() {
var _Ember;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var func = args.pop();
return (_Ember = Ember).computed.apply(_Ember, args.concat([function () {
return ObjectPromiseProxy.create({
promise: func.apply(this)
});
}]));
}
export { computedPromise, position };`
);
});
});

describe('when used with typescript', () => {
Expand Down
29 changes: 11 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ module.exports = function (babel) {
node.type.startsWith('TS') &&
!TSTypesRequiringModification.includes(node.type);

const GLOBALS_MAP = new Map();

// Flips the ember-rfc176-data mapping into an 'import' indexed object, that exposes the
// default import as well as named imports, e.g. import {foo} from 'bar'
const reverseMapping = {};
Expand All @@ -43,28 +41,23 @@ module.exports = function (babel) {
});

function getMemberExpressionFor(global) {
let memberExpression = GLOBALS_MAP.get(global);
if (memberExpression === undefined) {
let parts = global.split('.');
let parts = global.split('.');

let object = parts.shift();
let property = parts.shift();

let memberExpression = t.MemberExpression(
t.identifier(object),
t.identifier(property)
);

let object = parts.shift();
while (parts.length > 0) {
let property = parts.shift();

memberExpression = t.MemberExpression(
t.identifier(object),
memberExpression,
t.identifier(property)
);

while (parts.length > 0) {
let property = parts.shift();

memberExpression = t.MemberExpression(
memberExpression,
t.identifier(property)
);
}

GLOBALS_MAP.set(global, memberExpression);
}

return memberExpression;
Expand Down

0 comments on commit f69d769

Please sign in to comment.