| 1 |
import { declare } from "@babel/helper-plugin-utils"; |
| 2 |
|
| 3 |
const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"] as const; |
| 4 |
const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"] as const; |
| 5 |
const documentationURL = |
| 6 |
"https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator"; |
| 7 |
|
| 8 |
export interface Options { |
| •9 |
proposal: (typeof PIPELINE_PROPOSALS)[number]; |
| 10 |
topicToken?: (typeof TOPIC_TOKENS)[number]; |
| 11 |
} |
| 12 |
|
| 13 |
export default declare((api, { proposal, topicToken }: Options) => { |
| 14 |
api.assertVersion(7); |
| 15 |
|
| 16 |
if (typeof proposal !== "string" || !PIPELINE_PROPOSALS.includes(proposal)) { |
| 17 |
const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", "); |
| 18 |
throw new Error( |
| 19 |
`The pipeline plugin requires a "proposal" option. "proposal" must be one of: ${proposalList}. See <${documentationURL}>.`, |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
if (proposal === "hack" && !TOPIC_TOKENS.includes(topicToken)) { |
| 24 |
const topicTokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", "); |
| 25 |
throw new Error( |
| 26 |
`The pipeline plugin in "proposal": "hack" mode also requires a "topicToken" option. "topicToken" must be one of: ${topicTokenList}. See <${documentationURL}>.`, |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
return { |
| 31 |
name: "syntax-pipeline-operator", |
| 32 |
|
| 33 |
manipulateOptions(opts, parserOpts) { |
| 34 |
// Add parser options. |
| •35 |
// @ts-ignore |
| 36 |
parserOpts.plugins.push(["pipelineOperator", { proposal, topicToken }]); |
| 37 |
|
| 38 |
// Add generator options. |
| 39 |
opts.generatorOpts.topicToken = topicToken; |
| 40 |
}, |
| 41 |
}; |
| 42 |
}); |