Skip to content

Commit

Permalink
replace arguments.callee (#23)
Browse files Browse the repository at this point in the history
* replace arguments.callee

* fixup
  • Loading branch information
guybedford authored Feb 9, 2025
1 parent 64dfc2c commit 127e4fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ As well as execution wrapping, the following code transformations are handled:
* Internal `this` references that are not direct calls, fallback to _global.
* Implicit globals of the form `globalName = ...` are rescoped for a simple strict module conversion.
* 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.
* Replacement of `arguments.callee` with the function name
The remaining strict conversion cases that don't convert are then just the edge cases of loose -> strict mode conversion:
* Any use of `with` statements will throw
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/particles/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var p = function () {
return arguments.callee;
}
10 changes: 10 additions & 0 deletions test/fixtures/particles/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var exports = {},
_dewExec = false;
export function dew() {
if (_dewExec) return exports;
_dewExec = true;
var p = function _temp() {
return _temp;
};
return exports;
}
12 changes: 12 additions & 0 deletions transform-cjs-dew.js
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,18 @@ module.exports = function ({ types: t }) {
path.replaceWith(t.identifier('undefined'));
}
}
else if (state.functionDepth > 0 && t.isIdentifier(path.node.object, { name: 'arguments' }) && t.isIdentifier(path.node.property, { name: 'callee' })) {
let funcPath = path;
let func = funcPath.scope.block;
while (!t.isFunction(func)) {
funcPath = funcPath.parentPath;
func = funcPath.scope.block;
}
if (!func.id) {
func.id = funcPath.scope.generateUidIdentifier();
}
path.replaceWith(func.id);
}
},

Scope: {
Expand Down

0 comments on commit 127e4fc

Please sign in to comment.