{"action":"created","comment":{"url":"https://api.github.com/repos/cpojer/js-codemod/pulls/comments/40483103","id":40483103,"diff_hunk":"@@ -0,0 +1,79 @@\n+/** \n+ * Transform all your `var`s to `let` and `const`.\n+ *\n+ * The only catch is it may not catch nested bodies that mutate anything beyond\n+ * a single parent.\n+ */\n+\n+\n+module.exports = function(file, api) {\n+  const j = api.jscodeshift;\n+\n+  // a quick and likely incomplete check if a variable has been mutated within\n+  // a variables direct parent scope\n+  var isMutated = (node) => {\n+    var mutated = false;\n+    j(node.parent)\n+      .find(j.AssignmentExpression)\n+      .forEach(n => {\n+        if (node.value.declarations.some(d => d.id.name === n.value.left.name)) {\n+          mutated = true;\n+        }\n+      });\n+\n+    j(node.parent)\n+      .find(j.UpdateExpression)\n+      .forEach(n => {\n+        if (node.value.declarations.some(d => d.id.name === n.value.argument.name)) {\n+          mutated = true;\n+        }\n+      });\n+    return mutated;\n+  }\n+\n+  var ast = j(file.source)\n+\n+  // convert all necessary variable declarations to let or const\n+  ast\n+    .find(j.VariableDeclaration)\n+    .forEach(\n+      p => {\n+        if (p.value.kind !== 'var') return;\n+        if (\n+          'ForStatement' === p.parent.value.type ||\n+          'ForInStatement' === p.parent.value.type ||\n+          'ForOfStatement' === p.parent.value.type ||\n+          'WhileStatement' === p.parent.value.type ||\n+          isMutated(p)\n+        ) {\n+          p.value.kind = 'let';\n+        } else {\n+          p.value.kind = 'const';\n+        }\n+      }\n+    );\n+\n+  // if a iterator statement attempts to reuse a loose iterator variable \n+  // change it to a let declaration\n+  ast\n+    .find(j.Statement)\n+    .filter(exp => (\n+      exp.value.type === 'ForStatement' ||\n+      exp.value.type === 'ForInStatement' ||\n+      exp.value.type === 'ForOfStatement'\n+    ))\n+    .forEach(\n+      p => {\n+        if (p.value.init.type === 'AssignmentExpression') {\n+          p.value.init = j.variableDeclaration('let', [\n+            j.variableDeclarator(\n+              p.value.init.left,\n+              p.value.init.right\n+            )\n+          ]);","path":"transforms/no-vars.js","position":73,"original_position":73,"commit_id":"6f18118a8d801b4ac765e93515dcdb5b8cbc86ad","original_commit_id":"6f18118a8d801b4ac765e93515dcdb5b8cbc86ad","user":{"login":"iamdustan","id":227879,"avatar_url":"https://avatars.githubusercontent.com/u/227879?v=3","gravatar_id":"","url":"https://api.github.com/users/iamdustan","html_url":"https://github.com/iamdustan","followers_url":"https://api.github.com/users/iamdustan/followers","following_url":"https://api.github.com/users/iamdustan/following{/other_user}","gists_url":"https://api.github.com/users/iamdustan/gists{/gist_id}","starred_url":"https://api.github.com/users/iamdustan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iamdustan/subscriptions","organizations_url":"https://api.github.com/users/iamdustan/orgs","repos_url":"https://api.github.com/users/iamdustan/repos","events_url":"https://api.github.com/users/iamdustan/events{/privacy}","received_events_url":"https://api.github.com/users/iamdustan/received_events","type":"User","site_admin":false},"body":"Oh excellent. That is much clearer. I’ll update next time I’m on my computer. Thanks for the tip!","created_at":"2015-09-25T22:52:37Z","updated_at":"2015-09-25T22:52:37Z","html_url":"https://github.com/cpojer/js-codemod/pull/5#discussion_r40483103","pull_request_url":"https://api.github.com/repos/cpojer/js-codemod/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/cpojer/js-codemod/pulls/comments/40483103"},"html":{"href":"https://github.com/cpojer/js-codemod/pull/5#discussion_r40483103"},"pull_request":{"href":"https://api.github.com/repos/cpojer/js-codemod/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/cpojer/js-codemod/pulls/5","id":46044985,"html_url":"https://github.com/cpojer/js-codemod/pull/5","diff_url":"https://github.com/cpojer/js-codemod/pull/5.diff","patch_url":"https://github.com/cpojer/js-codemod/pull/5.patch","issue_url":"https://api.github.com/repos/cpojer/js-codemod/issues/5","number":5,"state":"open","locked":false,"title":"deprecate your vars","user":{"login":"iamdustan","id":227879,"avatar_url":"https://avatars.githubusercontent.com/u/227879?v=3","gravatar_id":"","url":"https://api.github.com/users/iamdustan","html_url":"https://github.com/iamdustan","followers_url":"https://api.github.com/users/iamdustan/followers","following_url":"https://api.github.com/users/iamdustan/following{/other_user}","gists_url":"https://api.github.com/users/iamdustan/gists{/gist_id}","starred_url":"https://api.github.com/users/iamdustan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iamdustan/subscriptions","organizations_url":"https://api.github.com/users/iamdustan/orgs","repos_url":"https://api.github.com/users/iamdustan/repos","events_url":"https://api.github.com/users/iamdustan/events{/privacy}","received_events_url":"https://api.github.com/users/iamdustan/received_events","type":"User","site_admin":false},"body":"Closes #4.","created_at":"2015-09-25T18:50:14Z","updated_at":"2015-09-25T22:52:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"59b2a357485a56a0eb9bb9cf1a96c084cb6b825f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cpojer/js-codemod/pulls/5/commits","review_comments_url":"https://api.github.com/repos/cpojer/js-codemod/pulls/5/comments","review_comment_url":"https://api.github.com/repos/cpojer/js-codemod/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cpojer/js-codemod/issues/5/comments","statuses_url":"https://api.github.com/repos/cpojer/js-codemod/statuses/6f18118a8d801b4ac765e93515dcdb5b8cbc86ad","head":{"label":"iamdustan:deprecate-var","ref":"deprecate-var","sha":"6f18118a8d801b4ac765e93515dcdb5b8cbc86ad","user":{"login":"iamdustan","id":227879,"avatar_url":"https://avatars.githubusercontent.com/u/227879?v=3","gravatar_id":"","url":"https://api.github.com/users/iamdustan","html_url":"https://github.com/iamdustan","followers_url":"https://api.github.com/users/iamdustan/followers","following_url":"https://api.github.com/users/iamdustan/following{/other_user}","gists_url":"https://api.github.com/users/iamdustan/gists{/gist_id}","starred_url":"https://api.github.com/users/iamdustan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iamdustan/subscriptions","organizations_url":"https://api.github.com/users/iamdustan/orgs","repos_url":"https://api.github.com/users/iamdustan/repos","events_url":"https://api.github.com/users/iamdustan/events{/privacy}","received_events_url":"https://api.github.com/users/iamdustan/received_events","type":"User","site_admin":false},"repo":{"id":43150056,"name":"js-codemod","full_name":"iamdustan/js-codemod","owner":{"login":"iamdustan","id":227879,"avatar_url":"https://avatars.githubusercontent.com/u/227879?v=3","gravatar_id":"","url":"https://api.github.com/users/iamdustan","html_url":"https://github.com/iamdustan","followers_url":"https://api.github.com/users/iamdustan/followers","following_url":"https://api.github.com/users/iamdustan/following{/other_user}","gists_url":"https://api.github.com/users/iamdustan/gists{/gist_id}","starred_url":"https://api.github.com/users/iamdustan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iamdustan/subscriptions","organizations_url":"https://api.github.com/users/iamdustan/orgs","repos_url":"https://api.github.com/users/iamdustan/repos","events_url":"https://api.github.com/users/iamdustan/events{/privacy}","received_events_url":"https://api.github.com/users/iamdustan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/iamdustan/js-codemod","description":"Codemod scripts to transform code to next generation JS","fork":true,"url":"https://api.github.com/repos/iamdustan/js-codemod","forks_url":"https://api.github.com/repos/iamdustan/js-codemod/forks","keys_url":"https://api.github.com/repos/iamdustan/js-codemod/keys{/key_id}","collaborators_url":"https://api.github.com/repos/iamdustan/js-codemod/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/iamdustan/js-codemod/teams","hooks_url":"https://api.github.com/repos/iamdustan/js-codemod/hooks","issue_events_url":"https://api.github.com/repos/iamdustan/js-codemod/issues/events{/number}","events_url":"https://api.github.com/repos/iamdustan/js-codemod/events","assignees_url":"https://api.github.com/repos/iamdustan/js-codemod/assignees{/user}","branches_url":"https://api.github.com/repos/iamdustan/js-codemod/branches{/branch}","tags_url":"https://api.github.com/repos/iamdustan/js-codemod/tags","blobs_url":"https://api.github.com/repos/iamdustan/js-codemod/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/iamdustan/js-codemod/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/iamdustan/js-codemod/git/refs{/sha}","trees_url":"https://api.github.com/repos/iamdustan/js-codemod/git/trees{/sha}","statuses_url":"https://api.github.com/repos/iamdustan/js-codemod/statuses/{sha}","languages_url":"https://api.github.com/repos/iamdustan/js-codemod/languages","stargazers_url":"https://api.github.com/repos/iamdustan/js-codemod/stargazers","contributors_url":"https://api.github.com/repos/iamdustan/js-codemod/contributors","subscribers_url":"https://api.github.com/repos/iamdustan/js-codemod/subscribers","subscription_url":"https://api.github.com/repos/iamdustan/js-codemod/subscription","commits_url":"https://api.github.com/repos/iamdustan/js-codemod/commits{/sha}","git_commits_url":"https://api.github.com/repos/iamdustan/js-codemod/git/commits{/sha}","comments_url":"https://api.github.com/repos/iamdustan/js-codemod/comments{/number}","issue_comment_url":"https://api.github.com/repos/iamdustan/js-codemod/issues/comments{/number}","contents_url":"https://api.github.com/repos/iamdustan/js-codemod/contents/{+path}","compare_url":"https://api.github.com/repos/iamdustan/js-codemod/compare/{base}...{head}","merges_url":"https://api.github.com/repos/iamdustan/js-codemod/merges","archive_url":"https://api.github.com/repos/iamdustan/js-codemod/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/iamdustan/js-codemod/downloads","issues_url":"https://api.github.com/repos/iamdustan/js-codemod/issues{/number}","pulls_url":"https://api.github.com/repos/iamdustan/js-codemod/pulls{/number}","milestones_url":"https://api.github.com/repos/iamdustan/js-codemod/milestones{/number}","notifications_url":"https://api.github.com/repos/iamdustan/js-codemod/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/iamdustan/js-codemod/labels{/name}","releases_url":"https://api.github.com/repos/iamdustan/js-codemod/releases{/id}","created_at":"2015-09-25T14:36:36Z","updated_at":"2015-09-25T14:36:37Z","pushed_at":"2015-09-25T18:49:08Z","git_url":"git://github.com/iamdustan/js-codemod.git","ssh_url":"git@github.com:iamdustan/js-codemod.git","clone_url":"https://github.com/iamdustan/js-codemod.git","svn_url":"https://github.com/iamdustan/js-codemod","homepage":null,"size":119,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"cpojer:master","ref":"master","sha":"38620e40dcb4d2467ec95a54d38c91dd364754ba","user":{"login":"cpojer","id":13352,"avatar_url":"https://avatars.githubusercontent.com/u/13352?v=3","gravatar_id":"","url":"https://api.github.com/users/cpojer","html_url":"https://github.com/cpojer","followers_url":"https://api.github.com/users/cpojer/followers","following_url":"https://api.github.com/users/cpojer/following{/other_user}","gists_url":"https://api.github.com/users/cpojer/gists{/gist_id}","starred_url":"https://api.github.com/users/cpojer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpojer/subscriptions","organizations_url":"https://api.github.com/users/cpojer/orgs","repos_url":"https://api.github.com/users/cpojer/repos","events_url":"https://api.github.com/users/cpojer/events{/privacy}","received_events_url":"https://api.github.com/users/cpojer/received_events","type":"User","site_admin":false},"repo":{"id":32711479,"name":"js-codemod","full_name":"cpojer/js-codemod","owner":{"login":"cpojer","id":13352,"avatar_url":"https://avatars.githubusercontent.com/u/13352?v=3","gravatar_id":"","url":"https://api.github.com/users/cpojer","html_url":"https://github.com/cpojer","followers_url":"https://api.github.com/users/cpojer/followers","following_url":"https://api.github.com/users/cpojer/following{/other_user}","gists_url":"https://api.github.com/users/cpojer/gists{/gist_id}","starred_url":"https://api.github.com/users/cpojer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpojer/subscriptions","organizations_url":"https://api.github.com/users/cpojer/orgs","repos_url":"https://api.github.com/users/cpojer/repos","events_url":"https://api.github.com/users/cpojer/events{/privacy}","received_events_url":"https://api.github.com/users/cpojer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cpojer/js-codemod","description":"Codemod scripts to transform code to next generation JS","fork":false,"url":"https://api.github.com/repos/cpojer/js-codemod","forks_url":"https://api.github.com/repos/cpojer/js-codemod/forks","keys_url":"https://api.github.com/repos/cpojer/js-codemod/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cpojer/js-codemod/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cpojer/js-codemod/teams","hooks_url":"https://api.github.com/repos/cpojer/js-codemod/hooks","issue_events_url":"https://api.github.com/repos/cpojer/js-codemod/issues/events{/number}","events_url":"https://api.github.com/repos/cpojer/js-codemod/events","assignees_url":"https://api.github.com/repos/cpojer/js-codemod/assignees{/user}","branches_url":"https://api.github.com/repos/cpojer/js-codemod/branches{/branch}","tags_url":"https://api.github.com/repos/cpojer/js-codemod/tags","blobs_url":"https://api.github.com/repos/cpojer/js-codemod/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cpojer/js-codemod/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cpojer/js-codemod/git/refs{/sha}","trees_url":"https://api.github.com/repos/cpojer/js-codemod/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cpojer/js-codemod/statuses/{sha}","languages_url":"https://api.github.com/repos/cpojer/js-codemod/languages","stargazers_url":"https://api.github.com/repos/cpojer/js-codemod/stargazers","contributors_url":"https://api.github.com/repos/cpojer/js-codemod/contributors","subscribers_url":"https://api.github.com/repos/cpojer/js-codemod/subscribers","subscription_url":"https://api.github.com/repos/cpojer/js-codemod/subscription","commits_url":"https://api.github.com/repos/cpojer/js-codemod/commits{/sha}","git_commits_url":"https://api.github.com/repos/cpojer/js-codemod/git/commits{/sha}","comments_url":"https://api.github.com/repos/cpojer/js-codemod/comments{/number}","issue_comment_url":"https://api.github.com/repos/cpojer/js-codemod/issues/comments{/number}","contents_url":"https://api.github.com/repos/cpojer/js-codemod/contents/{+path}","compare_url":"https://api.github.com/repos/cpojer/js-codemod/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cpojer/js-codemod/merges","archive_url":"https://api.github.com/repos/cpojer/js-codemod/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cpojer/js-codemod/downloads","issues_url":"https://api.github.com/repos/cpojer/js-codemod/issues{/number}","pulls_url":"https://api.github.com/repos/cpojer/js-codemod/pulls{/number}","milestones_url":"https://api.github.com/repos/cpojer/js-codemod/milestones{/number}","notifications_url":"https://api.github.com/repos/cpojer/js-codemod/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cpojer/js-codemod/labels{/name}","releases_url":"https://api.github.com/repos/cpojer/js-codemod/releases{/id}","created_at":"2015-03-23T04:45:13Z","updated_at":"2015-09-25T15:49:30Z","pushed_at":"2015-09-25T18:50:14Z","git_url":"git://github.com/cpojer/js-codemod.git","ssh_url":"git@github.com:cpojer/js-codemod.git","clone_url":"https://github.com/cpojer/js-codemod.git","svn_url":"https://github.com/cpojer/js-codemod","homepage":null,"size":311,"stargazers_count":18,"watchers_count":18,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":3,"forks":4,"open_issues":3,"watchers":18,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cpojer/js-codemod/pulls/5"},"html":{"href":"https://github.com/cpojer/js-codemod/pull/5"},"issue":{"href":"https://api.github.com/repos/cpojer/js-codemod/issues/5"},"comments":{"href":"https://api.github.com/repos/cpojer/js-codemod/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/cpojer/js-codemod/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/cpojer/js-codemod/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cpojer/js-codemod/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/cpojer/js-codemod/statuses/6f18118a8d801b4ac765e93515dcdb5b8cbc86ad"}}}}