-
Notifications
You must be signed in to change notification settings - Fork 122
/
javascript.ts
139 lines (129 loc) · 5.01 KB
/
javascript.ts
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
import {Parser, tokTypes, type Options} from "acorn";
import mime from "mime";
import {join} from "node:path";
import {canReadSync} from "./files.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 {findImports, rewriteImports} from "./javascript/imports.js";
import {findReferences} from "./javascript/references.js";
import {Sourcemap} from "./sourcemap.js";
export interface DatabaseReference {
name: string;
}
export interface FileReference {
name: string;
mimeType: string | null;
}
export interface ImportReference {
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;
inline?: boolean;
sourceLine?: number;
globals?: Set<string>;
}
export function transpileJavaScript(input: string, options: ParseOptions): Transpile {
const {root, id} = options;
try {
const node = parseJavaScript(input, options);
const databases = node.features.filter((f) => f.type === "DatabaseClient").map((f) => ({name: f.name}));
const files = node.features
.filter((f) => f.type === "FileAttachment")
.filter((f) => canReadSync(join(root, f.name)))
.map((f) => ({name: f.name, mimeType: mime.getType(f.name)}));
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);
rewriteFetches(output, node);
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.
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 function parseJavaScript(input: string, options: ParseOptions) {
const {globals = defaultGlobals, inline = false, root} = 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 references = findReferences(body, globals, input);
const declarations = expression ? null : findDeclarations(body, globals, input);
const features = findFeatures(body, references, input);
const imports = findImports(body, root);
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(options, input, 0);
parser.nextToken();
try {
const node = (parser as any).parseExpression();
return parser.type === tokTypes.eof ? node : null;
} catch {
return null;
}
}