@valbuild/server 0.154.0 → 0.154.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -0
- package/browser/dist/valbuild-server-browser.cjs.dev.js +6 -3
- package/browser/dist/valbuild-server-browser.cjs.prod.js +6 -3
- package/browser/dist/valbuild-server-browser.esm.js +6 -3
- package/dist/{ValRouterCore-fe985e4c.esm.js → ValOpsMemory-611f33d2.esm.js} +44 -1988
- package/dist/{ValRouterCore-4be8eaf1.cjs.prod.js → ValOpsMemory-6ebbb3ba.cjs.prod.js} +53 -2009
- package/dist/{ValRouterCore-0c088c9e.cjs.dev.js → ValOpsMemory-d8bf9f5d.cjs.dev.js} +53 -2009
- package/dist/ValPatchStore-2f92eced.cjs.prod.js +1128 -0
- package/dist/ValPatchStore-df8cdbe7.cjs.dev.js +1128 -0
- package/dist/ValPatchStore-e23070df.esm.js +1119 -0
- package/dist/ValRouterCore-5b747abb.cjs.prod.js +612 -0
- package/dist/ValRouterCore-73eb843d.esm.js +608 -0
- package/dist/ValRouterCore-86757d56.cjs.dev.js +612 -0
- package/dist/createPrettierFormatter-05ad7480.cjs.prod.js +692 -0
- package/dist/createPrettierFormatter-1edca494.esm.js +673 -0
- package/dist/createPrettierFormatter-851fcf99.cjs.dev.js +692 -0
- package/dist/createValRuntime-2dc83070.cjs.prod.js +47 -0
- package/dist/createValRuntime-62836dbe.esm.js +45 -0
- package/dist/createValRuntime-fd9b673d.cjs.dev.js +47 -0
- package/dist/declarations/src/ValOpsMemory.d.ts +3 -73
- package/dist/declarations/src/ValPatchStore.d.ts +92 -0
- package/dist/declarations/src/ValRouter.d.ts +1 -11
- package/dist/declarations/src/createPrettierFormatter.d.ts +18 -3
- package/dist/declarations/src/createValRuntime.d.ts +12 -0
- package/dist/declarations/src/draftReads.d.ts +11 -3
- package/dist/declarations/src/index.d.ts +4 -2
- package/dist/declarations/src/internal/index.d.ts +28 -6
- package/dist/declarations/src/proposal.d.ts +13 -25
- package/dist/declarations/src/proposalJsonEntries.d.ts +30 -0
- package/dist/declarations/src/serverMode.d.ts +31 -0
- package/dist/{draftReads-0dcddf72.cjs.prod.js → draftReads-3f7dcda5.cjs.prod.js} +10 -2
- package/dist/{draftReads-df44e629.cjs.dev.js → draftReads-a300a02f.cjs.dev.js} +10 -2
- package/dist/{draftReads-1f29aedd.esm.js → draftReads-bca0d49c.esm.js} +10 -2
- package/dist/{inTabFetch-f6694ad2.cjs.prod.js → inTabFetch-0124da2e.cjs.prod.js} +5 -4
- package/dist/{inTabFetch-c8966e73.cjs.dev.js → inTabFetch-d0c9c58a.cjs.dev.js} +5 -4
- package/dist/{inTabFetch-877e7d11.esm.js → inTabFetch-e48a801e.esm.js} +2 -1
- package/dist/jsonValuesPatch-13acc674.esm.js +276 -0
- package/dist/jsonValuesPatch-88bc661d.cjs.dev.js +281 -0
- package/dist/jsonValuesPatch-92798812.cjs.prod.js +281 -0
- package/dist/proposalJsonEntries-0669f027.esm.js +480 -0
- package/dist/proposalJsonEntries-26f397bd.cjs.dev.js +500 -0
- package/dist/proposalJsonEntries-9e537244.cjs.prod.js +500 -0
- package/dist/{publishKit-cfe1273d.cjs.dev.js → publishKit-96522d5a.cjs.prod.js} +20 -22
- package/dist/{publishKit-e7f7db7c.cjs.prod.js → publishKit-b06031dc.cjs.dev.js} +20 -22
- package/dist/{publishKit-0709423c.esm.js → publishKit-b8929fe8.esm.js} +8 -9
- package/dist/{ValRouter-1f427c38.cjs.dev.js → valRuntime-ba8ff718.cjs.prod.js} +158 -6038
- package/dist/{ValRouter-cd6e854a.cjs.prod.js → valRuntime-c8c82b24.cjs.dev.js} +158 -6038
- package/dist/{ValRouter-558dc66d.esm.js → valRuntime-ccbbbfad.esm.js} +107 -5937
- package/dist/valbuild-server.cjs.dev.js +5146 -352
- package/dist/valbuild-server.cjs.prod.js +5146 -352
- package/dist/valbuild-server.esm.js +4902 -132
- package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +7 -4
- package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +7 -4
- package/internal/browser/dist/valbuild-server-internal-browser.esm.js +7 -4
- package/internal/dist/valbuild-server-internal.cjs.dev.js +47 -31
- package/internal/dist/valbuild-server-internal.cjs.prod.js +47 -31
- package/internal/dist/valbuild-server-internal.esm.js +38 -22
- package/package.json +4 -4
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import { result, pipe } from '@valbuild/core/fp';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import { r as resolveExistingJsonPath } from './jsonValuesPatch-13acc674.esm.js';
|
|
4
|
+
import { a as proposalSnapshot, i as isObject, d as isJsonValue } from './createPrettierFormatter-1edca494.esm.js';
|
|
5
|
+
|
|
6
|
+
class ValSyntaxError {
|
|
7
|
+
constructor(message, node) {
|
|
8
|
+
this.message = message;
|
|
9
|
+
this.node = node;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function forEachError(tree, callback) {
|
|
13
|
+
if (Array.isArray(tree)) {
|
|
14
|
+
for (const subtree of tree) {
|
|
15
|
+
forEachError(subtree, callback);
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
callback(tree);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function flatMapErrors(tree, cb) {
|
|
22
|
+
const result = [];
|
|
23
|
+
forEachError(tree, error => result.push(cb(error)));
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
function formatSyntaxError(error, sourceFile) {
|
|
27
|
+
const pos = sourceFile.getLineAndCharacterOfPosition(error.node.pos);
|
|
28
|
+
return `${pos.line}:${pos.character} ${error.message}`;
|
|
29
|
+
}
|
|
30
|
+
function formatSyntaxErrorTree(tree, sourceFile) {
|
|
31
|
+
return flatMapErrors(tree, error => formatSyntaxError(error, sourceFile));
|
|
32
|
+
}
|
|
33
|
+
function isLiteralPropertyName(name) {
|
|
34
|
+
return ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isNumericLiteral(name);
|
|
35
|
+
}
|
|
36
|
+
function validateObjectProperties(nodes) {
|
|
37
|
+
const errors = [];
|
|
38
|
+
for (const node of nodes) {
|
|
39
|
+
if (!ts.isPropertyAssignment(node)) {
|
|
40
|
+
errors.push(new ValSyntaxError("Object literal element must be property assignment", node));
|
|
41
|
+
} else if (!isLiteralPropertyName(node.name)) {
|
|
42
|
+
errors.push(new ValSyntaxError("Object literal element key must be an identifier or a literal", node));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (errors.length > 0) {
|
|
46
|
+
return result.err(errors);
|
|
47
|
+
}
|
|
48
|
+
return result.ok(nodes);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Validates that the expression is a JSON compatible type of expression without
|
|
53
|
+
* validating its children.
|
|
54
|
+
*/
|
|
55
|
+
function shallowValidateExpression(value) {
|
|
56
|
+
return ts.isStringLiteralLike(value) || ts.isNumericLiteral(value) || value.kind === ts.SyntaxKind.TrueKeyword || value.kind === ts.SyntaxKind.FalseKeyword || value.kind === ts.SyntaxKind.NullKeyword || ts.isArrayLiteralExpression(value) || ts.isObjectLiteralExpression(value) ? undefined : new ValSyntaxError("Expression must be a literal", value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Evaluates the expression as a JSON value
|
|
61
|
+
*/
|
|
62
|
+
function evaluateExpression(value) {
|
|
63
|
+
// The text property of a LiteralExpression stores the interpreted value of the literal in text form. For a StringLiteral,
|
|
64
|
+
// or any literal of a template, this means quotes have been removed and escapes have been converted to actual characters.
|
|
65
|
+
// For a NumericLiteral, the stored value is the toString() representation of the number. For example 1, 1.00, and 1e0 are all stored as just "1".
|
|
66
|
+
// https://github.com/microsoft/TypeScript/blob/4b794fe1dd0d184d3f8f17e94d8187eace57c91e/src/compiler/types.ts#L2127-L2131
|
|
67
|
+
|
|
68
|
+
if (ts.isStringLiteralLike(value)) {
|
|
69
|
+
return result.ok(value.text);
|
|
70
|
+
} else if (ts.isNumericLiteral(value)) {
|
|
71
|
+
return result.ok(Number(value.text));
|
|
72
|
+
} else if (value.kind === ts.SyntaxKind.TrueKeyword) {
|
|
73
|
+
return result.ok(true);
|
|
74
|
+
} else if (value.kind === ts.SyntaxKind.FalseKeyword) {
|
|
75
|
+
return result.ok(false);
|
|
76
|
+
} else if (value.kind === ts.SyntaxKind.NullKeyword) {
|
|
77
|
+
return result.ok(null);
|
|
78
|
+
} else if (ts.isArrayLiteralExpression(value)) {
|
|
79
|
+
return result.all(value.elements.map(evaluateExpression));
|
|
80
|
+
} else if (ts.isObjectLiteralExpression(value)) {
|
|
81
|
+
return pipe(validateObjectProperties(value.properties), result.flatMap(assignments => pipe(assignments.map(assignment => pipe(evaluateExpression(assignment.initializer), result.map(value => [assignment.name.text, value]))), result.all)), result.map(Object.fromEntries));
|
|
82
|
+
} else {
|
|
83
|
+
return result.err(new ValSyntaxError("Expression must be a literal", value));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Finds the property `key` is written as in an object literal, for an edit to
|
|
89
|
+
* write into.
|
|
90
|
+
*
|
|
91
|
+
* An object may spread another into itself (`{ ...noMedia, code: null }`): the
|
|
92
|
+
* module evaluates it, so the value reads fine, and an edit to a key written
|
|
93
|
+
* out AFTER every spread can go ahead - that property is the one that wins.
|
|
94
|
+
* What cannot is an edit to a key a spread might supply or override, since we
|
|
95
|
+
* would be writing a value the module does not end up with. Those are
|
|
96
|
+
* refused, naming the spread, so the author knows which line to write out.
|
|
97
|
+
*/
|
|
98
|
+
function findObjectPropertyAssignment(value, key) {
|
|
99
|
+
const spreads = value.properties.filter(ts.isSpreadAssignment);
|
|
100
|
+
return pipe(validateObjectProperties(value.properties.filter(property => !ts.isSpreadAssignment(property))), result.flatMap(assignments => {
|
|
101
|
+
const matchingAssignments = assignments.filter(assignment => assignment.name.text === key);
|
|
102
|
+
if (matchingAssignments.length > 1) {
|
|
103
|
+
return result.err(new ValSyntaxError(`Object key "${key}" is ambiguous`, value));
|
|
104
|
+
}
|
|
105
|
+
const [assignment] = matchingAssignments;
|
|
106
|
+
if (assignment === undefined) {
|
|
107
|
+
if (spreads.length > 0) {
|
|
108
|
+
return result.err(new ValSyntaxError(`Cannot edit "${key}": it is not written out in this object, so it may come from ${spreadNames(spreads)}. Write "${key}" out after the spread to make it editable`, spreads[spreads.length - 1]));
|
|
109
|
+
}
|
|
110
|
+
return result.ok(undefined);
|
|
111
|
+
}
|
|
112
|
+
const overriding = spreads.filter(spread => spread.pos > assignment.pos);
|
|
113
|
+
if (overriding.length > 0) {
|
|
114
|
+
return result.err(new ValSyntaxError(`Cannot edit "${key}": ${spreadNames(overriding)} comes after it and may override it. Move "${key}" after the spread to make it editable`, assignment));
|
|
115
|
+
}
|
|
116
|
+
return result.ok(assignment);
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Finds the property `key` is written as, for a REMOVE: stricter than
|
|
122
|
+
* {@link findObjectPropertyAssignment}, because removing a key from an object
|
|
123
|
+
* that spreads another into it is refused outright. The key's own property may
|
|
124
|
+
* be all that hides a value the spread supplies, and removing it would bring
|
|
125
|
+
* that value back - and for a `.jsonValues()` entry, delete its file as well.
|
|
126
|
+
*
|
|
127
|
+
* Every remove goes through this, so the two that exist (a value, and a
|
|
128
|
+
* `.jsonValues()` entry) cannot disagree about it.
|
|
129
|
+
*/
|
|
130
|
+
function findObjectPropertyAssignmentToRemove(value, key) {
|
|
131
|
+
const spreads = value.properties.filter(ts.isSpreadAssignment);
|
|
132
|
+
if (spreads.length > 0) {
|
|
133
|
+
return result.err(new ValSyntaxError(`Cannot remove "${key}" from an object that spreads another into it: ${spreadNames(spreads)} may have a "${key}" of its own, and removing this one would bring it back`, value));
|
|
134
|
+
}
|
|
135
|
+
return findObjectPropertyAssignment(value, key);
|
|
136
|
+
}
|
|
137
|
+
function spreadNames(spreads) {
|
|
138
|
+
// Not `getText()`: it needs parent pointers, and the parsers here do not set them.
|
|
139
|
+
return spreads.map(spread => ts.isIdentifier(spread.expression) ? `"...${spread.expression.text}"` : "a spread").join(", ");
|
|
140
|
+
}
|
|
141
|
+
function validateInitializers(nodes) {
|
|
142
|
+
for (const node of nodes) {
|
|
143
|
+
if (ts.isSpreadElement(node)) {
|
|
144
|
+
return new ValSyntaxError("Unexpected spread element", node);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isPath(node, path) {
|
|
151
|
+
let currentNode = node;
|
|
152
|
+
for (let i = path.length - 1; i > 0; --i) {
|
|
153
|
+
const name = path[i];
|
|
154
|
+
if (!ts.isPropertyAccessExpression(currentNode)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (!ts.isIdentifier(currentNode.name) || currentNode.name.text !== name) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
currentNode = currentNode.expression;
|
|
161
|
+
}
|
|
162
|
+
return ts.isIdentifier(currentNode) && currentNode.text === path[0];
|
|
163
|
+
}
|
|
164
|
+
function validateArguments(node, validators) {
|
|
165
|
+
return result.allV([node.arguments.length === validators.length ? result.voidOk : result.err(new ValSyntaxError(`Expected ${validators.length} arguments`, node)), ...node.arguments.slice(0, validators.length).map((argument, index) => validators[index](argument))]);
|
|
166
|
+
}
|
|
167
|
+
function analyzeDefaultExport(node) {
|
|
168
|
+
const cDefine = node.expression;
|
|
169
|
+
if (!ts.isCallExpression(cDefine)) {
|
|
170
|
+
return result.err(new ValSyntaxError("Expected default expression to be a call expression", cDefine));
|
|
171
|
+
}
|
|
172
|
+
if (!isPath(cDefine.expression, ["c", "define"])) {
|
|
173
|
+
return result.err(new ValSyntaxError("Expected default expression to be calling c.define", cDefine.expression));
|
|
174
|
+
}
|
|
175
|
+
return pipe(validateArguments(cDefine, [id => {
|
|
176
|
+
// TODO: validate ID value here?
|
|
177
|
+
if (!ts.isStringLiteralLike(id)) {
|
|
178
|
+
return result.err(new ValSyntaxError("Expected first argument to c.define to be a string literal", id));
|
|
179
|
+
}
|
|
180
|
+
return result.voidOk;
|
|
181
|
+
}, () => {
|
|
182
|
+
return result.voidOk;
|
|
183
|
+
}, () => {
|
|
184
|
+
return result.voidOk;
|
|
185
|
+
}]), result.map(() => {
|
|
186
|
+
const [, schema, source] = cDefine.arguments;
|
|
187
|
+
return {
|
|
188
|
+
schema,
|
|
189
|
+
source
|
|
190
|
+
};
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
193
|
+
function analyzeValModule(sourceFile) {
|
|
194
|
+
const analysis = sourceFile.forEachChild(node => {
|
|
195
|
+
if (ts.isExportAssignment(node)) {
|
|
196
|
+
return analyzeDefaultExport(node);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
if (!analysis) {
|
|
200
|
+
throw Error("Failed to find fixed content node in val module");
|
|
201
|
+
}
|
|
202
|
+
return analysis;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Given the `source` object-literal expression of a `.jsonValues()` record /
|
|
207
|
+
* router (the 3rd argument to `c.define`, via {@link analyzeValModule}),
|
|
208
|
+
* extracts each entry's `c.json(() => import("..."))` import path, keyed by
|
|
209
|
+
* entry key. Entries whose value is not a `c.json(...)` call are skipped.
|
|
210
|
+
*/
|
|
211
|
+
function analyzeJsonValuesEntries(source) {
|
|
212
|
+
const entries = new Map();
|
|
213
|
+
if (!ts.isObjectLiteralExpression(source)) {
|
|
214
|
+
return entries;
|
|
215
|
+
}
|
|
216
|
+
for (const prop of source.properties) {
|
|
217
|
+
if (!ts.isPropertyAssignment(prop)) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const key = getPropertyKey(prop.name);
|
|
221
|
+
if (key === null) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const entry = analyzeCJsonCall(prop.initializer);
|
|
225
|
+
if (entry) {
|
|
226
|
+
entries.set(key, entry);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return entries;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Every key of a `.jsonValues()` record's source literal, thunked or not.
|
|
234
|
+
*
|
|
235
|
+
* {@link analyzeJsonValuesEntries} answers "which entries have a file behind
|
|
236
|
+
* them", which is the question for resolving a path. This is the other one:
|
|
237
|
+
* which keys the record HAS. They differ on a value that is not a
|
|
238
|
+
* `c.json(...)` call - written inline, or mid-edit - and telling an existing
|
|
239
|
+
* key from a new one is what decides whether a whole-record write adds a
|
|
240
|
+
* `c.json(...)` thunk or replaces one. Using the thunk set for that would
|
|
241
|
+
* insert a second property with a key the literal already has.
|
|
242
|
+
*/
|
|
243
|
+
function jsonValuesRecordKeys(source) {
|
|
244
|
+
const keys = [];
|
|
245
|
+
if (!ts.isObjectLiteralExpression(source)) {
|
|
246
|
+
return keys;
|
|
247
|
+
}
|
|
248
|
+
for (const prop of source.properties) {
|
|
249
|
+
if (!ts.isPropertyAssignment(prop)) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const key = getPropertyKey(prop.name);
|
|
253
|
+
if (key !== null) {
|
|
254
|
+
keys.push(key);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return keys;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The Source a `c.define(...)` literal declares, as a module's Source is
|
|
262
|
+
* stored: each `.jsonValues()` entry's `c.json(() => import("..."))` as the
|
|
263
|
+
* marker `{ _type: "json" }` its content stands behind, and every other value
|
|
264
|
+
* as written. An error for anything that is not a literal.
|
|
265
|
+
*
|
|
266
|
+
* What a staging records for a module (`stagedModuleSources`): without the
|
|
267
|
+
* markers, an entry the assistant added to a record was in the module's code
|
|
268
|
+
* and not in its Source, and an edit into it was undone.
|
|
269
|
+
*/
|
|
270
|
+
function evaluateModuleSource(source) {
|
|
271
|
+
const entries = analyzeJsonValuesEntries(source);
|
|
272
|
+
if (entries.size === 0 || !ts.isObjectLiteralExpression(source)) {
|
|
273
|
+
return evaluateExpression(source);
|
|
274
|
+
}
|
|
275
|
+
const value = {};
|
|
276
|
+
for (const prop of source.properties) {
|
|
277
|
+
if (!ts.isPropertyAssignment(prop)) {
|
|
278
|
+
return result.err(new ValSyntaxError("Expected a property assignment", prop));
|
|
279
|
+
}
|
|
280
|
+
const key = getPropertyKey(prop.name);
|
|
281
|
+
if (key === null) {
|
|
282
|
+
return result.err(new ValSyntaxError("Expected a literal key", prop.name));
|
|
283
|
+
}
|
|
284
|
+
if (entries.has(key)) {
|
|
285
|
+
value[key] = {
|
|
286
|
+
_type: "json"
|
|
287
|
+
};
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const evaluated = evaluateExpression(prop.initializer);
|
|
291
|
+
if (result.isErr(evaluated)) return evaluated;
|
|
292
|
+
value[key] = evaluated.value;
|
|
293
|
+
}
|
|
294
|
+
return result.ok(value);
|
|
295
|
+
}
|
|
296
|
+
function getPropertyKey(name) {
|
|
297
|
+
if (ts.isStringLiteralLike(name)) {
|
|
298
|
+
return name.text;
|
|
299
|
+
}
|
|
300
|
+
if (ts.isIdentifier(name)) {
|
|
301
|
+
return name.text;
|
|
302
|
+
}
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
function analyzeCJsonCall(node) {
|
|
306
|
+
// c.json(() => import("path"))
|
|
307
|
+
if (!ts.isCallExpression(node) || !isCJson(node.expression)) {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
const [thunk] = node.arguments;
|
|
311
|
+
if (!thunk) {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
const importPath = getImportPathFromThunk(thunk);
|
|
315
|
+
if (importPath === null) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
importPath
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
function isCJson(expr) {
|
|
323
|
+
// c.json
|
|
324
|
+
return ts.isPropertyAccessExpression(expr) && ts.isIdentifier(expr.expression) && expr.expression.text === "c" && ts.isIdentifier(expr.name) && expr.name.text === "json";
|
|
325
|
+
}
|
|
326
|
+
function getImportPathFromThunk(thunk) {
|
|
327
|
+
// () => import("path") (concise body) OR () => { return import("path"); }
|
|
328
|
+
if (!ts.isArrowFunction(thunk)) {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
let importCall;
|
|
332
|
+
if (ts.isCallExpression(thunk.body)) {
|
|
333
|
+
importCall = thunk.body;
|
|
334
|
+
} else if (ts.isBlock(thunk.body)) {
|
|
335
|
+
for (const stmt of thunk.body.statements) {
|
|
336
|
+
if (ts.isReturnStatement(stmt) && stmt.expression && ts.isCallExpression(stmt.expression)) {
|
|
337
|
+
importCall = stmt.expression;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (!importCall || !ts.isCallExpression(importCall)) {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
const isImport = importCall.expression.kind === ts.SyntaxKind.ImportKeyword || ts.isIdentifier(importCall.expression) && importCall.expression.text === "import";
|
|
346
|
+
if (!isImport) {
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
const arg = importCall.arguments[0];
|
|
350
|
+
if (arg && ts.isStringLiteralLike(arg)) {
|
|
351
|
+
return arg.text;
|
|
352
|
+
}
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/*
|
|
357
|
+
* The half of a proposal's snapshot that needs the compiler: which
|
|
358
|
+
* `*.val.json` a saved entry is, read from its module's `.val.ts`. Apart from
|
|
359
|
+
* `./proposal` so that a reader serving published content -- which never has
|
|
360
|
+
* a saved entry -- does not evaluate TypeScript to start
|
|
361
|
+
* (`readerSnapshot` loads this only when there is one).
|
|
362
|
+
*/
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* The `.jsonValues()` entries the proposal saved, by module and key.
|
|
366
|
+
*
|
|
367
|
+
* An entry's content is not in its module's Source -- that holds a marker per
|
|
368
|
+
* entry -- but in the entry's own `*.val.json`, and a save writes that file:
|
|
369
|
+
* it reaches the proposal's address as saved TEXT ({@link ValProposal.files}),
|
|
370
|
+
* not as Source. So the snapshot alone shows the build's entries. This is the
|
|
371
|
+
* other half: each saved `*.val.json`, read as the entry its module names.
|
|
372
|
+
*
|
|
373
|
+
* Which entry a file is comes from the module's `.val.ts`, whose
|
|
374
|
+
* `c.json(() => import("..."))` thunks name the files -- the saved text if
|
|
375
|
+
* the save rewrote it, else the build's own (`projectSource`). An entry whose
|
|
376
|
+
* module cannot be read that way is left to the build, as before: this adds
|
|
377
|
+
* what it can find and never fails the address.
|
|
378
|
+
*/
|
|
379
|
+
function proposalJsonEntries(proposal, projectSource) {
|
|
380
|
+
const found = new Map();
|
|
381
|
+
const savedJson = Object.keys(proposal.files).filter(path => path.endsWith(".val.json"));
|
|
382
|
+
if (savedJson.length === 0) return found;
|
|
383
|
+
const savedJsonPaths = new Set(savedJson);
|
|
384
|
+
for (const [moduleFilePath, source] of Object.entries(proposal.modules)) {
|
|
385
|
+
if (!holdsJsonMarkers(source)) continue;
|
|
386
|
+
const relative = moduleFilePath.replace(/^\//, "");
|
|
387
|
+
const text = proposal.files[relative] ?? (projectSource === null || projectSource === void 0 ? void 0 : projectSource[relative]);
|
|
388
|
+
if (text === undefined) continue;
|
|
389
|
+
const entries = jsonEntryPaths(relative, text);
|
|
390
|
+
if (entries === null) continue;
|
|
391
|
+
const byKey = new Map();
|
|
392
|
+
for (const [key, importPath] of entries) {
|
|
393
|
+
const jsonPath = resolveExistingJsonPath(moduleFilePath, importPath).replace(/^\//, "");
|
|
394
|
+
if (!savedJsonPaths.has(jsonPath)) continue;
|
|
395
|
+
const content = parseJson(proposal.files[jsonPath]);
|
|
396
|
+
if (content !== undefined) byKey.set(key, content);
|
|
397
|
+
}
|
|
398
|
+
if (byKey.size > 0) found.set(moduleFilePath, byKey);
|
|
399
|
+
}
|
|
400
|
+
return found;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The snapshot as a page reads it: {@link proposalSnapshot}, with each saved
|
|
405
|
+
* `.jsonValues()` entry's content in place of its marker -- the shape a draft
|
|
406
|
+
* already has, where the entries it edits hold content and the rest are
|
|
407
|
+
* markers the readers resolve from the build. See {@link proposalJsonEntries}.
|
|
408
|
+
*
|
|
409
|
+
* For the readers only. Val's own server keeps the markers
|
|
410
|
+
* ({@link proposalSnapshot}): its patching works on them, and it takes the
|
|
411
|
+
* saved entries separately.
|
|
412
|
+
*/
|
|
413
|
+
function proposalSnapshotForReaders(proposal, projectSource) {
|
|
414
|
+
const snapshot = proposalSnapshot(proposal);
|
|
415
|
+
for (const [moduleFilePath, entries] of proposalJsonEntries(proposal, projectSource)) {
|
|
416
|
+
const source = snapshot[moduleFilePath];
|
|
417
|
+
if (!isObject(source)) continue;
|
|
418
|
+
const filled = {};
|
|
419
|
+
for (const [key, value] of Object.entries(source)) {
|
|
420
|
+
if (isJsonValue(value)) filled[key] = value;
|
|
421
|
+
}
|
|
422
|
+
for (const [key, content] of entries) {
|
|
423
|
+
// Own keys only: an entry may be named `toString`.
|
|
424
|
+
if (Object.prototype.hasOwnProperty.call(filled, key)) {
|
|
425
|
+
filled[key] = content;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
snapshot[moduleFilePath] = filled;
|
|
429
|
+
}
|
|
430
|
+
return snapshot;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** A record of `.jsonValues()` markers (`{ _type: "json" }`), at least one. */
|
|
434
|
+
function holdsJsonMarkers(source) {
|
|
435
|
+
return isObject(source) && Object.values(source).some(value => isObject(value) && value["_type"] === "json");
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** Each entry key's import path, from a `.jsonValues()` module's text. */
|
|
439
|
+
function jsonEntryPaths(fileName, text) {
|
|
440
|
+
try {
|
|
441
|
+
const analysis = analyzeValModule(ts.createSourceFile(fileName, text, ts.ScriptTarget.ES2015, true));
|
|
442
|
+
if (result.isErr(analysis)) return null;
|
|
443
|
+
const paths = new Map();
|
|
444
|
+
for (const [key, entry] of analyzeJsonValuesEntries(analysis.value.source)) {
|
|
445
|
+
paths.set(key, entry.importPath);
|
|
446
|
+
}
|
|
447
|
+
return paths;
|
|
448
|
+
} catch {
|
|
449
|
+
// Not a module this can read: its entries stay the build's.
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function parseJson(text) {
|
|
454
|
+
if (text === undefined) return undefined;
|
|
455
|
+
try {
|
|
456
|
+
const value = JSON.parse(text);
|
|
457
|
+
return isMutableJsonValue(value) ? value : undefined;
|
|
458
|
+
} catch {
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** {@link isJsonValue}, for the mutable JSON a server patches. */
|
|
464
|
+
function isMutableJsonValue(value) {
|
|
465
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
if (Array.isArray(value)) {
|
|
469
|
+
return value.every(isMutableJsonValue);
|
|
470
|
+
}
|
|
471
|
+
return isObject(value) && Object.values(value).every(isMutableJsonValue);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
var proposalJsonEntries$1 = /*#__PURE__*/Object.freeze({
|
|
475
|
+
__proto__: null,
|
|
476
|
+
proposalJsonEntries: proposalJsonEntries,
|
|
477
|
+
proposalSnapshotForReaders: proposalSnapshotForReaders
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
export { analyzeValModule as a, formatSyntaxError as b, analyzeJsonValuesEntries as c, formatSyntaxErrorTree as d, findObjectPropertyAssignment as e, flatMapErrors as f, findObjectPropertyAssignmentToRemove as g, evaluateExpression as h, proposalJsonEntries as i, evaluateModuleSource as j, jsonValuesRecordKeys as k, proposalJsonEntries$1 as l, proposalSnapshotForReaders as p, shallowValidateExpression as s, validateInitializers as v };
|