Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid rewiring left of ForInStatement.
Fixes #184
- Loading branch information
1 parent
87a321c
commit 8484dd7
Showing
4 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var a = ''; | ||
var b = {}; | ||
|
||
for (a in b) { | ||
console.log(a); | ||
} | ||
|
||
module.exports = a; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters