Skip to content

Commit 127e4fc

Browse files
authored
replace arguments.callee (#23)
* replace arguments.callee * fixup
1 parent 64dfc2c commit 127e4fc

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ As well as execution wrapping, the following code transformations are handled:
8686
* Internal `this` references that are not direct calls, fallback to _global.
8787
* Implicit globals of the form `globalName = ...` are rescoped for a simple strict module conversion.
8888
* Use of `Buffer` and `process` is transformed into an import of `buffer` or `process`. This module name can be customized by the `map` configuration option.
89+
* Replacement of `arguments.callee` with the function name
8990
9091
The remaining strict conversion cases that don't convert are then just the edge cases of loose -> strict mode conversion:
9192
* Any use of `with` statements will throw

test/fixtures/particles/actual.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var p = function () {
2+
return arguments.callee;
3+
}

test/fixtures/particles/expected.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var exports = {},
2+
_dewExec = false;
3+
export function dew() {
4+
if (_dewExec) return exports;
5+
_dewExec = true;
6+
var p = function _temp() {
7+
return _temp;
8+
};
9+
return exports;
10+
}

transform-cjs-dew.js

+12
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,18 @@ module.exports = function ({ types: t }) {
10201020
path.replaceWith(t.identifier('undefined'));
10211021
}
10221022
}
1023+
else if (state.functionDepth > 0 && t.isIdentifier(path.node.object, { name: 'arguments' }) && t.isIdentifier(path.node.property, { name: 'callee' })) {
1024+
let funcPath = path;
1025+
let func = funcPath.scope.block;
1026+
while (!t.isFunction(func)) {
1027+
funcPath = funcPath.parentPath;
1028+
func = funcPath.scope.block;
1029+
}
1030+
if (!func.id) {
1031+
func.id = funcPath.scope.generateUidIdentifier();
1032+
}
1033+
path.replaceWith(func.id);
1034+
}
10231035
},
10241036

10251037
Scope: {

0 commit comments

Comments
 (0)