|
12 | 12 |
|
13 | 13 | const babel = require('@babel/core');
|
14 | 14 |
|
15 |
| -const babelParserOptions = { |
16 |
| - sourceType: 'module', |
17 |
| - strictMode: false, |
18 |
| - tokens: true, |
19 |
| - plugins: [ |
20 |
| - 'jsx', |
21 |
| - 'flow', |
22 |
| - 'estree', |
23 |
| - 'doExpressions', |
24 |
| - 'objectRestSpread', |
25 |
| - 'classProperties', |
26 |
| - 'classPrivateProperties', |
27 |
| - 'classPrivateMethods', |
28 |
| - 'exportDefaultFrom', |
29 |
| - 'exportNamespaceFrom', |
30 |
| - 'asyncGenerators', |
31 |
| - 'functionBind', |
32 |
| - 'functionSent', |
33 |
| - 'dynamicImport', |
34 |
| - 'numericSeparator', |
35 |
| - 'optionalChaining', |
36 |
| - 'importMeta', |
37 |
| - 'bigInt', |
38 |
| - 'optionalCatchBinding', |
39 |
| - 'throwExpressions', |
40 |
| - ['pipelineOperator', { proposal: 'minimal' }], |
41 |
| - 'nullishCoalescingOperator', |
42 |
| - ], |
| 15 | +const defaultPlugins = [ |
| 16 | + 'jsx', |
| 17 | + 'flow', |
| 18 | + 'asyncGenerators', |
| 19 | + 'bigInt', |
| 20 | + 'classProperties', |
| 21 | + 'classPrivateProperties', |
| 22 | + 'classPrivateMethods', |
| 23 | + ['decorators', { decoratorsBeforeExport: false }], |
| 24 | + 'doExpressions', |
| 25 | + 'dynamicImport', |
| 26 | + 'exportDefaultFrom', |
| 27 | + 'exportNamespaceFrom', |
| 28 | + 'functionBind', |
| 29 | + 'functionSent', |
| 30 | + 'importMeta', |
| 31 | + 'logicalAssignment', |
| 32 | + 'nullishCoalescingOperator', |
| 33 | + 'numericSeparator', |
| 34 | + 'objectRestSpread', |
| 35 | + 'optionalCatchBinding', |
| 36 | + 'optionalChaining', |
| 37 | + ['pipelineOperator', { proposal: 'minimal' }], |
| 38 | + 'throwExpressions', |
| 39 | +]; |
| 40 | + |
| 41 | +type ParserOptions = { |
| 42 | + plugins?: Array<string | [string, {}]>, |
| 43 | + tokens?: boolean, |
43 | 44 | };
|
44 | 45 |
|
45 | 46 | export type Options = {
|
46 | 47 | cwd?: string,
|
47 | 48 | filename?: string,
|
48 |
| - legacyDecorators?: boolean, |
49 |
| - decoratorsBeforeExport?: boolean, |
| 49 | + parserOptions?: ParserOptions, |
50 | 50 | };
|
51 | 51 |
|
52 |
| -function buildOptions(options: Options) { |
53 |
| - const parserOptions = { |
54 |
| - strictMode: false, |
55 |
| - tokens: true, |
| 52 | +function buildOptions({ |
| 53 | + cwd, |
| 54 | + filename, |
| 55 | + parserOptions, |
| 56 | +}: Options): ParserOptions { |
| 57 | + let options = { |
56 | 58 | plugins: [],
|
57 | 59 | };
|
58 | 60 |
|
59 |
| - if (options.legacyDecorators) { |
60 |
| - parserOptions.plugins.push('decorators-legacy'); |
| 61 | + if (parserOptions) { |
| 62 | + options = { |
| 63 | + ...parserOptions, |
| 64 | + plugins: parserOptions.plugins ? [...parserOptions.plugins] : [], |
| 65 | + }; |
61 | 66 | }
|
62 | 67 |
|
63 | 68 | const partialConfig = babel.loadPartialConfig({
|
64 |
| - cwd: options.cwd, |
65 |
| - filename: options.filename, |
| 69 | + cwd, |
| 70 | + filename, |
66 | 71 | });
|
67 | 72 |
|
68 |
| - if (!partialConfig.hasFilesystemConfig()) { |
69 |
| - parserOptions.plugins = [...babelParserOptions.plugins]; |
70 |
| - |
71 |
| - if (!options.legacyDecorators) { |
72 |
| - parserOptions.plugins.push([ |
73 |
| - 'decorators', |
74 |
| - { decoratorsBeforeExport: options.decoratorsBeforeExport || false }, |
75 |
| - ]); |
76 |
| - } |
| 73 | + if (!partialConfig.hasFilesystemConfig() && options.plugins.length === 0) { |
| 74 | + options.plugins = [...defaultPlugins]; |
77 | 75 | }
|
78 | 76 |
|
79 |
| - return parserOptions; |
| 77 | + // Recast needs tokens to be in the tree |
| 78 | + // $FlowIssue tokens is clearly in the Options |
| 79 | + options.tokens = true; |
| 80 | + // Ensure we always have estree plugin enabled, if we add it a second time |
| 81 | + // here it does not matter |
| 82 | + options.plugins.push('estree'); |
| 83 | + |
| 84 | + return options; |
80 | 85 | }
|
81 | 86 |
|
82 | 87 | export default function buildParse(options?: Options = {}) {
|
|
0 commit comments