{"action":"created","comment":{"url":"https://api.github.com/repos/babel/babel-upgrade/pulls/comments/172054897","pull_request_review_id":101011190,"id":172054897,"diff_hunk":"@@ -1,110 +1,81 @@\n-const { presets: oldPresets, plugins: oldPlugins } = require('./packageData');\n+const packageData = require('./packageData');\n \n-// TODO: fix all of this\n-function changePresets(config, options = {}) {\n-  let presets = config.presets;\n+function parseConfigCollection(collection) {\n+  if (!Array.isArray(collection) && typeof collection === 'string') {\n+    return collection.split(',').map(collection => collection.trim());\n+  }\n+\n+  return collection;\n+}\n+\n+function parseConfigItem(rawConfigItem, configItemType) {\n+  let name = Array.isArray(rawConfigItem) ? rawConfigItem[0] : rawConfigItem;\n \n-  if (!Array.isArray(presets) && typeof presets === 'string') {\n-    presets = config.presets = config.presets.split(',').map((preset) => preset.trim());\n+  if (name.indexOf(`babel-${configItemType}`) !== 0 && name.indexOf('@babel/') !== 0) {\n+    name = `babel-${configItemType}-${name}`;\n   }\n \n-  // check if presets are there\n-  if (presets) {\n-    // assume it's an array\n-    for (let i = 0; i < presets.length; i++) {\n-      let preset = presets[i];\n-      const presetsToReplace = Object.keys(oldPresets);\n-\n-      // check if it's a preset with options (an array)\n-      if (Array.isArray(preset)) {\n-        if (preset[0].indexOf('babel-preset') !== 0 && preset[0].indexOf('@babel/') !== 0) {\n-          preset[0] = `babel-preset-${preset[0]}`;\n-        }\n-        if (presetsToReplace.includes(preset[0])) {\n-          if (oldPresets[preset[0]]) {\n-            preset[0] = oldPresets[preset[0]];\n-          } else {\n-            presets.splice(i, 1);\n-            i--;\n-          }\n-        }\n-      } else {\n-        if (preset.indexOf('babel-preset') !== 0 && preset.indexOf('@babel/') !== 0) {\n-          preset = `babel-preset-${preset}`;\n-        }\n-        if (presetsToReplace.includes(preset)) {\n-          if (oldPresets[preset]) {\n-            presets[i] = oldPresets[preset];\n-          } else {\n-            presets.splice(i, 1);\n-            i--;\n-          }\n-        }\n-      }\n-    }\n-\n-    if (options.hasFlow && !presets.includes('@babel/preset-flow')) {\n-      presets.push('@babel/preset-flow');\n-    }\n+  return {\n+    name,\n+    options: Array.isArray(rawConfigItem) ? rawConfigItem[1] : {}\n+  };\n+}\n+\n+function formatConfigItem(item) {\n+  return Object.keys(item.options).length > 0 ? [item.name, item.options] : item.name;\n+}\n+\n+const buildConfigItemTransformer = configItemType => configItem => {\n+  const newConfigItem = parseConfigItem(configItem, configItemType);\n+\n+  const newName = packageData[`${configItemType}s`][newConfigItem.name];\n+  if (newName !== undefined) {\n+    return formatConfigItem(Object.assign(newConfigItem, { name: newName }));\n   }\n+\n+  return configItem;\n+};\n+\n+function buildConfigCollectionTransformer(configItemType) {\n+  const itemTransformer = buildConfigItemTransformer(configItemType);\n+  return function(configItems) {\n+    configItems = parseConfigCollection(configItems);\n+\n+    return configItems && configItems.map(itemTransformer).filter(Boolean);\n+  };\n }\n \n-function changePlugins(config) {\n-  let plugins = config.plugins;\n+const transformPresets = buildConfigCollectionTransformer('preset');\n+const transformPlugins = buildConfigCollectionTransformer('plugin');\n+\n+function appendFlowIfNeeded(presets) {\n+  return presets.find(preset => preset === '@babel/preset-flow' || preset[0] === '@babel/preset-flow')\n+    ? presets\n+    : presets.concat(['@babel/preset-flow']);\n+}\n \n-  if (!Array.isArray(plugins) && typeof plugins === 'string') {\n-    plugins = config.plugins = config.plugins.split(',').map((plugin) => plugin.trim());\n+function upgradeConfigForEnv(config, options) {\n+  if (config.presets) {\n+    config.presets = options.hasFlow\n+      ? appendFlowIfNeeded(transformPresets(config.presets))\n+      : transformPresets(config.presets);\n   }\n \n-  // check if plugins are there\n-  if (plugins) {\n-    // assume it's an array\n-    for (let i = 0; i < plugins.length; i++) {\n-      let plugin = plugins[i];\n-      const pluginsToReplace = Object.keys(oldPlugins);\n-\n-      // check if it's a plugin with options (an array)\n-      if (Array.isArray(plugin)) {\n-\n-        if (plugin[0].indexOf('babel-plugin') !== 0 && plugin[0].indexOf('@babel/') !== 0) {\n-          plugin[0] = `babel-plugin-${plugin[0]}`;\n-        }\n-\n-        if (pluginsToReplace.includes(plugin[0])) {\n-          if (oldPlugins[plugin[0]]) {\n-            plugin[0] = oldPlugins[plugin[0]];\n-          } else {\n-            plugins.splice(i, 1);\n-            i--;\n-          }\n-        }\n-      } else {\n-        if (plugin.indexOf('babel-plugin') !== 0 && plugin[0].indexOf('@babel/') !== 0) {\n-          plugin = `babel-plugin-${plugin}`;\n-        }\n-        if (pluginsToReplace.includes(plugin)) {\n-          if (oldPlugins[plugin]) {\n-            plugins[i] = oldPlugins[plugin];\n-          } else {\n-            plugins.splice(i, 1);\n-            i--;\n-          }\n-        }\n-      }\n-    }\n+  if (config.plugins) {\n+    config.plugins = transformPlugins(config.plugins);\n   }\n }\n \n-module.exports = function upgradeConfig(config, options) {\n+module.exports = function upgradeConfig(config, options = {}) {\n   config = Object.assign({}, config);\n \n-  changePresets(config, options);\n-  changePlugins(config);\n+  upgradeConfigForEnv(config, options);\n \n   if (config.env) {\n-    Object.keys(config.env).forEach((env) => {\n-      changePresets(config.env[env]);\n-      changePlugins(config.env[env]);\n+    Object.keys(config.env).forEach(env => {","path":"src/upgradeConfig.js","position":165,"original_position":165,"commit_id":"6fcdef56352cc3ba66cfcd2b24297f7e727b73ea","original_commit_id":"6fcdef56352cc3ba66cfcd2b24297f7e727b73ea","user":{"login":"kedromelon","id":5393571,"avatar_url":"https://avatars1.githubusercontent.com/u/5393571?v=4","gravatar_id":"","url":"https://api.github.com/users/kedromelon","html_url":"https://github.com/kedromelon","followers_url":"https://api.github.com/users/kedromelon/followers","following_url":"https://api.github.com/users/kedromelon/following{/other_user}","gists_url":"https://api.github.com/users/kedromelon/gists{/gist_id}","starred_url":"https://api.github.com/users/kedromelon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kedromelon/subscriptions","organizations_url":"https://api.github.com/users/kedromelon/orgs","repos_url":"https://api.github.com/users/kedromelon/repos","events_url":"https://api.github.com/users/kedromelon/events{/privacy}","received_events_url":"https://api.github.com/users/kedromelon/received_events","type":"User","site_admin":false},"body":"heh, great point. that totally didn't occur to me while moving this code around 😆 ","created_at":"2018-03-04T16:32:20Z","updated_at":"2018-03-04T16:32:20Z","html_url":"https://github.com/babel/babel-upgrade/pull/50#discussion_r172054897","pull_request_url":"https://api.github.com/repos/babel/babel-upgrade/pulls/50","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/babel/babel-upgrade/pulls/comments/172054897"},"html":{"href":"https://github.com/babel/babel-upgrade/pull/50#discussion_r172054897"},"pull_request":{"href":"https://api.github.com/repos/babel/babel-upgrade/pulls/50"}},"in_reply_to_id":172048444},"pull_request":{"url":"https://api.github.com/repos/babel/babel-upgrade/pulls/50","id":172643512,"html_url":"https://github.com/babel/babel-upgrade/pull/50","diff_url":"https://github.com/babel/babel-upgrade/pull/50.diff","patch_url":"https://github.com/babel/babel-upgrade/pull/50.patch","issue_url":"https://api.github.com/repos/babel/babel-upgrade/issues/50","number":50,"state":"open","locked":false,"title":"refactor upgradeConfig.js","user":{"login":"kedromelon","id":5393571,"avatar_url":"https://avatars1.githubusercontent.com/u/5393571?v=4","gravatar_id":"","url":"https://api.github.com/users/kedromelon","html_url":"https://github.com/kedromelon","followers_url":"https://api.github.com/users/kedromelon/followers","following_url":"https://api.github.com/users/kedromelon/following{/other_user}","gists_url":"https://api.github.com/users/kedromelon/gists{/gist_id}","starred_url":"https://api.github.com/users/kedromelon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kedromelon/subscriptions","organizations_url":"https://api.github.com/users/kedromelon/orgs","repos_url":"https://api.github.com/users/kedromelon/repos","events_url":"https://api.github.com/users/kedromelon/events{/privacy}","received_events_url":"https://api.github.com/users/kedromelon/received_events","type":"User","site_admin":false},"body":"refactored this file to be (in my opinion) a bit easier to follow\r\n\r\nnot attached to this approach, very open to other suggestions too!!","created_at":"2018-03-03T01:56:47Z","updated_at":"2018-03-04T16:32:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a473dc5016b5a4535b1506457a12fe6516fd9c5d","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/babel/babel-upgrade/pulls/50/commits","review_comments_url":"https://api.github.com/repos/babel/babel-upgrade/pulls/50/comments","review_comment_url":"https://api.github.com/repos/babel/babel-upgrade/pulls/comments{/number}","comments_url":"https://api.github.com/repos/babel/babel-upgrade/issues/50/comments","statuses_url":"https://api.github.com/repos/babel/babel-upgrade/statuses/6fcdef56352cc3ba66cfcd2b24297f7e727b73ea","head":{"label":"kedromelon:refactor-config","ref":"refactor-config","sha":"6fcdef56352cc3ba66cfcd2b24297f7e727b73ea","user":{"login":"kedromelon","id":5393571,"avatar_url":"https://avatars1.githubusercontent.com/u/5393571?v=4","gravatar_id":"","url":"https://api.github.com/users/kedromelon","html_url":"https://github.com/kedromelon","followers_url":"https://api.github.com/users/kedromelon/followers","following_url":"https://api.github.com/users/kedromelon/following{/other_user}","gists_url":"https://api.github.com/users/kedromelon/gists{/gist_id}","starred_url":"https://api.github.com/users/kedromelon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kedromelon/subscriptions","organizations_url":"https://api.github.com/users/kedromelon/orgs","repos_url":"https://api.github.com/users/kedromelon/repos","events_url":"https://api.github.com/users/kedromelon/events{/privacy}","received_events_url":"https://api.github.com/users/kedromelon/received_events","type":"User","site_admin":false},"repo":{"id":123058883,"name":"babel-upgrade","full_name":"kedromelon/babel-upgrade","owner":{"login":"kedromelon","id":5393571,"avatar_url":"https://avatars1.githubusercontent.com/u/5393571?v=4","gravatar_id":"","url":"https://api.github.com/users/kedromelon","html_url":"https://github.com/kedromelon","followers_url":"https://api.github.com/users/kedromelon/followers","following_url":"https://api.github.com/users/kedromelon/following{/other_user}","gists_url":"https://api.github.com/users/kedromelon/gists{/gist_id}","starred_url":"https://api.github.com/users/kedromelon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kedromelon/subscriptions","organizations_url":"https://api.github.com/users/kedromelon/orgs","repos_url":"https://api.github.com/users/kedromelon/repos","events_url":"https://api.github.com/users/kedromelon/events{/privacy}","received_events_url":"https://api.github.com/users/kedromelon/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kedromelon/babel-upgrade","description":"Tool for upgrading babel versions (v7): `npx babel-upgrade`","fork":true,"url":"https://api.github.com/repos/kedromelon/babel-upgrade","forks_url":"https://api.github.com/repos/kedromelon/babel-upgrade/forks","keys_url":"https://api.github.com/repos/kedromelon/babel-upgrade/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kedromelon/babel-upgrade/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kedromelon/babel-upgrade/teams","hooks_url":"https://api.github.com/repos/kedromelon/babel-upgrade/hooks","issue_events_url":"https://api.github.com/repos/kedromelon/babel-upgrade/issues/events{/number}","events_url":"https://api.github.com/repos/kedromelon/babel-upgrade/events","assignees_url":"https://api.github.com/repos/kedromelon/babel-upgrade/assignees{/user}","branches_url":"https://api.github.com/repos/kedromelon/babel-upgrade/branches{/branch}","tags_url":"https://api.github.com/repos/kedromelon/babel-upgrade/tags","blobs_url":"https://api.github.com/repos/kedromelon/babel-upgrade/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kedromelon/babel-upgrade/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kedromelon/babel-upgrade/git/refs{/sha}","trees_url":"https://api.github.com/repos/kedromelon/babel-upgrade/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kedromelon/babel-upgrade/statuses/{sha}","languages_url":"https://api.github.com/repos/kedromelon/babel-upgrade/languages","stargazers_url":"https://api.github.com/repos/kedromelon/babel-upgrade/stargazers","contributors_url":"https://api.github.com/repos/kedromelon/babel-upgrade/contributors","subscribers_url":"https://api.github.com/repos/kedromelon/babel-upgrade/subscribers","subscription_url":"https://api.github.com/repos/kedromelon/babel-upgrade/subscription","commits_url":"https://api.github.com/repos/kedromelon/babel-upgrade/commits{/sha}","git_commits_url":"https://api.github.com/repos/kedromelon/babel-upgrade/git/commits{/sha}","comments_url":"https://api.github.com/repos/kedromelon/babel-upgrade/comments{/number}","issue_comment_url":"https://api.github.com/repos/kedromelon/babel-upgrade/issues/comments{/number}","contents_url":"https://api.github.com/repos/kedromelon/babel-upgrade/contents/{+path}","compare_url":"https://api.github.com/repos/kedromelon/babel-upgrade/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kedromelon/babel-upgrade/merges","archive_url":"https://api.github.com/repos/kedromelon/babel-upgrade/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kedromelon/babel-upgrade/downloads","issues_url":"https://api.github.com/repos/kedromelon/babel-upgrade/issues{/number}","pulls_url":"https://api.github.com/repos/kedromelon/babel-upgrade/pulls{/number}","milestones_url":"https://api.github.com/repos/kedromelon/babel-upgrade/milestones{/number}","notifications_url":"https://api.github.com/repos/kedromelon/babel-upgrade/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kedromelon/babel-upgrade/labels{/name}","releases_url":"https://api.github.com/repos/kedromelon/babel-upgrade/releases{/id}","deployments_url":"https://api.github.com/repos/kedromelon/babel-upgrade/deployments","created_at":"2018-02-27T02:10:51Z","updated_at":"2018-02-27T02:10:52Z","pushed_at":"2018-03-03T20:56:22Z","git_url":"git://github.com/kedromelon/babel-upgrade.git","ssh_url":"git@github.com:kedromelon/babel-upgrade.git","clone_url":"https://github.com/kedromelon/babel-upgrade.git","svn_url":"https://github.com/kedromelon/babel-upgrade","homepage":"","size":248,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"babel:master","ref":"master","sha":"51534a4bd3f742d284d42e231ae36e71c83bc93d","user":{"login":"babel","id":9637642,"avatar_url":"https://avatars1.githubusercontent.com/u/9637642?v=4","gravatar_id":"","url":"https://api.github.com/users/babel","html_url":"https://github.com/babel","followers_url":"https://api.github.com/users/babel/followers","following_url":"https://api.github.com/users/babel/following{/other_user}","gists_url":"https://api.github.com/users/babel/gists{/gist_id}","starred_url":"https://api.github.com/users/babel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/babel/subscriptions","organizations_url":"https://api.github.com/users/babel/orgs","repos_url":"https://api.github.com/users/babel/repos","events_url":"https://api.github.com/users/babel/events{/privacy}","received_events_url":"https://api.github.com/users/babel/received_events","type":"Organization","site_admin":false},"repo":{"id":121311248,"name":"babel-upgrade","full_name":"babel/babel-upgrade","owner":{"login":"babel","id":9637642,"avatar_url":"https://avatars1.githubusercontent.com/u/9637642?v=4","gravatar_id":"","url":"https://api.github.com/users/babel","html_url":"https://github.com/babel","followers_url":"https://api.github.com/users/babel/followers","following_url":"https://api.github.com/users/babel/following{/other_user}","gists_url":"https://api.github.com/users/babel/gists{/gist_id}","starred_url":"https://api.github.com/users/babel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/babel/subscriptions","organizations_url":"https://api.github.com/users/babel/orgs","repos_url":"https://api.github.com/users/babel/repos","events_url":"https://api.github.com/users/babel/events{/privacy}","received_events_url":"https://api.github.com/users/babel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/babel/babel-upgrade","description":"Tool for upgrading babel versions (v7): `npx babel-upgrade`","fork":false,"url":"https://api.github.com/repos/babel/babel-upgrade","forks_url":"https://api.github.com/repos/babel/babel-upgrade/forks","keys_url":"https://api.github.com/repos/babel/babel-upgrade/keys{/key_id}","collaborators_url":"https://api.github.com/repos/babel/babel-upgrade/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/babel/babel-upgrade/teams","hooks_url":"https://api.github.com/repos/babel/babel-upgrade/hooks","issue_events_url":"https://api.github.com/repos/babel/babel-upgrade/issues/events{/number}","events_url":"https://api.github.com/repos/babel/babel-upgrade/events","assignees_url":"https://api.github.com/repos/babel/babel-upgrade/assignees{/user}","branches_url":"https://api.github.com/repos/babel/babel-upgrade/branches{/branch}","tags_url":"https://api.github.com/repos/babel/babel-upgrade/tags","blobs_url":"https://api.github.com/repos/babel/babel-upgrade/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/babel/babel-upgrade/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/babel/babel-upgrade/git/refs{/sha}","trees_url":"https://api.github.com/repos/babel/babel-upgrade/git/trees{/sha}","statuses_url":"https://api.github.com/repos/babel/babel-upgrade/statuses/{sha}","languages_url":"https://api.github.com/repos/babel/babel-upgrade/languages","stargazers_url":"https://api.github.com/repos/babel/babel-upgrade/stargazers","contributors_url":"https://api.github.com/repos/babel/babel-upgrade/contributors","subscribers_url":"https://api.github.com/repos/babel/babel-upgrade/subscribers","subscription_url":"https://api.github.com/repos/babel/babel-upgrade/subscription","commits_url":"https://api.github.com/repos/babel/babel-upgrade/commits{/sha}","git_commits_url":"https://api.github.com/repos/babel/babel-upgrade/git/commits{/sha}","comments_url":"https://api.github.com/repos/babel/babel-upgrade/comments{/number}","issue_comment_url":"https://api.github.com/repos/babel/babel-upgrade/issues/comments{/number}","contents_url":"https://api.github.com/repos/babel/babel-upgrade/contents/{+path}","compare_url":"https://api.github.com/repos/babel/babel-upgrade/compare/{base}...{head}","merges_url":"https://api.github.com/repos/babel/babel-upgrade/merges","archive_url":"https://api.github.com/repos/babel/babel-upgrade/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/babel/babel-upgrade/downloads","issues_url":"https://api.github.com/repos/babel/babel-upgrade/issues{/number}","pulls_url":"https://api.github.com/repos/babel/babel-upgrade/pulls{/number}","milestones_url":"https://api.github.com/repos/babel/babel-upgrade/milestones{/number}","notifications_url":"https://api.github.com/repos/babel/babel-upgrade/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/babel/babel-upgrade/labels{/name}","releases_url":"https://api.github.com/repos/babel/babel-upgrade/releases{/id}","deployments_url":"https://api.github.com/repos/babel/babel-upgrade/deployments","created_at":"2018-02-12T22:36:46Z","updated_at":"2018-03-03T17:16:51Z","pushed_at":"2018-03-03T20:56:24Z","git_url":"git://github.com/babel/babel-upgrade.git","ssh_url":"git@github.com:babel/babel-upgrade.git","clone_url":"https://github.com/babel/babel-upgrade.git","svn_url":"https://github.com/babel/babel-upgrade","homepage":"","size":234,"stargazers_count":125,"watchers_count":125,"language":"JavaScript","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":14,"mirror_url":null,"archived":false,"open_issues_count":16,"license":null,"forks":14,"open_issues":16,"watchers":125,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/babel/babel-upgrade/pulls/50"},"html":{"href":"https://github.com/babel/babel-upgrade/pull/50"},"issue":{"href":"https://api.github.com/repos/babel/babel-upgrade/issues/50"},"comments":{"href":"https://api.github.com/repos/babel/babel-upgrade/issues/50/comments"},"review_comments":{"href":"https://api.github.com/repos/babel/babel-upgrade/pulls/50/comments"},"review_comment":{"href":"https://api.github.com/repos/babel/babel-upgrade/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/babel/babel-upgrade/pulls/50/commits"},"statuses":{"href":"https://api.github.com/repos/babel/babel-upgrade/statuses/6fcdef56352cc3ba66cfcd2b24297f7e727b73ea"}},"author_association":"NONE"}}
{"actor":{"display_login":"kedromelon"}}