@webergency-utils/typechecker 0.1.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/LICENSE +21 -0
- package/README.md +263 -0
- package/dist/engine/generators.d.ts +23 -0
- package/dist/engine/generators.js +283 -0
- package/dist/engine/hoister.d.ts +2 -0
- package/dist/engine/hoister.js +106 -0
- package/dist/engine/resolver.d.ts +7 -0
- package/dist/engine/resolver.js +882 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +2 -0
- package/dist/runtime/tags/constraint.d.ts +61 -0
- package/dist/runtime/tags/constraint.js +1 -0
- package/dist/runtime/tags/format.d.ts +15 -0
- package/dist/runtime/tags/format.js +1 -0
- package/dist/runtime/tags/tag.d.ts +3 -0
- package/dist/runtime/tags/tag.js +1 -0
- package/dist/runtime/tags/transform.d.ts +24 -0
- package/dist/runtime/tags/transform.js +1 -0
- package/dist/runtime/tags.d.ts +44 -0
- package/dist/runtime/tags.js +12 -0
- package/dist/runtime/validators.d.ts +89 -0
- package/dist/runtime/validators.js +768 -0
- package/dist/transformer.d.ts +3 -0
- package/dist/transformer.js +134 -0
- package/package.json +62 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export function hoistRegistrations(sourceFile, cache, requiredUtils, schemasMap) {
|
|
3
|
+
if (cache.size === 0 && requiredUtils.size === 0) {
|
|
4
|
+
return sourceFile;
|
|
5
|
+
}
|
|
6
|
+
const utilityStatements = [
|
|
7
|
+
// 1. import "@webergency-utils/typechecker/runtime";
|
|
8
|
+
ts.factory.createImportDeclaration(undefined, undefined, ts.factory.createStringLiteral('@webergency-utils/typechecker/runtime'), undefined)
|
|
9
|
+
];
|
|
10
|
+
if (!hasVariableDeclaration(sourceFile.statements, 'validators') &&
|
|
11
|
+
!hasVariableDeclaration(utilityStatements, 'validators')) {
|
|
12
|
+
utilityStatements.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
13
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier('validators'), undefined, undefined, ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('globalThis'), '__WEBERGENCY_TYPECHECKER_VALIDATORS__'))
|
|
14
|
+
], ts.NodeFlags.Const)));
|
|
15
|
+
}
|
|
16
|
+
if (!hasVariableDeclaration(sourceFile.statements, 'MetadataStore') &&
|
|
17
|
+
!hasVariableDeclaration(utilityStatements, 'MetadataStore')) {
|
|
18
|
+
utilityStatements.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
19
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier('MetadataStore'), undefined, undefined, ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('globalThis'), '__WEBERGENCY_TYPECHECKER_METADATA_STORE__'))
|
|
20
|
+
], ts.NodeFlags.Const)));
|
|
21
|
+
}
|
|
22
|
+
const variablePrepends = [];
|
|
23
|
+
const registrationAppends = [];
|
|
24
|
+
for (const [hash, expr] of cache.entries()) {
|
|
25
|
+
// const __val_hash = expr;
|
|
26
|
+
if (!hasVariableDeclaration(sourceFile.statements, `__val_${hash}`) &&
|
|
27
|
+
!hasVariableDeclaration(utilityStatements, `__val_${hash}`) &&
|
|
28
|
+
!hasVariableDeclaration(variablePrepends, `__val_${hash}`)) {
|
|
29
|
+
variablePrepends.push(ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
|
|
30
|
+
ts.factory.createVariableDeclaration(ts.factory.createIdentifier(`__val_${hash}`), undefined, undefined, expr)
|
|
31
|
+
], ts.NodeFlags.Const)));
|
|
32
|
+
}
|
|
33
|
+
// MetadataStore.registerValidator(hash, __val_hash);
|
|
34
|
+
registrationAppends.push(ts.factory.createExpressionStatement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('MetadataStore'), 'registerValidator'), undefined, [
|
|
35
|
+
ts.factory.createStringLiteral(hash),
|
|
36
|
+
ts.factory.createIdentifier(`__val_${hash}`)
|
|
37
|
+
])));
|
|
38
|
+
}
|
|
39
|
+
if (schemasMap) {
|
|
40
|
+
for (const [hash, schemaExpr] of schemasMap.entries()) {
|
|
41
|
+
registrationAppends.push(ts.factory.createExpressionStatement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('MetadataStore'), 'registerSchema'), undefined, [
|
|
42
|
+
ts.factory.createStringLiteral(hash),
|
|
43
|
+
schemaExpr
|
|
44
|
+
])));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const mergedStatements = [
|
|
48
|
+
...utilityStatements,
|
|
49
|
+
...variablePrepends,
|
|
50
|
+
...sourceFile.statements
|
|
51
|
+
];
|
|
52
|
+
const insertIndex = findInsertionIndex(mergedStatements);
|
|
53
|
+
return ts.factory.updateSourceFile(sourceFile, [
|
|
54
|
+
...mergedStatements.slice(0, insertIndex),
|
|
55
|
+
...registrationAppends,
|
|
56
|
+
...mergedStatements.slice(insertIndex)
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
function findInsertionIndex(statements) {
|
|
60
|
+
let lastClassIndex = -1;
|
|
61
|
+
for (let i = 0; i < statements.length; i++) {
|
|
62
|
+
if (ts.isClassDeclaration(statements[i])) {
|
|
63
|
+
lastClassIndex = i;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const startIndex = lastClassIndex !== -1 ? lastClassIndex + 1 : 0;
|
|
67
|
+
for (let i = startIndex; i < statements.length; i++) {
|
|
68
|
+
const s = statements[i];
|
|
69
|
+
if (ts.isImportDeclaration(s) || ts.isInterfaceDeclaration(s) || ts.isTypeAliasDeclaration(s)) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ts.isVariableStatement(s)) {
|
|
73
|
+
let isPrependedVar = true;
|
|
74
|
+
for (const decl of s.declarationList.declarations) {
|
|
75
|
+
if (ts.isIdentifier(decl.name)) {
|
|
76
|
+
const text = decl.name.text;
|
|
77
|
+
if (text !== 'validators' && text !== 'MetadataStore' && text !== '__server_metadata_store' && !text.startsWith('__val_')) {
|
|
78
|
+
isPrependedVar = false;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
isPrependedVar = false;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (isPrependedVar) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return i;
|
|
92
|
+
}
|
|
93
|
+
return statements.length;
|
|
94
|
+
}
|
|
95
|
+
function hasVariableDeclaration(statements, name) {
|
|
96
|
+
for (const statement of statements) {
|
|
97
|
+
if (ts.isVariableStatement(statement)) {
|
|
98
|
+
for (const decl of statement.declarationList.declarations) {
|
|
99
|
+
if (ts.isIdentifier(decl.name) && decl.name.text === name) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export declare function buildValidator(type: ts.Type, checker: ts.TypeChecker, validatorsMap: Map<string, ts.Expression>, requiredUtils: Set<string>): ts.Expression;
|
|
3
|
+
export declare function generateHash(type: ts.Type, checker: ts.TypeChecker): string;
|
|
4
|
+
export declare function objectToAst(val: any): ts.Expression;
|
|
5
|
+
export declare function getTypeComplexity(type: ts.Type, checker: ts.TypeChecker, visited: Set<number>): number;
|
|
6
|
+
export declare function preScanType(type: ts.Type, checker: ts.TypeChecker, counts: Map<string, number>, circularHashes: Set<string>, visited: Set<number>): void;
|
|
7
|
+
export declare function buildJsonSchema(type: ts.Type, checker: ts.TypeChecker): any;
|