From 127e4fc1e1599a0f7c54d2de21b6e4e881c7c6f6 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 9 Feb 2025 15:53:39 -0800 Subject: [PATCH] replace arguments.callee (#23) * replace arguments.callee * fixup --- README.md | 1 + test/fixtures/particles/actual.js | 3 +++ test/fixtures/particles/expected.js | 10 ++++++++++ transform-cjs-dew.js | 12 ++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 test/fixtures/particles/actual.js create mode 100644 test/fixtures/particles/expected.js diff --git a/README.md b/README.md index 2a2313e..3317deb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/test/fixtures/particles/actual.js b/test/fixtures/particles/actual.js new file mode 100644 index 0000000..57efd78 --- /dev/null +++ b/test/fixtures/particles/actual.js @@ -0,0 +1,3 @@ +var p = function () { + return arguments.callee; +} diff --git a/test/fixtures/particles/expected.js b/test/fixtures/particles/expected.js new file mode 100644 index 0000000..71f07bb --- /dev/null +++ b/test/fixtures/particles/expected.js @@ -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; +} \ No newline at end of file diff --git a/transform-cjs-dew.js b/transform-cjs-dew.js index d50151a..49ef7bb 100644 --- a/transform-cjs-dew.js +++ b/transform-cjs-dew.js @@ -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: {