forked from babel/babel
/
base.js
61 lines (55 loc) 路 2.16 KB
/
base.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
59
60
61
// @flow
import type { Options } from "../options";
import type State from "../tokenizer/state";
import type { PluginsMap } from "./index";
import type ScopeHandler from "../util/scope";
import type ExpressionScopeHandler from "../util/expression-scope";
import type ClassScopeHandler from "../util/class-scope";
import type ProductionParameterHandler from "../util/production-parameter";
export default class BaseParser {
// Properties set by constructor in index.js
declare options: Options;
declare inModule: boolean;
declare scope: ScopeHandler<*>;
declare classScope: ClassScopeHandler;
declare prodParam: ProductionParameterHandler;
declare expressionScope: ExpressionScopeHandler;
declare plugins: PluginsMap;
declare filename: ?string;
// Names of exports store. `default` is stored as a name for both
// `export default foo;` and `export { foo as default };`.
declare exportedIdentifiers: Set<string>;
sawUnambiguousESM: boolean = false;
ambiguousScriptDifferentAst: boolean = false;
// Initialized by Tokenizer
declare state: State;
// input and length are not in state as they are constant and we do
// not want to ever copy them, which happens if state gets cloned
declare input: string;
declare length: number;
// This method accepts either a string (plugin name) or an array pair
// (plugin name and options object). If an options object is given,
// then each value is non-recursively checked for identity with that
// plugin鈥檚 actual option value.
hasPlugin(pluginConfig: PluginConfig): boolean {
if (typeof pluginConfig === "string") {
return this.plugins.has(pluginConfig);
} else {
const [pluginName, pluginOptions] = pluginConfig;
if (!this.hasPlugin(pluginName)) {
return false;
}
const actualOptions = this.plugins.get(pluginName);
for (const key of Object.keys(pluginOptions)) {
if (actualOptions?.[key] !== pluginOptions[key]) {
return false;
}
}
return true;
}
}
getPluginOption(plugin: string, name: string) {
return this.plugins.get(plugin)?.[name];
}
}
export type PluginConfig = string | [string, { [string]: any }];