UNPKG

2.44 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, parseSettings, 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 allowInvalidAST: parseSettings.allowInvalidAST,
22 errorOnUnknownASTType: parseSettings.errorOnUnknownASTType,
23 shouldPreserveNodeMaps,
24 suppressDeprecatedPropertyWarnings: parseSettings.suppressDeprecatedPropertyWarnings,
25 });
26 const estree = instance.convertProgram();
27 /**
28 * Optionally remove range and loc if specified
29 */
30 if (!parseSettings.range || !parseSettings.loc) {
31 (0, simple_traverse_1.simpleTraverse)(estree, {
32 enter: node => {
33 if (!parseSettings.range) {
34 // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- TS 4.0 made this an error because the types aren't optional
35 // @ts-expect-error
36 delete node.range;
37 }
38 if (!parseSettings.loc) {
39 // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- TS 4.0 made this an error because the types aren't optional
40 // @ts-expect-error
41 delete node.loc;
42 }
43 },
44 });
45 }
46 /**
47 * Optionally convert and include all tokens in the AST
48 */
49 if (parseSettings.tokens) {
50 estree.tokens = (0, node_utils_1.convertTokens)(ast);
51 }
52 /**
53 * Optionally convert and include all comments in the AST
54 */
55 if (parseSettings.comment) {
56 estree.comments = (0, convert_comments_1.convertComments)(ast, parseSettings.codeFullText);
57 }
58 const astMaps = instance.getASTMaps();
59 return { estree, astMaps };
60}
61exports.astConverter = astConverter;
62//# sourceMappingURL=ast-converter.js.map
\No newline at end of file