-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathanalyzer.js
More file actions
48 lines (43 loc) · 1.41 KB
/
analyzer.js
File metadata and controls
48 lines (43 loc) · 1.41 KB
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
/**
* Low-level module analysis primitives for ESM.
*
* {@link analyzeModule} returns a per-parse context object
* holding plain `{ visitor }` objects and a `buildRecord` function.
*
* Consumers are responsible for:
* 1. Traversing the AST with `ctx.analyzePass.visitor`.
* 2. Traversing the (mutated) AST with `ctx.transformPass.visitor`.
* 3. Generating code.
* 4. Calling `ctx.buildRecord(code, location)` to produce the module record.
*
* @module
*/
/**
* @import {ModuleAnalysisContext, AnalysisOptions} from './types/analyzer.js'
*/
import makeModulePlugins from './babel-plugin.js';
import { createSourceOptions } from './source-options.js';
import { buildFunctorSource, buildModuleRecord } from './functor.js';
/**
* Creates a fresh ESM analysis context for a single module parse.
*
* @param {AnalysisOptions} [options]
* @returns {ModuleAnalysisContext}
*/
export const analyzeModule = (options = {}) => {
const { allowHidden = false } = options;
const sourceOptions = createSourceOptions({ allowHidden });
const { analyzePlugin, transformPlugin } = makeModulePlugins(sourceOptions);
return {
analyzePass: analyzePlugin,
transformPass: transformPlugin,
buildRecord(generatedCode, location) {
const functorSource = buildFunctorSource(
generatedCode,
sourceOptions,
location,
);
return buildModuleRecord(sourceOptions, functorSource);
},
};
};