MCPcopy Index your code
hub / github.com/jquery/esprima / parse

Function parse

src/esprima.ts:30–80  ·  view source on GitHub ↗
(code: string, options, delegate)

Source from the content-addressed store, hash-verified

28import { Tokenizer } from './tokenizer';
29
30export function parse(code: string, options, delegate) {
31 let commentHandler: CommentHandler | null = null;
32 const proxyDelegate = (node, metadata) => {
33 if (delegate) {
34 delegate(node, metadata);
35 }
36 if (commentHandler) {
37 commentHandler.visit(node, metadata);
38 }
39 };
40
41 let parserDelegate = (typeof delegate === 'function') ? proxyDelegate : null;
42 let collectComment = false;
43 if (options) {
44 collectComment = (typeof options.comment === 'boolean' && options.comment);
45 const attachComment = (typeof options.attachComment === 'boolean' && options.attachComment);
46 if (collectComment || attachComment) {
47 commentHandler = new CommentHandler();
48 commentHandler.attach = attachComment;
49 options.comment = true;
50 parserDelegate = proxyDelegate;
51 }
52 }
53
54 let isModule = false;
55 if (options && typeof options.sourceType === 'string') {
56 isModule = (options.sourceType === 'module');
57 }
58
59 let parser: Parser;
60 if (options && typeof options.jsx === 'boolean' && options.jsx) {
61 parser = new JSXParser(code, options, parserDelegate);
62 } else {
63 parser = new Parser(code, options, parserDelegate);
64 }
65
66 const program = isModule ? parser.parseModule() : parser.parseScript();
67 const ast = program as any;
68
69 if (collectComment && commentHandler) {
70 ast.comments = commentHandler.comments;
71 }
72 if (parser.config.tokens) {
73 ast.tokens = parser.tokens;
74 }
75 if (parser.config.tolerant) {
76 ast.errors = parser.errorHandler.errors;
77 }
78
79 return ast;
80}
81
82export function parseModule(code: string, options, delegate) {
83 const parsingOptions = options || {};

Callers 2

parseModuleFunction · 0.70
parseScriptFunction · 0.70

Calls 2

parseModuleMethod · 0.80
parseScriptMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…