UNPKG

1.23 kBJavaScriptView Raw
1
2/**
3 * Copyright (c) Facebook, Inc. and its affiliates.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9'use strict';
10
11const babylon = require('@babel/parser');
12
13// These are the options that were the default of the Babel5 parse function
14// see https://github.com/babel/babel/blob/5.x/packages/babel/src/api/node.js#L81
15const options = {
16 sourceType: 'module',
17 allowHashBang: true,
18 ecmaVersion: Infinity,
19 allowImportExportEverywhere: true,
20 allowReturnOutsideFunction: true,
21 startLine: 1,
22 tokens: true,
23 plugins: [
24 'estree',
25 'jsx',
26 'asyncGenerators',
27 'classProperties',
28 'doExpressions',
29 'exportExtensions',
30 'functionBind',
31 'functionSent',
32 'objectRestSpread',
33 'dynamicImport',
34 'nullishCoalescingOperator',
35 'optionalChaining',
36 ['decorators', {decoratorsBeforeExport: false}],
37 ],
38};
39
40/**
41 * Wrapper to set default options. Doesn't accept custom options because in that
42 * case babylon should be used instead.
43 */
44module.exports = function() {
45 return {
46 parse(code) {
47 return babylon.parse(code, options);
48 },
49 };
50};