Skip to content

Commit

Permalink
Add proposal flag to pipeline plugin (#8196)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mAAdhaTTah authored and nicolo-ribaudo committed Jun 27, 2018
1 parent e166275 commit eac4c5b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 11 deletions.
@@ -1,3 +1,6 @@
{
"presets": [["stage-0", { "decoratorsLegacy": true }], "es2015"]
"presets": [
["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }],
"es2015"
]
}
@@ -1,7 +1,7 @@
{
"plugins": ["external-helpers", ["proposal-class-properties", {"loose": true}]],
"presets": [
["stage-0", { "decoratorsLegacy": true }],
["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }],
"es2015"
]
}
@@ -1,7 +1,7 @@
{
"plugins": ["external-helpers", "proposal-class-properties"],
"presets": [
["stage-0", { "decoratorsLegacy": true }],
["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }],
"es2015"
]
}
@@ -1,6 +1,6 @@
{
"presets": [
"es2015",
["stage-0", { "decoratorsLegacy": true }]
["stage-0", { "decoratorsLegacy": true, "pipelineProposal": "minimal" }]
]
}
@@ -1,3 +1,5 @@
{
"plugins": ["proposal-pipeline-operator"]
"plugins": [
["proposal-pipeline-operator", { "proposal": "minimal" }]
]
}
10 changes: 8 additions & 2 deletions 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 }]);
},
};
});
22 changes: 20 additions & 2 deletions packages/babel-preset-stage-0/src/index.js
Expand Up @@ -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.");
Expand All @@ -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],
};
});
17 changes: 15 additions & 2 deletions packages/babel-preset-stage-1/src/index.js
Expand Up @@ -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.");
Expand All @@ -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,
],
Expand Down

0 comments on commit eac4c5b

Please sign in to comment.