callsite icon indicating copy to clipboard operation
callsite copied to clipboard

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

Open vjpr opened this issue 10 years ago • 1 comments

When in strict mode (use strict at the top of the file) arguments.callee throws an error.

This came up for me because Babel's require-hook was transpiling this file - yes i know it probably shouldn't.

A nasty bug though because it occurs while the stack trace has been replaced, which makes it hard to find, printing an obscure message to the console.

The workarounds:

  • configure Babel to blacklist useStrict to avoid adding strict to the top of the file.
  • configure Babel to ignore callsite.js.

Regardless I propose the following change to make sure the error gets thrown if for some reason use strict is placed at the top of this file:

module.exports = function(){
  var orig = Error.prepareStackTrace;
  var callee = arguments.callee; // <- access arguments.callee here which will throw a readable error if use strict is set.
  Error.prepareStackTrace = function(_, stack){ return stack; };
  var err = new Error;
  Error.captureStackTrace(err, callee); // <- reference callee here.
  var stack = err.stack;
  Error.prepareStackTrace = orig;
  return stack;
};

vjpr avatar Aug 08 '15 13:08 vjpr

this came up for me in trying to use node's --use_strict flag, long story short--you can't if you have any (direct or indirect) dependency on this project.

maxscott avatar Feb 14 '16 22:02 maxscott