{"action":"created","issue":{"url":"https://api.github.com/repos/macbre/phantomas/issues/467","labels_url":"https://api.github.com/repos/macbre/phantomas/issues/467/labels{/name}","comments_url":"https://api.github.com/repos/macbre/phantomas/issues/467/comments","events_url":"https://api.github.com/repos/macbre/phantomas/issues/467/events","html_url":"https://github.com/macbre/phantomas/issues/467","id":53117159,"number":467,"title":"Scope problem with the spied version of eval()","user":{"login":"gmetais","id":498031,"avatar_url":"https://avatars.githubusercontent.com/u/498031?v=3","gravatar_id":"","url":"https://api.github.com/users/gmetais","html_url":"https://github.com/gmetais","followers_url":"https://api.github.com/users/gmetais/followers","following_url":"https://api.github.com/users/gmetais/following{/other_user}","gists_url":"https://api.github.com/users/gmetais/gists{/gist_id}","starred_url":"https://api.github.com/users/gmetais/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmetais/subscriptions","organizations_url":"https://api.github.com/users/gmetais/orgs","repos_url":"https://api.github.com/users/gmetais/repos","events_url":"https://api.github.com/users/gmetais/events{/privacy}","received_events_url":"https://api.github.com/users/gmetais/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2014-12-30T13:59:15Z","updated_at":"2015-01-05T09:35:01Z","closed_at":null,"body":"Hi there! Sorry for disturbing during the holidays :)\r\n\r\nI have a bug with Phantomas counting a jsError on this page:\r\n```\r\nphantomas --url=http://www.francetvinfo.fr --timeout=30\r\n```\r\nThe error is `ReferenceError: Can't find variable: oEl - unknown fn:  @ 1`.\r\n\r\nHere is the portion of code responsible for the problem:\r\n```js\r\nfunction xtP(oEl, inTYPE) {\r\n    if (typeof (oEl.offsetParent) != un) {\r\n        var sType = 'oEl.offset' + inTYPE;\r\n        for (var iVal = 0; oEl; oEl = oEl.offsetParent) {\r\n            iVal += eval(sType);\r\n        }\r\n        return iVal;\r\n    }\r\n    return -1;\r\n}\r\n```\r\noEl is correctly defined inside the function, but not inside eval.\r\n\r\n\r\nWhen the javaScriptBottleneck module is disabled, the error disappears. The reason was hard to determine, but i finally found. Eval is made to run in the scope of the calling function, but phantomas' spy function is changing the scope.\r\n\r\nSimplified example:\r\n```js\r\n// Code example that works because it's using the original eval function\r\nfunction workingScript() {\r\n    var foo = 999;\r\n    return eval('foo');\r\n};\r\n\r\nconsole.log(workingScript());\r\n// 999\r\n\r\n\r\n// Simulating the spy function for eval\r\nfunction spyedEval(code) {\r\n    var origEval = eval;\r\n    return origEval.apply(this, [code]);\r\n};\r\n\r\n// Code example using the spyed function\r\nfunction buggingScript() {\r\n    var foo = 999;\r\n    return spyedEval('foo');\r\n};\r\n\r\nconsole.log(buggingScript());\r\n// ReferenceError: foo is not defined\r\n```\r\n\r\n\r\nHere is what MDN says about eval:\r\n> You cannot indirectly use the eval function by invoking it via a name other than eval(); if you do, a runtime error might occur. For example, you should not use the following code:\r\n\r\n> ```var x = 2;\r\nvar y = 4;\r\nvar myEval = eval;\r\nmyEval(\"x + y\");```\r\n\r\n([source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval))\r\n\r\nSo i don't have any solution for the moment, and it looks like it's going to be difficult to fix this... Any idea?"},"comment":{"url":"https://api.github.com/repos/macbre/phantomas/issues/comments/68685348","html_url":"https://github.com/macbre/phantomas/issues/467#issuecomment-68685348","issue_url":"https://api.github.com/repos/macbre/phantomas/issues/467","id":68685348,"user":{"login":"gmetais","id":498031,"avatar_url":"https://avatars.githubusercontent.com/u/498031?v=3","gravatar_id":"","url":"https://api.github.com/users/gmetais","html_url":"https://github.com/gmetais","followers_url":"https://api.github.com/users/gmetais/followers","following_url":"https://api.github.com/users/gmetais/following{/other_user}","gists_url":"https://api.github.com/users/gmetais/gists{/gist_id}","starred_url":"https://api.github.com/users/gmetais/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmetais/subscriptions","organizations_url":"https://api.github.com/users/gmetais/orgs","repos_url":"https://api.github.com/users/gmetais/repos","events_url":"https://api.github.com/users/gmetais/events{/privacy}","received_events_url":"https://api.github.com/users/gmetais/received_events","type":"User","site_admin":false},"created_at":"2015-01-05T09:35:01Z","updated_at":"2015-01-05T09:35:01Z","body":"That would be perfect!"}}