-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathjavascript.ts
More file actions
163 lines (151 loc) · 6.11 KB
/
javascript.ts
File metadata and controls
163 lines (151 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {type Identifier, type Node, type Options, Parser, tokTypes} from "acorn";
import {fileReference} from "./files.js";
import {findAssignments} from "./javascript/assignments.js";
import {findAwaits} from "./javascript/awaits.js";
import {findDeclarations} from "./javascript/declarations.js";
import {findFeatures} from "./javascript/features.js";
import {rewriteFetches} from "./javascript/fetches.js";
import {defaultGlobals} from "./javascript/globals.js";
import {createImportResolver, findExports, findImports, rewriteImports} from "./javascript/imports.js";
import {findReferences} from "./javascript/references.js";
import {syntaxError} from "./javascript/syntaxError.js";
import {Sourcemap} from "./sourcemap.js";
export interface DatabaseReference {
name: string;
}
export interface FileReference {
name: string;
mimeType: string | null;
/** The relative path from the document to the file */
path: string;
}
export interface ImportReference {
name: string;
type: "global" | "local";
}
export interface Feature {
type: "FileAttachment" | "DatabaseClient" | "Secret";
name: string;
}
export interface Transpile {
id: string;
inputs?: string[];
outputs?: string[];
inline?: boolean;
body: string;
databases?: DatabaseReference[];
files?: FileReference[];
imports?: ImportReference[];
}
export interface ParseOptions {
id: string;
root: string;
sourcePath: string;
inline?: boolean;
sourceLine?: number;
globals?: Set<string>;
verbose?: boolean;
}
export function transpileJavaScript(input: string, options: ParseOptions): Transpile {
const {id, root, sourcePath, verbose = true} = options;
try {
const node = parseJavaScript(input, options);
const databases = node.features
.filter((f) => f.type === "DatabaseClient")
.map((f): DatabaseReference => ({name: f.name}));
const files = node.features
.filter((f) => f.type === "FileAttachment")
.map(({name}) => fileReference(name, sourcePath));
const inputs = Array.from(new Set<string>(node.references.map((r) => r.name)));
const output = new Sourcemap(input);
trim(output, input);
if (node.expression && !inputs.includes("display") && !inputs.includes("view")) {
output.insertLeft(0, "display((\n");
output.insertRight(input.length, "\n))");
inputs.push("display");
}
rewriteImports(output, node, sourcePath, createImportResolver(root, "_import"));
rewriteFetches(output, node, sourcePath);
return {
id,
...(inputs.length ? {inputs} : null),
...(options.inline ? {inline: true} : null),
...(node.declarations?.length ? {outputs: node.declarations.map(({name}) => name)} : null),
...(databases.length ? {databases} : null),
...(files.length ? {files} : null),
body: `${node.async ? "async " : ""}(${inputs}) => {
${String(output)}${node.declarations?.length ? `\nreturn {${node.declarations.map(({name}) => name)}};` : ""}
}`,
...(node.imports.length ? {imports: node.imports} : null)
};
} catch (error) {
if (!(error instanceof SyntaxError)) throw error;
let message = error.message;
const match = /^(.+)\s\((\d+):(\d+)\)$/.exec(message);
if (match) {
const line = +match[2] + (options?.sourceLine ?? 0);
const column = +match[3] + 1;
message = `${match[1]} at line ${line}, column ${column}`;
} else if (options?.sourceLine) {
message = `${message} at line ${options.sourceLine + 1}`;
}
// TODO: Consider showing a code snippet along with the error. Also, consider
// whether we want to show the file name here.
if (verbose) console.error(`${error.name}: ${message}`);
return {
id: `${id}`,
body: `() => { throw new SyntaxError(${JSON.stringify(error.message)}); }`
};
}
}
function trim(output: Sourcemap, input: string): void {
if (input.startsWith("\n")) output.delete(0, 1); // TODO better trim
if (input.endsWith("\n")) output.delete(input.length - 1, input.length); // TODO better trim
}
export const parseOptions: Options = {ecmaVersion: 13, sourceType: "module"};
export interface JavaScriptNode {
body: Node;
declarations: Identifier[] | null; // null for expressions that can’t declare top-level variables, a.k.a outputs
references: Identifier[]; // the unbound references, a.k.a. inputs
features: Feature[];
imports: ImportReference[];
expression: boolean; // is this an expression or a program cell?
async: boolean; // does this use top-level await?
}
function parseJavaScript(input: string, options: ParseOptions): JavaScriptNode {
const {globals = defaultGlobals, inline = false, root, sourcePath} = options;
// First attempt to parse as an expression; if this fails, parse as a program.
let expression = maybeParseExpression(input, parseOptions);
if (expression?.type === "ClassExpression" && expression.id) expression = null; // treat named class as program
if (expression?.type === "FunctionExpression" && expression.id) expression = null; // treat named function as program
if (!expression && inline) throw new SyntaxError("invalid expression");
const body = expression ?? (Parser.parse(input, parseOptions) as any);
const exports = findExports(body);
if (exports.length) throw syntaxError("Unexpected token 'export'", exports[0], input); // disallow exports
const references = findReferences(body, globals);
findAssignments(body, references, globals, input);
const declarations = expression ? null : findDeclarations(body, globals, input);
const imports = findImports(body, root, sourcePath);
const features = findFeatures(body, root, sourcePath, references, input);
return {
body,
declarations,
references,
features,
imports,
expression: !!expression,
async: findAwaits(body).length > 0
};
}
// Parses a single expression; like parseExpressionAt, but returns null if
// additional input follows the expression.
function maybeParseExpression(input, options) {
const parser = new (Parser as any)(options, input, 0); // private constructor
parser.nextToken();
try {
const node = (parser as any).parseExpression();
return parser.type === tokTypes.eof ? node : null;
} catch {
return null;
}
}