@kronor/dtv 7.7.2 → 8.0.0
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/README.md +2 -0
- package/dist/cli/typegen/runTypegen.js +169 -74
- package/dist/index.es.js +3750 -3765
- package/dist/types/dsl/filterControl.d.ts +125 -33
- package/dist/types/dsl/filterExpr.d.ts +88 -139
- package/dist/types/dsl/filterExprForRow.d.ts +111 -0
- package/dist/types/dsl/filterTransform.d.ts +1 -1
- package/dist/types/dsl/filterTyping.d.ts +228 -0
- package/dist/types/dsl/filters.d.ts +41 -19
- package/dist/types/dsl/index.d.ts +2 -0
- package/dist/types/framework/data.d.ts +1 -1
- package/dist/types/framework/filters.d.ts +87 -18
- package/dist/types/framework/graphql/query.d.ts +3 -3
- package/dist/types/framework/view-parser.d.ts +11 -0
- package/dist/types/lib/index.d.ts +2 -1
- package/docs/api/README.md +1 -0
- package/docs/api/row-typed-views.md +315 -0
- package/docs/typegen.md +35 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -136,6 +136,8 @@ npm run test
|
|
|
136
136
|
DTV ships a CLI command for generating TypeScript types from a Hasura GraphQL schema:
|
|
137
137
|
|
|
138
138
|
- See `docs/typegen.md`
|
|
139
|
+
- Once a view has its generated row type, its columns and filters are checked against it —
|
|
140
|
+
see `docs/api/row-typed-views.md`
|
|
139
141
|
|
|
140
142
|
## Release Process
|
|
141
143
|
|
|
@@ -14,22 +14,6 @@ function applyFileNamePattern(pattern, view) {
|
|
|
14
14
|
.replace(/\{rootFieldName\}/g, view.rootFieldName)
|
|
15
15
|
.replace(/\{collectionName\}/g, view.rootFieldName);
|
|
16
16
|
}
|
|
17
|
-
function getColumnDefinitionsArray(argObject) {
|
|
18
|
-
for (const p of argObject.properties) {
|
|
19
|
-
if (!ts.isPropertyAssignment(p))
|
|
20
|
-
continue;
|
|
21
|
-
const name = p.name;
|
|
22
|
-
const key = ts.isIdentifier(name)
|
|
23
|
-
? name.text
|
|
24
|
-
: ts.isStringLiteral(name)
|
|
25
|
-
? name.text
|
|
26
|
-
: null;
|
|
27
|
-
if (key !== 'columnDefinitions')
|
|
28
|
-
continue;
|
|
29
|
-
return ts.isArrayLiteralExpression(p.initializer) ? p.initializer : null;
|
|
30
|
-
}
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
17
|
function hasRowTypeProp(obj) {
|
|
34
18
|
for (const p of obj.properties) {
|
|
35
19
|
if (!ts.isPropertyAssignment(p))
|
|
@@ -76,62 +60,70 @@ function ensureRowTypeImport(sourceText, sourceFile, importName, importPathNoExt
|
|
|
76
60
|
const insertPos = importStmts.length
|
|
77
61
|
? importStmts[importStmts.length - 1].end
|
|
78
62
|
: 0;
|
|
79
|
-
|
|
80
|
-
|
|
63
|
+
// After the last import the line break belongs in front; with no imports at
|
|
64
|
+
// all it belongs behind, so the file's first statement keeps its own line.
|
|
65
|
+
const importStatement = `import { ${importName} } from ${singleQuoteStringLiteral(importPathNoExt)};`;
|
|
66
|
+
const importLine = insertPos === 0 ? `${importStatement}\n` : `\n${importStatement}`;
|
|
81
67
|
return {
|
|
82
68
|
updatedText: applyTextEdits(sourceText, [{ pos: insertPos, insert: importLine }]),
|
|
83
69
|
changed: true
|
|
84
70
|
};
|
|
85
71
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
72
|
+
/**
|
|
73
|
+
* DSL helpers that take a `rowType` for type checking against the view's row.
|
|
74
|
+
* `column` sits directly in `columnDefinitions`; `filter` sits inside the
|
|
75
|
+
* groups of `filterGroups`, so the whole view argument is searched rather than
|
|
76
|
+
* one property of it.
|
|
77
|
+
*/
|
|
78
|
+
const ROW_TYPED_HELPERS = new Set(['column', 'filter']);
|
|
79
|
+
/** A `DSL.<helper>(...)` / `<ns>.DSL.<helper>(...)` call, however DTV was imported. */
|
|
80
|
+
function getRowTypedHelperCall(node, dslIdentifiers, dtvNamespaces) {
|
|
81
|
+
if (!ts.isCallExpression(node))
|
|
82
|
+
return null;
|
|
83
|
+
const expr = node.expression;
|
|
84
|
+
if (!ts.isPropertyAccessExpression(expr))
|
|
85
|
+
return null;
|
|
86
|
+
if (!ROW_TYPED_HELPERS.has(expr.name.text))
|
|
87
|
+
return null;
|
|
88
|
+
const receiver = expr.expression;
|
|
89
|
+
if (ts.isIdentifier(receiver) && dslIdentifiers.has(receiver.text))
|
|
90
|
+
return node;
|
|
91
|
+
if (ts.isPropertyAccessExpression(receiver) && receiver.name.text === 'DSL') {
|
|
92
|
+
const maybeNs = receiver.expression;
|
|
93
|
+
if (ts.isIdentifier(maybeNs) && dtvNamespaces.has(maybeNs.text))
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
function patchInlineRowTypeArgs(args) {
|
|
90
99
|
const edits = [];
|
|
91
100
|
let patchedCount = 0;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
const visit = (node) => {
|
|
102
|
+
const call = getRowTypedHelperCall(node, args.dslIdentifiers, args.dtvNamespaces);
|
|
103
|
+
if (call) {
|
|
104
|
+
const firstArg = call.arguments[0];
|
|
105
|
+
if (firstArg && ts.isObjectLiteralExpression(firstArg) && !hasRowTypeProp(firstArg)) {
|
|
106
|
+
const firstProp = firstArg.properties[0];
|
|
107
|
+
const insertPos = firstProp ? firstProp.getStart(args.sourceFile, false) : firstArg.getEnd() - 1;
|
|
108
|
+
const between = args.sourceText.slice(firstArg.getStart(args.sourceFile, false) + 1, insertPos);
|
|
109
|
+
const isMultiline = between.includes('\n');
|
|
110
|
+
if (isMultiline) {
|
|
111
|
+
// insertPos sits after the first property's indentation, so
|
|
112
|
+
// the new property goes in first and carries that
|
|
113
|
+
// indentation over to the one it displaced.
|
|
114
|
+
const lineStart = args.sourceText.lastIndexOf('\n', insertPos - 1) + 1;
|
|
115
|
+
const indent = args.sourceText.slice(lineStart, insertPos).match(/^[ \t]*/)?.[0] ?? '';
|
|
116
|
+
edits.push({ pos: insertPos, insert: `rowType: ${args.rowTypeIdentifier},\n${indent}` });
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
edits.push({ pos: insertPos, insert: `rowType: ${args.rowTypeIdentifier}, ` });
|
|
120
|
+
}
|
|
121
|
+
patchedCount += 1;
|
|
109
122
|
}
|
|
110
|
-
return false;
|
|
111
|
-
})();
|
|
112
|
-
if (!isDtvColumn)
|
|
113
|
-
continue;
|
|
114
|
-
const firstArg = el.arguments[0];
|
|
115
|
-
if (!firstArg || !ts.isObjectLiteralExpression(firstArg))
|
|
116
|
-
continue;
|
|
117
|
-
if (hasRowTypeProp(firstArg))
|
|
118
|
-
continue;
|
|
119
|
-
const firstProp = firstArg.properties[0];
|
|
120
|
-
const insertPos = firstProp ? firstProp.getStart(args.sourceFile, false) : firstArg.getEnd() - 1;
|
|
121
|
-
const between = args.sourceText.slice(firstArg.getStart(args.sourceFile, false) + 1, insertPos);
|
|
122
|
-
const isMultiline = between.includes('\n');
|
|
123
|
-
if (!isMultiline) {
|
|
124
|
-
edits.push({ pos: insertPos, insert: `rowType: ${args.rowTypeIdentifier}, ` });
|
|
125
|
-
patchedCount += 1;
|
|
126
|
-
continue;
|
|
127
123
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const indent = indentMatch ? indentMatch[0] : '';
|
|
132
|
-
edits.push({ pos: insertPos, insert: `${indent}rowType: ${args.rowTypeIdentifier},\n` });
|
|
133
|
-
patchedCount += 1;
|
|
134
|
-
}
|
|
124
|
+
ts.forEachChild(node, visit);
|
|
125
|
+
};
|
|
126
|
+
visit(args.viewArgObject);
|
|
135
127
|
if (edits.length === 0) {
|
|
136
128
|
return { updatedText: args.sourceText, changed: false, patchedCount: 0 };
|
|
137
129
|
}
|
|
@@ -183,7 +175,7 @@ function findViewArgObjectById(sourceFile, viewId, receivers) {
|
|
|
183
175
|
visit(sourceFile);
|
|
184
176
|
return viewArgObject;
|
|
185
177
|
}
|
|
186
|
-
function
|
|
178
|
+
function containsIdentifier(root, identifier) {
|
|
187
179
|
let found = false;
|
|
188
180
|
const visit = (node) => {
|
|
189
181
|
if (found)
|
|
@@ -194,10 +186,39 @@ function viewArgObjectHasIdentifier(viewArgObject, identifier) {
|
|
|
194
186
|
}
|
|
195
187
|
ts.forEachChild(node, visit);
|
|
196
188
|
};
|
|
189
|
+
visit(root);
|
|
190
|
+
return found;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The `rowType:` values in a view that are something other than the generated
|
|
194
|
+
* const — a hand-written shape, or `{} as any`. Those views read as covered
|
|
195
|
+
* while their checks run against a type nobody regenerates, which is worth
|
|
196
|
+
* telling their author about; passing no `rowType` at all is a different and
|
|
197
|
+
* perfectly fair choice.
|
|
198
|
+
*/
|
|
199
|
+
function findForeignRowTypes(viewArgObject, sourceFile, rowTypeConstName) {
|
|
200
|
+
const found = [];
|
|
201
|
+
const visit = (node) => {
|
|
202
|
+
if (ts.isPropertyAssignment(node)) {
|
|
203
|
+
const name = node.name;
|
|
204
|
+
const key = ts.isIdentifier(name)
|
|
205
|
+
? name.text
|
|
206
|
+
: ts.isStringLiteral(name)
|
|
207
|
+
? name.text
|
|
208
|
+
: null;
|
|
209
|
+
if (key === 'rowType' && !containsIdentifier(node.initializer, rowTypeConstName)) {
|
|
210
|
+
const text = node.initializer.getText(sourceFile).replace(/\s+/g, ' ');
|
|
211
|
+
found.push(text.length > 60 ? `${text.slice(0, 57)}...` : text);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
ts.forEachChild(node, visit);
|
|
215
|
+
};
|
|
197
216
|
visit(viewArgObject);
|
|
198
217
|
return found;
|
|
199
218
|
}
|
|
200
|
-
function findViewsInFile(sourceText, fileName, dtvImport, debug
|
|
219
|
+
function findViewsInFile(sourceText, fileName, dtvImport, debug,
|
|
220
|
+
/** Called for a `DSL.view({ ... })` whose id or source is not a string literal. */
|
|
221
|
+
onUnidentified) {
|
|
201
222
|
const sourceFile = ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.Latest, true);
|
|
202
223
|
const unwrapParens = (expr) => {
|
|
203
224
|
let cur = expr;
|
|
@@ -362,7 +383,9 @@ function findViewsInFile(sourceText, fileName, dtvImport, debug) {
|
|
|
362
383
|
? functionName
|
|
363
384
|
: null;
|
|
364
385
|
if (!viewId || !sourceType || !rootFieldName || (collectionName && functionName)) {
|
|
365
|
-
|
|
386
|
+
const detail = `id=${viewId ?? 'not a string literal'}, sourceType=${sourceType ?? 'not a string literal'}, collectionName=${collectionName ?? 'null'}, functionName=${functionName ?? 'null'}`;
|
|
387
|
+
debug?.log(`- found DSL.view({ ... }) but id/source not valid string literals (${detail})`);
|
|
388
|
+
onUnidentified?.(detail);
|
|
366
389
|
}
|
|
367
390
|
else {
|
|
368
391
|
debug?.log(`- found view id=${JSON.stringify(viewId)} rootFieldName=${JSON.stringify(rootFieldName)}`);
|
|
@@ -403,6 +426,7 @@ async function scanViews(config, debug) {
|
|
|
403
426
|
}
|
|
404
427
|
}
|
|
405
428
|
const results = [];
|
|
429
|
+
const unidentified = [];
|
|
406
430
|
const filesToScan = focusAbs ? files.filter(f => path.resolve(f) === path.resolve(focusAbs)) : files;
|
|
407
431
|
for (const f of filesToScan) {
|
|
408
432
|
if (!f.endsWith('.ts') && !f.endsWith('.tsx'))
|
|
@@ -411,7 +435,9 @@ async function scanViews(config, debug) {
|
|
|
411
435
|
const fileDebug = debug?.enabled
|
|
412
436
|
? { log: (line) => console.log(`[dtv typegen] ${path.resolve(f)} ${line}`) }
|
|
413
437
|
: undefined;
|
|
414
|
-
results.push(...findViewsInFile(text, f, dtvImport, fileDebug
|
|
438
|
+
results.push(...findViewsInFile(text, f, dtvImport, fileDebug, detail => {
|
|
439
|
+
unidentified.push({ sourceFile: f, detail });
|
|
440
|
+
}));
|
|
415
441
|
}
|
|
416
442
|
const byId = new Map();
|
|
417
443
|
for (const v of results) {
|
|
@@ -437,14 +463,19 @@ async function scanViews(config, debug) {
|
|
|
437
463
|
}
|
|
438
464
|
throw new Error(lines.join('\n'));
|
|
439
465
|
}
|
|
440
|
-
return results;
|
|
466
|
+
return { views: results, unidentified };
|
|
441
467
|
}
|
|
442
468
|
export async function runTypegen(args) {
|
|
443
469
|
const config = await loadConfig(args.configPath);
|
|
444
470
|
const debug = (args.debugScan || args.debugScanFile)
|
|
445
471
|
? { enabled: true, focusFile: args.debugScanFile }
|
|
446
472
|
: undefined;
|
|
447
|
-
const views = await scanViews(config, debug);
|
|
473
|
+
const { views, unidentified } = await scanViews(config, debug);
|
|
474
|
+
// Reported before anything else: a view read as unidentified generates
|
|
475
|
+
// nothing, which leaves any committed types for it silently frozen.
|
|
476
|
+
for (const u of unidentified) {
|
|
477
|
+
console.warn(`Warning: DSL.view({ ... }) in ${path.resolve(u.sourceFile)} was not read — ${u.detail}. Its id and source.collectionName / source.functionName must be string literals for types to be generated.`);
|
|
478
|
+
}
|
|
448
479
|
if (views.length === 0) {
|
|
449
480
|
throw new Error('No views found. Ensure Config.scan.include matches files that import DSL from your configured DTV specifier and call DSL.view({ ... }).');
|
|
450
481
|
}
|
|
@@ -477,6 +508,17 @@ export async function runTypegen(args) {
|
|
|
477
508
|
};
|
|
478
509
|
});
|
|
479
510
|
const outputFiles = new Set();
|
|
511
|
+
/**
|
|
512
|
+
* What became of each view. A view that produces nothing leaves whatever
|
|
513
|
+
* generated file is already committed in place, so silence here reads as
|
|
514
|
+
* success while the row type it describes goes stale — every outcome is
|
|
515
|
+
* recorded and reported.
|
|
516
|
+
*/
|
|
517
|
+
const written = [];
|
|
518
|
+
const removed = [];
|
|
519
|
+
const skipped = [];
|
|
520
|
+
const optedOut = [];
|
|
521
|
+
const failed = [];
|
|
480
522
|
for (const v of viewRows) {
|
|
481
523
|
const root = schema.getType(v.rowTypeName);
|
|
482
524
|
if (!root || Array.isArray(root)) {
|
|
@@ -562,6 +604,10 @@ export async function runTypegen(args) {
|
|
|
562
604
|
}
|
|
563
605
|
const viewArgObject = findViewArgObjectById(sf, v.viewId, { dslIdentifiers, dtvNamespaces });
|
|
564
606
|
if (!viewArgObject) {
|
|
607
|
+
skipped.push({
|
|
608
|
+
viewId: v.viewId,
|
|
609
|
+
reason: `could not locate the DSL.view({ id: ${JSON.stringify(v.viewId)} }) argument in ${path.resolve(v.sourceFile)}`
|
|
610
|
+
});
|
|
565
611
|
continue;
|
|
566
612
|
}
|
|
567
613
|
const importPathNoExt = './' + fileName.replace(/\.ts$/i, '');
|
|
@@ -569,9 +615,13 @@ export async function runTypegen(args) {
|
|
|
569
615
|
const sf2 = ts.createSourceFile(v.sourceFile, withImport.updatedText, ts.ScriptTarget.Latest, true);
|
|
570
616
|
const viewArgObject2 = findViewArgObjectById(sf2, v.viewId);
|
|
571
617
|
if (!viewArgObject2) {
|
|
618
|
+
skipped.push({
|
|
619
|
+
viewId: v.viewId,
|
|
620
|
+
reason: `could not re-locate the view argument after adding the ${rowTypeConstName} import`
|
|
621
|
+
});
|
|
572
622
|
continue;
|
|
573
623
|
}
|
|
574
|
-
const patched =
|
|
624
|
+
const patched = patchInlineRowTypeArgs({
|
|
575
625
|
sourceText: withImport.updatedText,
|
|
576
626
|
sourceFile: sf2,
|
|
577
627
|
viewArgObject: viewArgObject2,
|
|
@@ -582,10 +632,35 @@ export async function runTypegen(args) {
|
|
|
582
632
|
const sf3 = ts.createSourceFile(v.sourceFile, patched.updatedText, ts.ScriptTarget.Latest, true);
|
|
583
633
|
const viewArgObject3 = findViewArgObjectById(sf3, v.viewId, { dslIdentifiers, dtvNamespaces });
|
|
584
634
|
const shouldEmitForView = viewArgObject3
|
|
585
|
-
?
|
|
635
|
+
? containsIdentifier(viewArgObject3, rowTypeConstName)
|
|
586
636
|
: false;
|
|
587
637
|
if (!shouldEmitForView) {
|
|
638
|
+
// Nothing in the view reads the generated module. Passing no
|
|
639
|
+
// rowType is a fair choice; passing one of the view's own is
|
|
640
|
+
// not the same thing, and neither is losing the view argument
|
|
641
|
+
// — so each is named rather than left to silence.
|
|
642
|
+
const foreign = viewArgObject3
|
|
643
|
+
? findForeignRowTypes(viewArgObject3, sf3, rowTypeConstName)
|
|
644
|
+
: [];
|
|
645
|
+
const existed = await fs.access(outFile).then(() => true, () => false);
|
|
588
646
|
await fs.rm(outFile, { force: true });
|
|
647
|
+
if (existed)
|
|
648
|
+
removed.push({ viewId: v.viewId, outFile });
|
|
649
|
+
if (!viewArgObject3) {
|
|
650
|
+
skipped.push({
|
|
651
|
+
viewId: v.viewId,
|
|
652
|
+
reason: `its view argument could not be located after patching ${path.resolve(v.sourceFile)}`
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
else if (foreign.length) {
|
|
656
|
+
skipped.push({
|
|
657
|
+
viewId: v.viewId,
|
|
658
|
+
reason: `it passes rowType: ${foreign[0]} rather than ${rowTypeConstName}, so its checks run against a hand-written type instead of the generated one`
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
else if (!existed) {
|
|
662
|
+
optedOut.push(v.viewId);
|
|
663
|
+
}
|
|
589
664
|
continue;
|
|
590
665
|
}
|
|
591
666
|
if (outputFiles.has(outFile)) {
|
|
@@ -606,18 +681,38 @@ export async function runTypegen(args) {
|
|
|
606
681
|
''
|
|
607
682
|
].join('\n');
|
|
608
683
|
await writeFileEnsuringDir(outFile, content);
|
|
684
|
+
written.push(outFile);
|
|
609
685
|
if (withImport.changed || patched.changed) {
|
|
610
686
|
await fs.writeFile(v.sourceFile, patched.updatedText, 'utf8');
|
|
611
687
|
}
|
|
612
688
|
}
|
|
613
|
-
catch {
|
|
614
|
-
//
|
|
689
|
+
catch (error) {
|
|
690
|
+
// Keep going so one broken view doesn't hide the rest, and report
|
|
691
|
+
// it at the end: this used to be swallowed, which left the view's
|
|
692
|
+
// committed types silently frozen at whatever they were.
|
|
693
|
+
failed.push({ viewId: v.viewId, message: error instanceof Error ? error.message : String(error) });
|
|
615
694
|
}
|
|
616
695
|
}
|
|
696
|
+
for (const r of removed) {
|
|
697
|
+
console.warn(`Removed ${path.resolve(r.outFile)} — view ${JSON.stringify(r.viewId)} no longer passes its rowType.`);
|
|
698
|
+
}
|
|
699
|
+
for (const sk of skipped) {
|
|
700
|
+
console.warn(`Warning: generated no types for view ${JSON.stringify(sk.viewId)} — ${sk.reason}.`);
|
|
701
|
+
}
|
|
702
|
+
for (const viewId of optedOut) {
|
|
703
|
+
console.log(`View ${JSON.stringify(viewId)} passes no rowType, so it has no generated types.`);
|
|
704
|
+
}
|
|
617
705
|
if (args.onlyViewId) {
|
|
618
706
|
console.log(`Generated types for view ${JSON.stringify(args.onlyViewId)}.`);
|
|
619
707
|
}
|
|
620
708
|
else {
|
|
621
|
-
console.log(`Generated types for ${viewRows.length} view(s).`);
|
|
709
|
+
console.log(`Generated types for ${written.length} of ${viewRows.length} view(s).`);
|
|
710
|
+
}
|
|
711
|
+
if (failed.length) {
|
|
712
|
+
const lines = ['Type generation failed for:'];
|
|
713
|
+
for (const f of failed)
|
|
714
|
+
lines.push(`- ${f.viewId}: ${f.message}`);
|
|
715
|
+
lines.push('Their committed generated types are unchanged and may now be stale.');
|
|
716
|
+
throw new Error(lines.join('\n'));
|
|
622
717
|
}
|
|
623
718
|
}
|