UNPKG

2.21 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.astConverter = void 0;
4const convert_1 = require("./convert");
5const convert_comments_1 = require("./convert-comments");
6const node_utils_1 = require("./node-utils");
7const simple_traverse_1 = require("./simple-traverse");
8function astConverter(ast, extra, shouldPreserveNodeMaps) {
9 /**
10 * The TypeScript compiler produced fundamental parse errors when parsing the
11 * source.
12 */
13 const { parseDiagnostics } = ast;
14 if (parseDiagnostics.length) {
15 throw (0, convert_1.convertError)(parseDiagnostics[0]);
16 }
17 /**
18 * Recursively convert the TypeScript AST into an ESTree-compatible AST
19 */
20 const instance = new convert_1.Converter(ast, {
21 errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
22 shouldPreserveNodeMaps,
23 });
24 const estree = instance.convertProgram();
25 /**
26 * Optionally remove range and loc if specified
27 */
28 if (!extra.range || !extra.loc) {
29 (0, simple_traverse_1.simpleTraverse)(estree, {
30 enter: node => {
31 if (!extra.range) {
32 // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- TS 4.0 made this an error because the types aren't optional
33 // @ts-expect-error
34 delete node.range;
35 }
36 if (!extra.loc) {
37 // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- TS 4.0 made this an error because the types aren't optional
38 // @ts-expect-error
39 delete node.loc;
40 }
41 },
42 });
43 }
44 /**
45 * Optionally convert and include all tokens in the AST
46 */
47 if (extra.tokens) {
48 estree.tokens = (0, node_utils_1.convertTokens)(ast);
49 }
50 /**
51 * Optionally convert and include all comments in the AST
52 */
53 if (extra.comment) {
54 estree.comments = (0, convert_comments_1.convertComments)(ast, extra.code);
55 }
56 const astMaps = instance.getASTMaps();
57 return { estree, astMaps };
58}
59exports.astConverter = astConverter;
60//# sourceMappingURL=ast-converter.js.map
\No newline at end of file