1 | var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2 | if (kind === "m") throw new TypeError("Private method is not writable");
|
3 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5 | return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6 | };
|
7 | var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10 | return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11 | };
|
12 | var _FileState_importer;
|
13 | import babelTraverse, { NodePath } from '@babel/traverse';
|
14 | import babelParse from './babelParser.js';
|
15 |
|
16 | const traverse = babelTraverse.default ?? babelTraverse;
|
17 | class FileState {
|
18 | constructor(options, { code, ast, importer }) {
|
19 | _FileState_importer.set(this, void 0);
|
20 | this.hub = {
|
21 |
|
22 | file: this,
|
23 | parse: this.parse.bind(this),
|
24 | import: this.import.bind(this),
|
25 | getCode: () => this.code,
|
26 | getScope: () => this.scope,
|
27 | addHelper: () => undefined,
|
28 | buildError: (node, msg, Error) => {
|
29 | const err = new Error(msg);
|
30 | err.node = node;
|
31 | return err;
|
32 | },
|
33 | };
|
34 | this.opts = options;
|
35 | this.code = code;
|
36 | this.ast = ast;
|
37 | __classPrivateFieldSet(this, _FileState_importer, importer, "f");
|
38 | this.path = NodePath.get({
|
39 | hub: this.hub,
|
40 | parentPath: null,
|
41 | parent: this.ast,
|
42 | container: this.ast,
|
43 | key: 'program',
|
44 | }).setContext();
|
45 | this.scope = this.path.scope;
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | import(path, name) {
|
51 | return __classPrivateFieldGet(this, _FileState_importer, "f").call(this, path, name, this);
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | parse(code, filename) {
|
59 | const newOptions = { ...this.opts, filename };
|
60 |
|
61 |
|
62 | const ast = babelParse(code, newOptions);
|
63 | return new FileState(newOptions, {
|
64 | ast,
|
65 | code,
|
66 | importer: __classPrivateFieldGet(this, _FileState_importer, "f"),
|
67 | });
|
68 | }
|
69 | |
70 |
|
71 |
|
72 | traverse(visitors, state) {
|
73 | traverse(this.ast, visitors, this.scope, state);
|
74 | }
|
75 | }
|
76 | _FileState_importer = new WeakMap();
|
77 | export default FileState;
|