{"action":"created","issue":{"url":"https://api.github.com/repos/Rich-Harris/butternut/issues/158","repository_url":"https://api.github.com/repos/Rich-Harris/butternut","labels_url":"https://api.github.com/repos/Rich-Harris/butternut/issues/158/labels{/name}","comments_url":"https://api.github.com/repos/Rich-Harris/butternut/issues/158/comments","events_url":"https://api.github.com/repos/Rich-Harris/butternut/issues/158/events","html_url":"https://github.com/Rich-Harris/butternut/issues/158","id":229935974,"number":158,"title":"Bug: Unused variables containing anonymous functions","user":{"login":"Loilo","id":1922624,"avatar_url":"https://avatars0.githubusercontent.com/u/1922624?v=3","gravatar_id":"","url":"https://api.github.com/users/Loilo","html_url":"https://github.com/Loilo","followers_url":"https://api.github.com/users/Loilo/followers","following_url":"https://api.github.com/users/Loilo/following{/other_user}","gists_url":"https://api.github.com/users/Loilo/gists{/gist_id}","starred_url":"https://api.github.com/users/Loilo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Loilo/subscriptions","organizations_url":"https://api.github.com/users/Loilo/orgs","repos_url":"https://api.github.com/users/Loilo/repos","events_url":"https://api.github.com/users/Loilo/events{/privacy}","received_events_url":"https://api.github.com/users/Loilo/received_events","type":"User","site_admin":false},"labels":[{"id":600189532,"url":"https://api.github.com/repos/Rich-Harris/butternut/labels/bug","name":"bug","color":"ee0701","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-05-19T10:33:07Z","updated_at":"2017-07-06T11:48:35Z","closed_at":null,"body":"This one is a little bit tricky because I can't really tell what's the root cause for this, it's probably related to #134.\r\n\r\n---\r\n\r\nThe situation: Butternut removes an unused variable (`any_value_2` in the examples below) but sometimes keeps its value, an anonymous function.\r\n\r\nThat's obviously not the right thing to do since a standalone unnamed function isn't valid JavaScript. However, I don't know exactly *when* the function is actually kept.\r\n\r\n### Example 1\r\nThe function seems to be kept when there's a variable declaration with an assigned function call each *before* and also *after* the anonymous function.\r\n\r\n**Input:**\r\n```javascript\r\nfunction wrapper () {\r\n  var any_value_1 = any_fn_1()\r\n  var any_value_2 = function () {\r\n    // Really anything\r\n  }\r\n  var any_value_3 = any_fn_2()\r\n}\r\n```\r\n\r\n**Output Butternut 0.4.6:**\r\n```javascript\r\nfunction wrapper(){any_fn_1();function(){};any_fn_2()}\r\n```\r\n\r\n**Output UglifyJS 3.0.8:**\r\n```javascript\r\nfunction wrapper(){any_fn_1(),any_fn_2()}\r\n```\r\n\r\n### Example 2\r\nThe function also will be kept if its body keeps a reference to a local variable that has been defined before the function (but still inside the `wrapper` function) and there's a variable declaration with an assigned function call after.\r\n\r\n**Input:**\r\n```javascript\r\nfunction wrapper () {\r\n  var any_value_1\r\n  var any_value_2 = function () {\r\n    any_value_1\r\n  }\r\n  var any_value_3 = side_effected()\r\n}\r\n```\r\n\r\n**Output Butternut 0.4.6:**\r\n```javascript\r\nfunction wrapper(){var n;function(){n};side_effected()}\r\n```\r\n\r\n**Output UglifyJS 3.0.8:**\r\n```javascript\r\nfunction wrapper(){side_effected()}\r\n```"},"comment":{"url":"https://api.github.com/repos/Rich-Harris/butternut/issues/comments/313373805","html_url":"https://github.com/Rich-Harris/butternut/issues/158#issuecomment-313373805","issue_url":"https://api.github.com/repos/Rich-Harris/butternut/issues/158","id":313373805,"user":{"login":"hrdinka","id":1436960,"avatar_url":"https://avatars2.githubusercontent.com/u/1436960?v=3","gravatar_id":"","url":"https://api.github.com/users/hrdinka","html_url":"https://github.com/hrdinka","followers_url":"https://api.github.com/users/hrdinka/followers","following_url":"https://api.github.com/users/hrdinka/following{/other_user}","gists_url":"https://api.github.com/users/hrdinka/gists{/gist_id}","starred_url":"https://api.github.com/users/hrdinka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hrdinka/subscriptions","organizations_url":"https://api.github.com/users/hrdinka/orgs","repos_url":"https://api.github.com/users/hrdinka/repos","events_url":"https://api.github.com/users/hrdinka/events{/privacy}","received_events_url":"https://api.github.com/users/hrdinka/received_events","type":"User","site_admin":false},"created_at":"2017-07-06T11:48:35Z","updated_at":"2017-07-06T11:48:35Z","body":"In this example the bug is triggered by calling the anonymous function:\r\n\r\n#### Input\r\n\r\n```javascript\r\n(function() {\r\n\r\nvar v = function() {\r\n    return 1;\r\n}();\r\n\r\n})\r\n```\r\n#### Output Butternut 0.4.6:\r\n\r\n```javascript\r\n(function(){function(){return 1}()})\r\n```"}}
{"actor":{"display_login":"hrdinka"}}