-
Notifications
You must be signed in to change notification settings - Fork 55
/
parse.js
58 lines (51 loc) · 1.29 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
export const DEFAULT_PARSER_PLUGINS = [
'jsx',
'doExpressions',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportExtensions',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
[
'pipelineOperator',
{
proposal: 'minimal',
},
],
'nullishCoalescingOperator',
'exportNamespaceFrom',
'exportDefaultFrom',
];
const TYPESCRIPT_FILE_PATH_REGEX = /\.tsx?$/;
let parserPlugins = DEFAULT_PARSER_PLUGINS;
export function configureParserPlugins(newParserPlugins) {
parserPlugins = newParserPlugins;
return;
}
function getParserPlugins(absolutePathToFile) {
const typePlugin = TYPESCRIPT_FILE_PATH_REGEX.test(absolutePathToFile)
? 'typescript'
: 'flow';
return [typePlugin, ...parserPlugins];
}
export default function parse(fileContent, absolutePathToFile) {
// For some reason, we can't `import` @babel/parser using an es6 import.
const babelParser = require('@babel/parser');
return babelParser.parse(fileContent, {
allowImportExportEverywhere: true,
plugins: getParserPlugins(absolutePathToFile),
sourceType: 'module',
});
}