Skip to content

Commit bf0cefe

Browse files
author
Robert Jackson
committed
Ensure global member expressions are not shared.
The original fix in b63bb6e attempted to cache the generated member expressions, so that we didn't do duplicated work for each reference to a given global. Unfortunately, this optimization failed to take into consideration that most babel plugins work by directly mutating the node in question. In practice what that meant was that once _any_ usage of a given global was needed to be transformed (e.g. say you had `computed(...args, function(){})` and are transpiling for IE11), then all usages of that global would be mutated. A more concrete example. ```js // input import { computed } from '@ember/object'; function specialComputed(dependentKeys) { return computed(...dependentKeys, function() {}); } function otherLessSpecialComputed() { return computed('stuff', 'hard', 'coded', function() {}); } ``` In this example, the first method (`specialComputed`) needs to be transpiled to something akin to (most of the changes here are from `@babel/plugin-transform-spread`): ```js function specialComputed(dependentKeys) { return Ember.computed.apply(Ember, dependentKeys.concat([function() {}])); } ``` Unfortunately, since the generated `MemberExpression` for `Ember.computed` is shared, this forced the other `computed` usage to be transpiled to: ```js function otherLessSpecialComputed() { return Ember.computed.apply('stuff', 'hard', 'coded', function() {}); } ``` Which is clearly, **totally** invalid. 🤦
1 parent 1431b51 commit bf0cefe

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

src/index.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ module.exports = function (babel) {
2525
node.type.startsWith('TS') &&
2626
!TSTypesRequiringModification.includes(node.type);
2727

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

4543
function getMemberExpressionFor(global) {
46-
let memberExpression = GLOBALS_MAP.get(global);
47-
if (memberExpression === undefined) {
48-
let parts = global.split('.');
44+
let parts = global.split('.');
45+
46+
let object = parts.shift();
47+
let property = parts.shift();
48+
49+
let memberExpression = t.MemberExpression(
50+
t.identifier(object),
51+
t.identifier(property)
52+
);
4953

50-
let object = parts.shift();
54+
while (parts.length > 0) {
5155
let property = parts.shift();
5256

5357
memberExpression = t.MemberExpression(
54-
t.identifier(object),
58+
memberExpression,
5559
t.identifier(property)
5660
);
57-
58-
while (parts.length > 0) {
59-
let property = parts.shift();
60-
61-
memberExpression = t.MemberExpression(
62-
memberExpression,
63-
t.identifier(property)
64-
);
65-
}
66-
67-
GLOBALS_MAP.set(global, memberExpression);
6861
}
6962

7063
return memberExpression;

0 commit comments

Comments
 (0)