(code: string, options, delegate)
| 28 | import { Tokenizer } from './tokenizer'; |
| 29 | |
| 30 | export 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 | |
| 82 | export function parseModule(code: string, options, delegate) { |
| 83 | const parsingOptions = options || {}; |
no test coverage detected
searching dependent graphs…