From 41d0ddb57eac2c1ec545f3fb7078258996ee2514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 24 Jul 2018 09:23:02 +0200 Subject: [PATCH] Remove stage presets --- .../__snapshots__/packageData.test.js.snap | 21 ++++- .../__snapshots__/upgradeConfig.test.js.snap | 77 ++++++++----------- __tests__/packageData.test.js | 6 ++ __tests__/upgradeConfig.test.js | 16 ++-- src/packageData.js | 30 ++++++++ src/upgradeConfig.js | 14 +++- src/upgradeDeps.js | 12 ++- src/upgradeOptions.js | 4 - 8 files changed, 117 insertions(+), 63 deletions(-) diff --git a/__tests__/__snapshots__/packageData.test.js.snap b/__tests__/__snapshots__/packageData.test.js.snap index b5df27c..7d2576f 100644 --- a/__tests__/__snapshots__/packageData.test.js.snap +++ b/__tests__/__snapshots__/packageData.test.js.snap @@ -40,6 +40,21 @@ Object { } `; +exports[`replaces stage presets 1`] = ` +Object { + "@babel/core": "7.0.0-beta.39", + "@babel/plugin-proposal-class-properties": "7.0.0-beta.39", + "@babel/plugin-proposal-decorators": "7.0.0-beta.39", + "@babel/plugin-proposal-export-namespace-from": "7.0.0-beta.39", + "@babel/plugin-proposal-function-sent": "7.0.0-beta.39", + "@babel/plugin-proposal-json-strings": "7.0.0-beta.39", + "@babel/plugin-proposal-numeric-separator": "7.0.0-beta.39", + "@babel/plugin-proposal-throw-expressions": "7.0.0-beta.39", + "@babel/plugin-syntax-dynamic-import": "7.0.0-beta.39", + "@babel/plugin-syntax-import-meta": "7.0.0-beta.39", +} +`; + exports[`scripts 1`] = ` Object { "name": "mocha-scripts-test", @@ -164,6 +179,8 @@ Object { "@babel/plugin-proposal-export-namespace-from": "7.0.0-beta.39", "@babel/plugin-proposal-function-bind": "7.0.0-beta.39", "@babel/plugin-proposal-function-sent": "7.0.0-beta.39", + "@babel/plugin-proposal-json-strings": "7.0.0-beta.39", + "@babel/plugin-proposal-logical-assignment-operators": "7.0.0-beta.39", "@babel/plugin-proposal-nullish-coalescing-operator": "7.0.0-beta.39", "@babel/plugin-proposal-numeric-separator": "7.0.0-beta.39", "@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.39", @@ -245,10 +262,6 @@ Object { "@babel/preset-env-standalone": "7.0.0-beta.39", "@babel/preset-flow": "7.0.0-beta.39", "@babel/preset-react": "7.0.0-beta.39", - "@babel/preset-stage-0": "7.0.0-beta.39", - "@babel/preset-stage-1": "7.0.0-beta.39", - "@babel/preset-stage-2": "7.0.0-beta.39", - "@babel/preset-stage-3": "7.0.0-beta.39", "@babel/preset-typescript": "7.0.0-beta.39", "@babel/register": "7.0.0-beta.39", "@babel/runtime": "7.0.0-beta.39", diff --git a/__tests__/__snapshots__/upgradeConfig.test.js.snap b/__tests__/__snapshots__/upgradeConfig.test.js.snap index cfbcbe8..694f77f 100644 --- a/__tests__/__snapshots__/upgradeConfig.test.js.snap +++ b/__tests__/__snapshots__/upgradeConfig.test.js.snap @@ -25,28 +25,6 @@ Object { }, ], ], - "presets": Array [ - Array [ - "@babel/preset-stage-0", - Object { - "decoratorsLegacy": true, - "pipelineProposal": "minimal", - }, - ], - Array [ - "@babel/preset-stage-1", - Object { - "decoratorsLegacy": true, - "pipelineProposal": "minimal", - }, - ], - Array [ - "@babel/preset-stage-2", - Object { - "decoratorsLegacy": true, - }, - ], - ], } `; @@ -66,28 +44,6 @@ Object { }, ], ], - "presets": Array [ - Array [ - "@babel/preset-stage-0", - Object { - "decoratorsLegacy": true, - "pipelineProposal": "minimal", - }, - ], - Array [ - "@babel/preset-stage-1", - Object { - "decoratorsLegacy": true, - "pipelineProposal": "minimal", - }, - ], - Array [ - "@babel/preset-stage-2", - Object { - "decoratorsLegacy": true, - }, - ], - ], } `; @@ -244,3 +200,36 @@ Object { ], } `; + +exports[`replaces stage presets 1`] = ` +Object { + "plugins": Array [ + "@babel/plugin-syntax-dynamic-import", + "@babel/plugin-syntax-import-meta", + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-json-strings", + Array [ + "@babel/plugin-proposal-decorators", + Object { + "legacy": true, + }, + ], + "@babel/plugin-proposal-function-sent", + "@babel/plugin-proposal-export-namespace-from", + "@babel/plugin-proposal-numeric-separator", + "@babel/plugin-proposal-throw-expressions", + "@babel/plugin-proposal-export-default-from", + "@babel/plugin-proposal-logical-assignment-operators", + "@babel/plugin-proposal-optional-chaining", + Array [ + "@babel/plugin-proposal-pipeline-operator", + Object { + "proposal": "minimal", + }, + ], + "@babel/plugin-proposal-nullish-coalescing-operator", + "@babel/plugin-proposal-do-expressions", + ], + "presets": Array [], +} +`; diff --git a/__tests__/packageData.test.js b/__tests__/packageData.test.js index ac6fd89..6d9aa54 100644 --- a/__tests__/packageData.test.js +++ b/__tests__/packageData.test.js @@ -79,3 +79,9 @@ test('jest-cli babel-core bridge', async () => { test('webpack v1 compatibility', async () => { expect(await updatePackageJSON(webpackV1Fixture)).toMatchSnapshot(); }); + +test('replaces stage presets', () => { + expect(upgradeDeps({ + "@babel/preset-stage-2": "7.0.0-alpha.0" + }, VERSION)).toMatchSnapshot(); +}); diff --git a/__tests__/upgradeConfig.test.js b/__tests__/upgradeConfig.test.js index a1eb6af..291b130 100644 --- a/__tests__/upgradeConfig.test.js +++ b/__tests__/upgradeConfig.test.js @@ -89,11 +89,6 @@ test("adds legacy option to decorators", () => { "transform-decorators", "@babel/plugin-syntax-decorators" ], - "presets": [ - ["@babel/preset-stage-0", { pipelineProposal: "minimal" }], - ["babel-preset-stage-1", { pipelineProposal: "minimal" }], - "stage-2" - ] }; expect(upgradeConfig(config)).toMatchSnapshot(); @@ -105,10 +100,15 @@ test("adds proposal option to pipeline", () => { "transform-pipeline-operator", "@babel/plugin-syntax-pipeline-operator" ], + }; + + expect(upgradeConfig(config)).toMatchSnapshot(); +}); + +test("replaces stage presets", () => { + const config = { "presets": [ - ["@babel/preset-stage-0", { decoratorsLegacy: true }], - ["babel-preset-stage-1", { decoratorsLegacy: true }], - ["stage-2", { decoratorsLegacy: true }] + "stage-1" ] }; diff --git a/src/packageData.js b/src/packageData.js index 9fe6bfd..e90965a 100644 --- a/src/packageData.js +++ b/src/packageData.js @@ -245,9 +245,39 @@ const packages = Object.assign( const latestPackages = new Set(Object.values(packages)); +const stagePresets = Object.create(null); +stagePresets[3] = [ + "@babel/plugin-syntax-dynamic-import", + "@babel/plugin-syntax-import-meta", + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-json-strings", +]; +stagePresets[2] = [ + ...stagePresets[3], + ["@babel/plugin-proposal-decorators", { "legacy": true }], + "@babel/plugin-proposal-function-sent", + "@babel/plugin-proposal-export-namespace-from", + "@babel/plugin-proposal-numeric-separator", + "@babel/plugin-proposal-throw-expressions", +]; +stagePresets[1] = [ + ...stagePresets[2], + "@babel/plugin-proposal-export-default-from", + "@babel/plugin-proposal-logical-assignment-operators", + "@babel/plugin-proposal-optional-chaining", + ["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }], + "@babel/plugin-proposal-nullish-coalescing-operator", + "@babel/plugin-proposal-do-expressions", +]; +stagePresets[0] = [ + ...stagePresets[1], + "@babel/plugin-proposal-function-bind", +]; + module.exports = { packages, presets, plugins, latestPackages, + stagePresets, }; diff --git a/src/upgradeConfig.js b/src/upgradeConfig.js index de48731..94a7aa1 100644 --- a/src/upgradeConfig.js +++ b/src/upgradeConfig.js @@ -1,4 +1,4 @@ -const { presets: oldPresets, plugins: oldPlugins } = require('./packageData'); +const { presets: oldPresets, plugins: oldPlugins, stagePresets } = require('./packageData'); const upgradeOptions = require('./upgradeOptions'); function changeName(originalName, kind) { @@ -23,6 +23,7 @@ function changeName(originalName, kind) { // TODO: fix all of this function changePresets(config, options = {}) { let presets = config.presets; + const newPlugins = []; if (!Array.isArray(presets) && typeof presets === 'string') { presets = config.presets = config.presets.split(',').map((preset) => preset.trim()); @@ -39,9 +40,14 @@ function changePresets(config, options = {}) { const isArray = Array.isArray(preset); const name = changeName(isArray ? preset[0] : preset, 'preset'); - if (name === null) { + if (name === null || name.startsWith('@babel/preset-stage-')) { presets.splice(i, 1); i--; + + if (name !== null) { + const stage = name.slice(-1); + newPlugins.push(stagePresets[stage]); + } } else { if (isArray) preset[0] = name; else preset = name; @@ -53,6 +59,10 @@ function changePresets(config, options = {}) { if (options.hasFlow && !presets.includes('@babel/preset-flow')) { presets.push('@babel/preset-flow'); } + + if (newPlugins.length > 0) { + config.plugins = (config.plugins || []).concat(...newPlugins); + } } } diff --git a/src/upgradeDeps.js b/src/upgradeDeps.js index 0313cbe..86d5a32 100644 --- a/src/upgradeDeps.js +++ b/src/upgradeDeps.js @@ -1,5 +1,5 @@ const semver = require('semver'); -const { packages: oldPackages, latestPackages } = require('./packageData'); +const { packages: oldPackages, latestPackages, stagePresets } = require('./packageData'); const otherPackages = { 'babel-loader': '^8.0.0-beta.0', @@ -81,5 +81,15 @@ module.exports = function upgradeDeps(dependencies, version, options = {}) { dependencies['babel-core'] = '^7.0.0-bridge.0'; } + for (let stage = 0; stage <= 3; stage++) { + if (dependencies[`@babel/preset-stage-${stage}`]) { + delete dependencies[`@babel/preset-stage-${stage}`]; + for (const plugin of stagePresets[stage]) { + const name = typeof plugin === "string" ? plugin : plugin[0]; + dependencies[name] = version; + } + } + } + return dependencies; } diff --git a/src/upgradeOptions.js b/src/upgradeOptions.js index 3942cc7..0cfd541 100644 --- a/src/upgradeOptions.js +++ b/src/upgradeOptions.js @@ -7,10 +7,6 @@ const updaters = { "@babel/plugin-proposal-pipeline-operator": define({ proposal: "minimal" }), "@babel/plugin-syntax-pipeline-operator": define({ proposal: "minimal" }), - - "@babel/preset-stage-0": define({ decoratorsLegacy: true, pipelineProposal: "minimal" }), - "@babel/preset-stage-1": define({ decoratorsLegacy: true, pipelineProposal: "minimal" }), - "@babel/preset-stage-2": define({ decoratorsLegacy: true }), }; module.exports = function updateOptions(entry) {