MCPcopy Create free account
hub / github.com/observablehq/framework / parseJavaScript

Function parseJavaScript

src/javascript/parse.ts:44–66  ·  view source on GitHub ↗
(input: string, options: ParseOptions)

Source from the content-addressed store, hash-verified

42 * the specified inline JavaScript expression.
43 */
44export 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
68export function parseProgram(input: string, params?: Params): Program {
69 const body = Parser.parse(input, acornOptions);

Callers 5

runTestsFunction · 0.85
runTestsFunction · 0.85
transpile-test.tsFile · 0.85
makeFenceRendererFunction · 0.85
makePlaceholderRendererFunction · 0.85

Calls 11

maybeParseExpressionFunction · 0.85
parseProgramFunction · 0.85
findExportsFunction · 0.85
syntaxErrorFunction · 0.85
findReferencesFunction · 0.85
checkParamsFunction · 0.85
checkAssignmentsFunction · 0.85
findDeclarationsFunction · 0.85
findImportsFunction · 0.85
findAwaitsFunction · 0.85
findFilesFunction · 0.70

Tested by

no test coverage detected