(input: string, options: ParseOptions)
| 42 | * the specified inline JavaScript expression. |
| 43 | */ |
| 44 | export function parseJavaScript(input: string, options: ParseOptions): JavaScriptNode { |
| 45 | const {inline = false, path, params} = options; |
| 46 | let expression = maybeParseExpression(input); // first attempt to parse as expression |
| 47 | if (expression?.type === "ClassExpression" && expression.id) expression = null; // treat named class as program |
| 48 | if (expression?.type === "FunctionExpression" && expression.id) expression = null; // treat named function as program |
| 49 | if (!expression && inline) throw new SyntaxError("invalid expression"); // inline code must be an expression |
| 50 | const body = expression ?? parseProgram(input); // otherwise parse as a program |
| 51 | const exports = findExports(body); |
| 52 | if (exports.length) throw syntaxError("Unexpected token 'export'", exports[0], input); // disallow exports |
| 53 | const references = findReferences(body); |
| 54 | if (params) checkParams(body, input, params); |
| 55 | checkAssignments(body, references, input); |
| 56 | return { |
| 57 | body, |
| 58 | declarations: expression ? null : findDeclarations(body as Program, input), |
| 59 | references, |
| 60 | files: findFiles(body, path, input, ["FileAttachment"]), |
| 61 | imports: findImports(body, path, input), |
| 62 | expression: !!expression, |
| 63 | async: findAwaits(body).length > 0, |
| 64 | input |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | export function parseProgram(input: string, params?: Params): Program { |
| 69 | const body = Parser.parse(input, acornOptions); |
no test coverage detected