Skip to content

Commit

Permalink
Avoid rewiring left of ForInStatement.
Browse files Browse the repository at this point in the history
Fixes #184
  • Loading branch information
mikesherov authored and Robert Binna committed Apr 10, 2017
1 parent 87a321c commit 8484dd7
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 2 deletions.
151 changes: 151 additions & 0 deletions fixtures/transformation/issue184/expected.js
@@ -0,0 +1,151 @@
var a = '';
var b = {};

for (a in _get__('b')) {
console.log(_get__('a'));
}

module.exports = _get__('a');

var _RewiredData__ = Object.create(null);

var INTENTIONAL_UNDEFINED = '__INTENTIONAL_UNDEFINED__';
let _RewireAPI__ = {};

(function () {
function addPropertyToAPIObject(name, value) {
Object.defineProperty(_RewireAPI__, name, {
value: value,
enumerable: false,
configurable: true
});
}

addPropertyToAPIObject('__get__', _get__);
addPropertyToAPIObject('__GetDependency__', _get__);
addPropertyToAPIObject('__Rewire__', _set__);
addPropertyToAPIObject('__set__', _set__);
addPropertyToAPIObject('__reset__', _reset__);
addPropertyToAPIObject('__ResetDependency__', _reset__);
addPropertyToAPIObject('__with__', _with__);
})();

function _get__(variableName) {
if (_RewiredData__ === undefined || _RewiredData__[variableName] === undefined) {
return _get_original__(variableName);
} else {
var value = _RewiredData__[variableName];

if (value === INTENTIONAL_UNDEFINED) {
return undefined;
} else {
return value;
}
}
}

function _get_original__(variableName) {
switch (variableName) {
case 'b':
return b;

case 'a':
return a;
}

return undefined;
}

function _assign__(variableName, value) {
if (_RewiredData__ === undefined || _RewiredData__[variableName] === undefined) {
return _set_original__(variableName, value);
} else {
return _RewiredData__[variableName] = value;
}
}

function _set_original__(variableName, _value) {
switch (variableName) {}

return undefined;
}

function _update_operation__(operation, variableName, prefix) {
var oldValue = _get__(variableName);

var newValue = operation === '++' ? oldValue + 1 : oldValue - 1;

_assign__(variableName, newValue);

return prefix ? newValue : oldValue;
}

function _set__(variableName, value) {
if (typeof variableName === 'object') {
Object.keys(variableName).forEach(function (name) {
_RewiredData__[name] = variableName[name];
});
} else {
if (value === undefined) {
_RewiredData__[variableName] = INTENTIONAL_UNDEFINED;
} else {
_RewiredData__[variableName] = value;
}

return function () {
_reset__(variableName);
};
}
}

function _reset__(variableName) {
delete _RewiredData__[variableName];
}

function _with__(object) {
var rewiredVariableNames = Object.keys(object);
var previousValues = {};

function reset() {
rewiredVariableNames.forEach(function (variableName) {
_RewiredData__[variableName] = previousValues[variableName];
});
}

return function (callback) {
rewiredVariableNames.forEach(function (variableName) {
previousValues[variableName] = _RewiredData__[variableName];
_RewiredData__[variableName] = object[variableName];
});
let result = callback();

if (!!result && typeof result.then == 'function') {
result.then(reset).catch(reset);
} else {
reset();
}

return result;
};
}

let _typeOfOriginalExport = typeof module.exports;

function addNonEnumerableProperty(name, value) {
Object.defineProperty(module.exports, name, {
value: value,
enumerable: false,
configurable: true
});
}

if ((_typeOfOriginalExport === 'object' || _typeOfOriginalExport === 'function') && Object.isExtensible(module.exports)) {
addNonEnumerableProperty('__get__', _get__);
addNonEnumerableProperty('__GetDependency__', _get__);
addNonEnumerableProperty('__Rewire__', _set__);
addNonEnumerableProperty('__set__', _set__);
addNonEnumerableProperty('__reset__', _reset__);
addNonEnumerableProperty('__ResetDependency__', _reset__);
addNonEnumerableProperty('__with__', _with__);
addNonEnumerableProperty('__RewireAPI__', _RewireAPI__);
}
8 changes: 8 additions & 0 deletions fixtures/transformation/issue184/input.js
@@ -0,0 +1,8 @@
var a = '';
var b = {};

for (a in b) {
console.log(a);
}

module.exports = a;
1 change: 1 addition & 0 deletions src/babel-plugin-rewire.js
Expand Up @@ -21,6 +21,7 @@ module.exports = function({ types: t }) {

return (variableBinding.referencePaths !== null) &&
!(parent.type === 'VariableDeclarator' && parent.id == node) &&
!(parent.type === 'ForInStatement' && parent.left == node) &&
!(parent.type === 'FunctionExpression' && parent.id === node) &&
!(parent.type === 'MemberExpression' && parent.property === node) &&
!(parent.type === 'ObjectProperty' && parent.key === node) &&
Expand Down
5 changes: 3 additions & 2 deletions test/BabelRewirePluginTransformTest.js
Expand Up @@ -61,7 +61,7 @@ describe('BabelRewirePluginTest', function() {
try {
fs.mkdirSync(tempDir);
} catch(error) {}

fs.writeFileSync(tempDir + '/testexpected' + testName + '.js', transformationOutput, 'utf-8');
//fs.writeFileSync(path.resolve(directory, 'expected.js'), transformationOutput, 'utf-8');

Expand Down Expand Up @@ -142,7 +142,8 @@ describe('BabelRewirePluginTest', function() {
'issue133',
'issue136',
'issue152',
'issue155'
'issue155',
'issue184'
];

var stage0FeaturesToTests = [
Expand Down

0 comments on commit 8484dd7

Please sign in to comment.