forked from babel/babel
/
index.ts
58 lines (51 loc) 路 1.63 KB
/
index.ts
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
import type { Options } from "../options";
import type * as N from "../types";
import type { PluginList } from "../plugin-utils";
import { getOptions } from "../options";
import StatementParser from "./statement";
import ScopeHandler from "../util/scope";
export type PluginsMap = Map<
string,
{
[x: string]: any;
}
>;
export default class Parser extends StatementParser {
// Forward-declaration so typescript plugin can override jsx plugin
// todo(flow->ts) - this probably can be removed
// abstract jsxParseOpeningElementAfterName(
// node: N.JSXOpeningElement,
// ): N.JSXOpeningElement;
constructor(options: Options | undefined | null, input: string) {
options = getOptions(options);
super(options, input);
this.options = options;
this.initializeScopes();
this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename;
}
// This can be overwritten, for example, by the TypeScript plugin.
getScopeHandler(): {
new (...args: any): ScopeHandler;
} {
return ScopeHandler;
}
parse(): N.File {
this.enterInitialScopes();
const file = this.startNode() as N.File;
const program = this.startNode() as N.Program;
this.nextToken();
file.errors = null;
this.parseTopLevel(file, program);
file.errors = this.state.errors;
return file;
}
}
function pluginsMap(plugins: PluginList): PluginsMap {
const pluginMap: PluginsMap = new Map();
for (const plugin of plugins) {
const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
if (!pluginMap.has(name)) pluginMap.set(name, options || {});
}
return pluginMap;
}