From eac4c5bc17133c2857f2c94c1a6a8643e3b547a7 Mon Sep 17 00:00:00 2001 From: James DiGioia Date: Wed, 27 Jun 2018 19:19:40 -0400 Subject: [PATCH] Add proposal flag to pipeline plugin (#8196) This is going to be required so we can add support for other proposals, as well as later set the accepted proposal as the default. Update stage-0 and stage-1 presets with `pipelineProposal` to thread down to the plugin. --- .../private-loose/foobar/options.json | 5 ++++- .../fixtures/public-loose/foobar/options.json | 2 +- .../test/fixtures/public/foobar/options.json | 2 +- .../legacy-regression/7030/options.json | 2 +- .../fixtures/pipeline-operator/options.json | 4 +++- .../src/index.js | 10 +++++++-- packages/babel-preset-stage-0/src/index.js | 22 +++++++++++++++++-- packages/babel-preset-stage-1/src/index.js | 17 ++++++++++++-- 8 files changed, 53 insertions(+), 11 deletions(-) diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/options.json index 8110be628e13..db6b4377adbf 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/options.json +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/foobar/options.json @@ -1,3 +1,6 @@ { - "presets": [["stage-0", { "decoratorsLegacy": true }], "es2015"] + "presets": [ + ["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }], + "es2015" + ] } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public-loose/foobar/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/public-loose/foobar/options.json index f5a0eb1d1651..50d88253d94e 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public-loose/foobar/options.json +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public-loose/foobar/options.json @@ -1,7 +1,7 @@ { "plugins": ["external-helpers", ["proposal-class-properties", {"loose": true}]], "presets": [ - ["stage-0", { "decoratorsLegacy": true }], + ["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }], "es2015" ] } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/options.json index e0415a05266a..e9dab83b7baf 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/options.json +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/options.json @@ -1,7 +1,7 @@ { "plugins": ["external-helpers", "proposal-class-properties"], "presets": [ - ["stage-0", { "decoratorsLegacy": true }], + ["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }], "es2015" ] } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/options.json index 64d67dac24ab..2b2c438c95d2 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/options.json +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/options.json @@ -1,6 +1,6 @@ { "presets": [ "es2015", - ["stage-0", { "decoratorsLegacy": true }] + ["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }] ] } diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/options.json b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/options.json index 1dece7986e4a..0a1dc4d01090 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/options.json +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/pipeline-operator/options.json @@ -1,3 +1,5 @@ { - "plugins": ["proposal-pipeline-operator"] + "plugins": [ + ["proposal-pipeline-operator", { "proposal": "minimal" }] + ] } diff --git a/packages/babel-plugin-syntax-pipeline-operator/src/index.js b/packages/babel-plugin-syntax-pipeline-operator/src/index.js index c77a915babae..ae371a379221 100644 --- a/packages/babel-plugin-syntax-pipeline-operator/src/index.js +++ b/packages/babel-plugin-syntax-pipeline-operator/src/index.js @@ -1,11 +1,17 @@ import { declare } from "@babel/helper-plugin-utils"; -export default declare(api => { +const proposals = ["minimal"]; + +export default declare((api, { proposal }) => { api.assertVersion(7); + if (typeof proposal !== "string" || !proposals.includes(proposal)) { + throw new Error("'proposal' must be one of: " + proposals.join(", ")); + } + return { manipulateOptions(opts, parserOpts) { - parserOpts.plugins.push("pipelineOperator"); + parserOpts.plugins.push(["pipelineOperator", { proposal }]); }, }; }); diff --git a/packages/babel-preset-stage-0/src/index.js b/packages/babel-preset-stage-0/src/index.js index 855614351d6c..b5034e4e6cea 100644 --- a/packages/babel-preset-stage-0/src/index.js +++ b/packages/babel-preset-stage-0/src/index.js @@ -6,7 +6,12 @@ import transformFunctionBind from "@babel/plugin-proposal-function-bind"; export default declare((api, opts = {}) => { api.assertVersion(7); - const { loose = false, useBuiltIns = false, decoratorsLegacy = false } = opts; + const { + loose = false, + useBuiltIns = false, + decoratorsLegacy = false, + pipelineProposal, + } = opts; if (typeof loose !== "boolean") { throw new Error("@babel/preset-stage-0 'loose' option must be a boolean."); @@ -30,8 +35,21 @@ export default declare((api, opts = {}) => { ); } + if (typeof pipelineProposal !== "string") { + throw new Error( + "The pipeline operator requires a proposal set." + + " You must pass the 'pipelineProposal' option to" + + " @babel/preset-stage-0", + ); + } + return { - presets: [[presetStage1, { loose, useBuiltIns, decoratorsLegacy }]], + presets: [ + [ + presetStage1, + { loose, useBuiltIns, decoratorsLegacy, pipelineProposal }, + ], + ], plugins: [transformFunctionBind], }; }); diff --git a/packages/babel-preset-stage-1/src/index.js b/packages/babel-preset-stage-1/src/index.js index f4e47dce627c..12d0c64de0c8 100644 --- a/packages/babel-preset-stage-1/src/index.js +++ b/packages/babel-preset-stage-1/src/index.js @@ -11,7 +11,12 @@ import transformDoExpressions from "@babel/plugin-proposal-do-expressions"; export default declare((api, opts = {}) => { api.assertVersion(7); - const { loose = false, useBuiltIns = false, decoratorsLegacy = false } = opts; + const { + loose = false, + useBuiltIns = false, + decoratorsLegacy = false, + pipelineProposal, + } = opts; if (typeof loose !== "boolean") { throw new Error("@babel/preset-stage-1 'loose' option must be a boolean."); @@ -35,13 +40,21 @@ export default declare((api, opts = {}) => { ); } + if (typeof pipelineProposal !== "string") { + throw new Error( + "The pipeline operator requires a proposal set." + + " You must pass 'pipelineProposal' option to" + + " @babel/preset-stage-1", + ); + } + return { presets: [[presetStage2, { loose, useBuiltIns, decoratorsLegacy }]], plugins: [ transformExportDefaultFrom, transformLogicalAssignmentOperators, [transformOptionalChaining, { loose }], - transformPipelineOperator, + [transformPipelineOperator, { proposal: pipelineProposal }], [transformNullishCoalescingOperator, { loose }], transformDoExpressions, ],