UNPKG

1.38 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.getBabelParserOptions = getBabelParserOptions;
5exports.babelParseToAst = babelParseToAst;
6
7const parser = require(`@babel/parser`);
8
9const PARSER_OPTIONS = {
10 allowImportExportEverywhere: true,
11 allowReturnOutsideFunction: true,
12 allowSuperOutsideMethod: true,
13 sourceType: `unambigious`,
14 sourceFilename: true,
15 plugins: [`jsx`, `flow`, `doExpressions`, `objectRestSpread`, [`decorators`, {
16 decoratorsBeforeExport: true
17 }], `classProperties`, `classPrivateProperties`, `classPrivateMethods`, `exportDefaultFrom`, `exportNamespaceFrom`, `asyncGenerators`, `functionBind`, `functionSent`, `dynamicImport`, `numericSeparator`, `optionalChaining`, `importMeta`, `bigInt`, `optionalCatchBinding`, `throwExpressions`, [`pipelineOperator`, {
18 proposal: `minimal`
19 }], `nullishCoalescingOperator`]
20};
21
22function getBabelParserOptions(filePath) {
23 // Flow and TypeScript plugins can't be enabled simultaneously
24 if (/\.tsx?/.test(filePath)) {
25 const {
26 plugins
27 } = PARSER_OPTIONS;
28 return Object.assign({}, PARSER_OPTIONS, {
29 plugins: plugins.map(plugin => plugin === `flow` ? `typescript` : plugin)
30 });
31 }
32
33 return PARSER_OPTIONS;
34}
35
36function babelParseToAst(contents, filePath) {
37 return parser.parse(contents, getBabelParserOptions(filePath));
38}
39//# sourceMappingURL=babel-parse-to-ast.js.map
\No newline at end of file