@bidirekt/typescript-sdk-cli 0.1.1 → 0.3.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 +73 -41
- package/dist/command.js +14 -13
- package/dist/model/contract.js +7 -1
- package/dist/model/declarations-to-contract.js +97 -37
- package/dist/model/description.js +16 -0
- package/dist/model/protocols/graphql/block-to-references.js +8 -0
- package/dist/model/protocols/graphql/blocks-to-merged.js +77 -0
- package/dist/model/protocols/graphql/contract.js +1 -0
- package/dist/model/protocols/graphql/field-extra.js +46 -0
- package/dist/model/protocols/graphql/returns-to-schema.js +77 -0
- package/dist/model/protocols/rest/contract.js +1 -0
- package/dist/model/{resources-to-merged.js → protocols/rest/resources-to-merged.js} +15 -10
- package/dist/model/protocols/rest/resources-to-references.js +10 -0
- package/dist/protocols.js +49 -0
- package/dist/reader/comment-tags.js +77 -37
- package/dist/reader/literal-options.js +17 -2
- package/dist/reader/program.js +1 -1
- package/dist/reader/project-to-contract.js +51 -23
- package/dist/reader/protocols/graphql/decorators-to-blocks.js +401 -0
- package/dist/reader/protocols/graphql/protocol-input-to-blocks.js +11 -0
- package/dist/reader/protocols/graphql/tags-to-blocks.js +239 -0
- package/dist/reader/{handler-methods.js → protocols/rest/handler-methods.js} +36 -26
- package/dist/reader/protocols/rest/protocol-input-to-resources.js +11 -0
- package/dist/reader/protocols/rest/tag-handlers.js +152 -0
- package/dist/reader/schema-classes.js +193 -31
- package/dist/reader/schema-context.js +10 -5
- package/dist/reader/sdk-references.js +149 -39
- package/dist/reader/tag-handlers.js +36 -146
- package/dist/reader/tag-schemas.js +93 -43
- package/dist/reader/type-text-to-schema-node.js +4 -1
- package/dist/reader/type-to-schema-node.js +60 -10
- package/dist/writer/contract-to-yaml.js +38 -59
- package/dist/writer/protocols/graphql/block-to-yaml.js +31 -0
- package/dist/writer/protocols/graphql/field-extra-to-yaml.js +13 -0
- package/dist/writer/protocols/rest/resources-to-yaml.js +36 -0
- package/package.json +2 -2
- /package/dist/model/{schema-names.js → protocols/rest/schema-names.js} +0 -0
|
@@ -1,20 +1,34 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { isDiagnostic } from "../model/diagnostic.js";
|
|
3
|
-
import { expressionToUnionMembers } from "./expression-to-schema-node.js";
|
|
4
|
-
import { expressionToLiteralOptions, literalOptionToBoolean,
|
|
3
|
+
import { expressionToSchemaRef, expressionToUnionMembers, } from "./expression-to-schema-node.js";
|
|
4
|
+
import { expressionToLiteralOptions, literalOptionToBoolean, literalOptionToDescription, literalOptionToName, literalOptionToStringLiteral, propertyNameToText, } from "./literal-options.js";
|
|
5
5
|
import { nodeToLocation } from "./location.js";
|
|
6
|
-
import { builtDeclarationToDiagnostic, isLibrarySymbol, isObjectDeclaration, javaScriptTagHints, memberToOwner, nodeToSymbol, symbolToClass, tagHints, unreadClassToDiagnostic, } from "./schema-context.js";
|
|
6
|
+
import { builtDeclarationToDiagnostic, fieldDecoratorHints, isLibrarySymbol, isObjectDeclaration, javaScriptTagHints, memberToOwner, nodeToSymbol, symbolToClass, tagHints, unreadClassToDiagnostic, } from "./schema-context.js";
|
|
7
7
|
import { typeTextToSchemaNode, typeTextUsage, } from "./type-text-to-schema-node.js";
|
|
8
|
-
import { typeNodeToDeclaredUnion, typeNodeToFlags, typeNodeToTranslatedType, } from "./type-to-schema-node.js";
|
|
9
|
-
|
|
8
|
+
import { typeNodeToDeclaredUnion, typeNodeToFlags, typeNodeToShapedLeaf, typeNodeToTranslatedType, } from "./type-to-schema-node.js";
|
|
9
|
+
const schemaOptionKeys = ['name', 'union'];
|
|
10
|
+
const fieldOptionKeys = [
|
|
11
|
+
'name',
|
|
12
|
+
'description',
|
|
13
|
+
'optional',
|
|
14
|
+
'nullable',
|
|
15
|
+
'type',
|
|
16
|
+
'union',
|
|
17
|
+
];
|
|
18
|
+
export function sdkReferencesToSchemas(references, tags, context, protocols) {
|
|
10
19
|
const errors = new Set();
|
|
11
20
|
const fieldAnnotations = new Map();
|
|
12
21
|
for (const [field, tagField] of tags.fields) {
|
|
13
22
|
fieldAnnotations.set(field, { from: 'tag', ...tagField });
|
|
14
23
|
}
|
|
24
|
+
const fieldDecorators = protocolsToFieldDecorators(protocols);
|
|
15
25
|
const schemaDecorators = [];
|
|
16
26
|
for (const reference of references) {
|
|
17
|
-
if (reference.use
|
|
27
|
+
if (reference.use !== 'decorator') {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const vocabulary = fieldDecorators.get(reference.name);
|
|
31
|
+
if (reference.protocol !== undefined && vocabulary === undefined) {
|
|
18
32
|
continue;
|
|
19
33
|
}
|
|
20
34
|
const element = reference.decorator.parent;
|
|
@@ -22,17 +36,32 @@ export function sdkReferencesToSchemas(references, tags, context) {
|
|
|
22
36
|
schemaDecorators.push([element, reference.call]);
|
|
23
37
|
}
|
|
24
38
|
else if (ts.isPropertyDeclaration(element)) {
|
|
25
|
-
|
|
39
|
+
const tagged = tags.fields.get(element);
|
|
40
|
+
if (tagged !== undefined) {
|
|
26
41
|
errors.add({
|
|
27
|
-
message:
|
|
42
|
+
message: `@${reference.name} and "${tagged.label}" are both on this property: keep one`,
|
|
28
43
|
location: reference.location,
|
|
29
44
|
});
|
|
30
45
|
continue;
|
|
31
46
|
}
|
|
32
|
-
fieldAnnotations.
|
|
47
|
+
const decorated = fieldAnnotations.get(element);
|
|
48
|
+
if (decorated?.from === 'decorator' &&
|
|
49
|
+
decorated.name !== reference.name) {
|
|
50
|
+
const message = `@${decorated.name} and @${reference.name} are both on this property: keep one`;
|
|
51
|
+
errors.add({ message, location: nodeToLocation(decorated.call) });
|
|
52
|
+
errors.add({ message, location: reference.location });
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
fieldAnnotations.set(element, {
|
|
56
|
+
from: 'decorator',
|
|
57
|
+
name: reference.name,
|
|
58
|
+
call: reference.call,
|
|
59
|
+
vocabulary,
|
|
60
|
+
});
|
|
33
61
|
}
|
|
34
62
|
}
|
|
35
63
|
const unreadOptions = new Set();
|
|
64
|
+
const unionOptions = new Map();
|
|
36
65
|
for (const [declaration, call] of schemaDecorators) {
|
|
37
66
|
if (tags.schemas.has(declaration)) {
|
|
38
67
|
errors.add({
|
|
@@ -42,18 +71,32 @@ export function sdkReferencesToSchemas(references, tags, context) {
|
|
|
42
71
|
continue;
|
|
43
72
|
}
|
|
44
73
|
let name = declaration.name?.text;
|
|
45
|
-
const declared =
|
|
74
|
+
const declared = schemaClassToOptions(declaration, call);
|
|
46
75
|
if (isDiagnostic(declared)) {
|
|
47
76
|
errors.add(declared);
|
|
48
77
|
unreadOptions.add(declaration);
|
|
49
78
|
}
|
|
50
79
|
else {
|
|
51
|
-
name = declared;
|
|
80
|
+
name = declared.name;
|
|
81
|
+
unionOptions.set(declaration, declared.union);
|
|
52
82
|
}
|
|
53
83
|
if (name !== undefined) {
|
|
54
84
|
context.schemaDeclarations.set(declaration, name);
|
|
55
85
|
}
|
|
56
86
|
}
|
|
87
|
+
const unions = new Map(tags.unions);
|
|
88
|
+
for (const [declaration, expression] of unionOptions) {
|
|
89
|
+
if (expression === undefined) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const members = expressionToUnionMembers(expression, '"union"', context);
|
|
93
|
+
if (isDiagnostic(members)) {
|
|
94
|
+
errors.add(members);
|
|
95
|
+
unreadOptions.add(declaration);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
unions.set(declaration, { members, location: nodeToLocation(expression) });
|
|
99
|
+
}
|
|
57
100
|
const fields = new Map();
|
|
58
101
|
const schemas = [];
|
|
59
102
|
const reading = {
|
|
@@ -66,7 +109,9 @@ export function sdkReferencesToSchemas(references, tags, context) {
|
|
|
66
109
|
for (const [declaration, name] of context.schemaDeclarations) {
|
|
67
110
|
const properties = [];
|
|
68
111
|
const declaredFields = declarationToFields(declaration, reading);
|
|
112
|
+
const union = unions.get(declaration);
|
|
69
113
|
if (declaredFields.length === 0 &&
|
|
114
|
+
union === undefined &&
|
|
70
115
|
!unreadOptions.has(declaration) &&
|
|
71
116
|
!hasUnreadFields(declaration, reading)) {
|
|
72
117
|
errors.add({
|
|
@@ -76,7 +121,7 @@ export function sdkReferencesToSchemas(references, tags, context) {
|
|
|
76
121
|
}
|
|
77
122
|
for (const [field, annotation] of declaredFields) {
|
|
78
123
|
if (!fields.has(field)) {
|
|
79
|
-
const read = fieldToProperty(field, annotation, context);
|
|
124
|
+
const read = fieldToProperty(field, annotation, context, protocols);
|
|
80
125
|
fields.set(field, read);
|
|
81
126
|
if (isDiagnostic(read)) {
|
|
82
127
|
errors.add(read);
|
|
@@ -87,9 +132,13 @@ export function sdkReferencesToSchemas(references, tags, context) {
|
|
|
87
132
|
properties.push(read);
|
|
88
133
|
}
|
|
89
134
|
}
|
|
135
|
+
const selfMember = union && unionToSelfMemberDiagnostic(union, name);
|
|
136
|
+
if (selfMember !== undefined) {
|
|
137
|
+
errors.add(selfMember);
|
|
138
|
+
}
|
|
90
139
|
schemas.push({
|
|
91
140
|
name,
|
|
92
|
-
node:
|
|
141
|
+
node: propertiesToSchemaNode(properties, union),
|
|
93
142
|
location: nodeToLocation(declaration.name ?? declaration),
|
|
94
143
|
});
|
|
95
144
|
}
|
|
@@ -102,6 +151,33 @@ export function sdkReferencesToSchemas(references, tags, context) {
|
|
|
102
151
|
]),
|
|
103
152
|
};
|
|
104
153
|
}
|
|
154
|
+
function unionToSelfMemberDiagnostic(union, name) {
|
|
155
|
+
const itself = union.members.find(({ node }) => node.type === 'ref' && node.schema === name);
|
|
156
|
+
if (itself === undefined) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
message: `schema "${name}" lists itself as member "${itself.key}" of its union: remove that member`,
|
|
161
|
+
location: union.location,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function propertiesToSchemaNode(properties, union) {
|
|
165
|
+
if (union === undefined) {
|
|
166
|
+
return { type: 'object', properties };
|
|
167
|
+
}
|
|
168
|
+
if (properties.length === 0) {
|
|
169
|
+
return { type: 'union', members: union.members };
|
|
170
|
+
}
|
|
171
|
+
return { type: 'union', properties, members: union.members };
|
|
172
|
+
}
|
|
173
|
+
function protocolsToFieldDecorators(protocols) {
|
|
174
|
+
return new Map(protocols.flatMap(({ vocabulary }) => {
|
|
175
|
+
if (vocabulary === undefined) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
return [[vocabulary.fieldDecorator, vocabulary]];
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
105
181
|
function schemaToHints(declaration, tags, context) {
|
|
106
182
|
if (tags.schemas.has(declaration)) {
|
|
107
183
|
return tagHints;
|
|
@@ -131,7 +207,7 @@ function fieldsOutsideSchemas({ fieldAnnotations, parents, context }, annotatedS
|
|
|
131
207
|
}
|
|
132
208
|
return [
|
|
133
209
|
{
|
|
134
|
-
message:
|
|
210
|
+
message: `@${annotation.name} is in ${declarationToDescription(field.parent)}, which is neither a schema nor the parent of one, so it is not read: add @BidirektSchema to the class`,
|
|
135
211
|
location: nodeToLocation(annotation.call),
|
|
136
212
|
},
|
|
137
213
|
];
|
|
@@ -163,8 +239,8 @@ export function declarationToDescription(declaration) {
|
|
|
163
239
|
}
|
|
164
240
|
return `class "${name}"`;
|
|
165
241
|
}
|
|
166
|
-
function
|
|
167
|
-
const options = expressionToLiteralOptions(call.arguments[0], '@BidirektSchema options',
|
|
242
|
+
function schemaClassToOptions(declaration, call) {
|
|
243
|
+
const options = expressionToLiteralOptions(call.arguments[0], '@BidirektSchema options', schemaOptionKeys);
|
|
168
244
|
if (isDiagnostic(options)) {
|
|
169
245
|
return options;
|
|
170
246
|
}
|
|
@@ -179,7 +255,7 @@ function schemaClassToName(declaration, call) {
|
|
|
179
255
|
location: nodeToLocation(declaration),
|
|
180
256
|
};
|
|
181
257
|
}
|
|
182
|
-
return resolved;
|
|
258
|
+
return { name: resolved, union: options.get('union') };
|
|
183
259
|
}
|
|
184
260
|
function declarationToFields(declaration, reading, visited = new Set()) {
|
|
185
261
|
const fields = [];
|
|
@@ -314,9 +390,9 @@ function unreadableParent(parent, declaration) {
|
|
|
314
390
|
location: nodeToLocation(parent),
|
|
315
391
|
};
|
|
316
392
|
}
|
|
317
|
-
function fieldToProperty(field, annotation, context) {
|
|
393
|
+
function fieldToProperty(field, annotation, context, protocols) {
|
|
318
394
|
const location = nodeToLocation(field.name);
|
|
319
|
-
const options = annotationToFieldOptions(annotation, context);
|
|
395
|
+
const options = annotationToFieldOptions(annotation, context, protocols);
|
|
320
396
|
if (isDiagnostic(options)) {
|
|
321
397
|
return withLocation(options, location);
|
|
322
398
|
}
|
|
@@ -332,9 +408,9 @@ function fieldToProperty(field, annotation, context) {
|
|
|
332
408
|
if (isDiagnostic(read)) {
|
|
333
409
|
return withLocation(read, location);
|
|
334
410
|
}
|
|
335
|
-
return
|
|
411
|
+
return fieldOptionsToProperty(name, read, options, location);
|
|
336
412
|
}
|
|
337
|
-
export function
|
|
413
|
+
export function fieldOptionsToProperty(name, read, options, location) {
|
|
338
414
|
const node = { ...read };
|
|
339
415
|
delete node.optional;
|
|
340
416
|
delete node.nullable;
|
|
@@ -347,11 +423,23 @@ export function fieldOptionsToNode(read, options) {
|
|
|
347
423
|
if (options.description !== undefined) {
|
|
348
424
|
node.description = options.description;
|
|
349
425
|
}
|
|
350
|
-
|
|
426
|
+
const { extension } = options;
|
|
427
|
+
if (extension === undefined) {
|
|
428
|
+
return { name, node, location };
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
name,
|
|
432
|
+
node: extension.vocabulary.toNode(extension.options, node),
|
|
433
|
+
location,
|
|
434
|
+
vocabulary: extension.vocabulary,
|
|
435
|
+
};
|
|
351
436
|
}
|
|
352
437
|
function annotationToContext(annotation, field, context) {
|
|
353
438
|
if (annotation.from !== 'tag') {
|
|
354
|
-
return
|
|
439
|
+
return {
|
|
440
|
+
...context,
|
|
441
|
+
hints: { ...context.hints, ...fieldDecoratorHints(annotation.name) },
|
|
442
|
+
};
|
|
355
443
|
}
|
|
356
444
|
if ((field.flags & ts.NodeFlags.JavaScriptFile) !== 0) {
|
|
357
445
|
return { ...context, hints: javaScriptTagHints };
|
|
@@ -366,6 +454,7 @@ function withLocation(diagnostic, location) {
|
|
|
366
454
|
}
|
|
367
455
|
function fieldToSchemaNode(field, options, context) {
|
|
368
456
|
const typeNode = field.type ?? ts.getJSDocType(field);
|
|
457
|
+
const leaf = fieldOptionsToLeaf(options);
|
|
369
458
|
if (options.declared !== undefined) {
|
|
370
459
|
const declared = declaredToFieldNode(options.declared, typeNode, context);
|
|
371
460
|
if (isDiagnostic(declared)) {
|
|
@@ -373,13 +462,13 @@ function fieldToSchemaNode(field, options, context) {
|
|
|
373
462
|
}
|
|
374
463
|
const flags = fieldToFlags(field, typeNode, context);
|
|
375
464
|
return {
|
|
376
|
-
...declared,
|
|
465
|
+
...withLeaf(declared, leaf),
|
|
377
466
|
optional: declared.optional ?? flags.optional,
|
|
378
467
|
nullable: declared.nullable ?? flags.nullable,
|
|
379
468
|
};
|
|
380
469
|
}
|
|
381
470
|
if (typeNode !== undefined) {
|
|
382
|
-
const translated =
|
|
471
|
+
const translated = typeNodeToFieldType(typeNode, leaf, context);
|
|
383
472
|
if (isDiagnostic(translated)) {
|
|
384
473
|
return translated;
|
|
385
474
|
}
|
|
@@ -388,10 +477,32 @@ function fieldToSchemaNode(field, options, context) {
|
|
|
388
477
|
optional: translated.optional || field.questionToken !== undefined,
|
|
389
478
|
};
|
|
390
479
|
}
|
|
480
|
+
if (leaf !== undefined) {
|
|
481
|
+
return { ...leaf, optional: field.questionToken !== undefined };
|
|
482
|
+
}
|
|
391
483
|
return {
|
|
392
484
|
message: `cannot read the type of "${field.name.getText()}": write its type or declare it with ${context.hints.declareFieldType}`,
|
|
393
485
|
};
|
|
394
486
|
}
|
|
487
|
+
function typeNodeToFieldType(typeNode, leaf, context) {
|
|
488
|
+
if (leaf === undefined) {
|
|
489
|
+
return typeNodeToTranslatedType(typeNode, context);
|
|
490
|
+
}
|
|
491
|
+
return typeNodeToShapedLeaf(typeNode, leaf, context);
|
|
492
|
+
}
|
|
493
|
+
export function fieldOptionsToLeaf(options) {
|
|
494
|
+
const { extension } = options;
|
|
495
|
+
return extension?.vocabulary.toLeaf?.(extension.options);
|
|
496
|
+
}
|
|
497
|
+
export function withLeaf(node, leaf) {
|
|
498
|
+
if (leaf === undefined) {
|
|
499
|
+
return node;
|
|
500
|
+
}
|
|
501
|
+
if (node.type === 'array') {
|
|
502
|
+
return { ...node, items: withLeaf(node.items, leaf) };
|
|
503
|
+
}
|
|
504
|
+
return leaf;
|
|
505
|
+
}
|
|
395
506
|
function declaredToFieldNode(declared, typeNode, context) {
|
|
396
507
|
if (declared.type !== 'union' || typeNode === undefined) {
|
|
397
508
|
return declared;
|
|
@@ -425,22 +536,73 @@ function fieldToFlags(field, typeNode, context) {
|
|
|
425
536
|
optional: flags.optional || field.questionToken !== undefined,
|
|
426
537
|
};
|
|
427
538
|
}
|
|
428
|
-
function annotationToFieldOptions(annotation, context) {
|
|
539
|
+
function annotationToFieldOptions(annotation, context, protocols) {
|
|
429
540
|
if (annotation.from === 'tag') {
|
|
430
541
|
return annotation.options;
|
|
431
542
|
}
|
|
432
|
-
const
|
|
543
|
+
const { name, call, vocabulary } = annotation;
|
|
544
|
+
const options = expressionToLiteralOptions(call.arguments[0], `@${name} options`, [...fieldOptionKeys, ...(vocabulary?.fieldOptions ?? [])], undefined, (key) => optionToFieldDecoratorHint(key, protocols));
|
|
433
545
|
if (isDiagnostic(options)) {
|
|
434
546
|
return options;
|
|
435
547
|
}
|
|
436
|
-
|
|
548
|
+
const fieldOptions = literalOptionsToFieldOptions(options, name, context);
|
|
549
|
+
if (vocabulary === undefined || isDiagnostic(fieldOptions)) {
|
|
550
|
+
return fieldOptions;
|
|
551
|
+
}
|
|
552
|
+
const extension = literalOptionsToExtension(options, vocabulary, context);
|
|
553
|
+
if (isDiagnostic(extension)) {
|
|
554
|
+
return extension;
|
|
555
|
+
}
|
|
556
|
+
return { ...fieldOptions, extension };
|
|
557
|
+
}
|
|
558
|
+
function optionToFieldDecoratorHint(key, protocols) {
|
|
559
|
+
const vocabulary = optionToOwningProtocol(key, protocols)?.vocabulary;
|
|
560
|
+
if (vocabulary === undefined) {
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
return `for ${vocabulary.label}, use @${vocabulary.fieldDecorator}`;
|
|
564
|
+
}
|
|
565
|
+
export function optionToOwningProtocol(key, protocols) {
|
|
566
|
+
return protocols.find(({ vocabulary }) => vocabulary?.fieldOptions.includes(key) === true);
|
|
567
|
+
}
|
|
568
|
+
function literalOptionsToExtension(options, vocabulary, context) {
|
|
569
|
+
const values = {};
|
|
570
|
+
for (const key of vocabulary.fieldOptions) {
|
|
571
|
+
const expression = options.get(key);
|
|
572
|
+
if (expression === undefined) {
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
const value = expressionToOptionValue(key, expression, context);
|
|
576
|
+
if (isDiagnostic(value)) {
|
|
577
|
+
return value;
|
|
578
|
+
}
|
|
579
|
+
values[key] = value;
|
|
580
|
+
}
|
|
581
|
+
return { vocabulary, options: values };
|
|
582
|
+
}
|
|
583
|
+
function expressionToOptionValue(key, expression, context) {
|
|
584
|
+
if (ts.isStringLiteralLike(expression)) {
|
|
585
|
+
return expression.text;
|
|
586
|
+
}
|
|
587
|
+
if (ts.isIdentifier(expression) ||
|
|
588
|
+
ts.isPropertyAccessExpression(expression)) {
|
|
589
|
+
const ref = expressionToSchemaRef(expression, context);
|
|
590
|
+
if (isDiagnostic(ref)) {
|
|
591
|
+
return ref;
|
|
592
|
+
}
|
|
593
|
+
return ref.schema;
|
|
594
|
+
}
|
|
595
|
+
return {
|
|
596
|
+
message: `"${key}" must be a string literal or a @BidirektSchema class written in place`,
|
|
597
|
+
location: nodeToLocation(expression),
|
|
598
|
+
};
|
|
437
599
|
}
|
|
438
|
-
function literalOptionsToFieldOptions(options, context) {
|
|
600
|
+
function literalOptionsToFieldOptions(options, decorator, context) {
|
|
439
601
|
const name = literalOptionToName(options, 'name');
|
|
440
602
|
if (isDiagnostic(name)) {
|
|
441
603
|
return name;
|
|
442
604
|
}
|
|
443
|
-
const description =
|
|
605
|
+
const description = literalOptionToDescription(options);
|
|
444
606
|
if (isDiagnostic(description)) {
|
|
445
607
|
return description;
|
|
446
608
|
}
|
|
@@ -459,7 +621,7 @@ function literalOptionsToFieldOptions(options, context) {
|
|
|
459
621
|
const union = options.get('union');
|
|
460
622
|
if (type !== undefined && union !== undefined) {
|
|
461
623
|
return {
|
|
462
|
-
message:
|
|
624
|
+
message: `declare either "type" or "union" in @${decorator}, not both`,
|
|
463
625
|
};
|
|
464
626
|
}
|
|
465
627
|
const fieldOptions = { name, description, optional, nullable };
|
|
@@ -2,14 +2,19 @@ import ts from 'typescript';
|
|
|
2
2
|
import { pathToShown } from "./location.js";
|
|
3
3
|
import { declarationToScript, isBuildOutput, isInstalledFile, } from "./program.js";
|
|
4
4
|
import { symbolToTarget } from "./sdk-references.js";
|
|
5
|
+
export function fieldDecoratorHints(decorator) {
|
|
6
|
+
return {
|
|
7
|
+
declareType: `@${decorator}({ type })`,
|
|
8
|
+
declareFieldType: `@${decorator}({ type })`,
|
|
9
|
+
declareUnion: `@${decorator}({ union })`,
|
|
10
|
+
declareName: `@${decorator}({ name })`,
|
|
11
|
+
placeUnion: `the "union" of @${decorator}`,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
5
14
|
const decoratorHints = {
|
|
6
|
-
|
|
7
|
-
declareFieldType: '@BidirektField({ type })',
|
|
8
|
-
declareUnion: '@BidirektField({ union })',
|
|
9
|
-
declareName: '@BidirektField({ name })',
|
|
15
|
+
...fieldDecoratorHints('BidirektField'),
|
|
10
16
|
schemaAnnotation: '@BidirektSchema',
|
|
11
17
|
addSchema: 'add @BidirektSchema to it',
|
|
12
|
-
placeUnion: 'the "union" of @BidirektField',
|
|
13
18
|
annotateField: '@BidirektField',
|
|
14
19
|
builtDeclaration: 'which has no decorators',
|
|
15
20
|
readsDeclarationFiles: false,
|
|
@@ -1,38 +1,41 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { nodeToLocation } from "./location.js";
|
|
3
|
-
import { programToUserFiles } from "./program.js";
|
|
4
|
-
const
|
|
3
|
+
import { programToUserFiles, sdkPackage } from "./program.js";
|
|
4
|
+
export const fieldTarget = {
|
|
5
|
+
isTarget: (node) => ts.isPropertyDeclaration(node) && !isStatic(node),
|
|
6
|
+
description: 'a property',
|
|
7
|
+
};
|
|
8
|
+
const coreTargets = {
|
|
5
9
|
BidirektSchema: {
|
|
6
10
|
isTarget: ts.isClassDeclaration,
|
|
7
11
|
description: 'a class declaration',
|
|
8
12
|
},
|
|
9
|
-
BidirektField:
|
|
10
|
-
isTarget: (node) => ts.isPropertyDeclaration(node) && !isStatic(node),
|
|
11
|
-
description: 'a property',
|
|
12
|
-
},
|
|
13
|
-
BidirektProvider: {
|
|
14
|
-
isTarget: ts.isMethodDeclaration,
|
|
15
|
-
description: 'a method',
|
|
16
|
-
},
|
|
17
|
-
BidirektConsumer: {
|
|
18
|
-
isTarget: ts.isMethodDeclaration,
|
|
19
|
-
description: 'a method',
|
|
20
|
-
},
|
|
13
|
+
BidirektField: fieldTarget,
|
|
21
14
|
};
|
|
22
|
-
export function
|
|
23
|
-
return
|
|
15
|
+
export function protocolsToDecoratorTargets(protocols) {
|
|
16
|
+
return new Map([
|
|
17
|
+
...Object.entries(coreTargets),
|
|
18
|
+
...protocols.flatMap(({ name, exports }) => Object.entries(exports).map(([exported, target]) => [
|
|
19
|
+
exported,
|
|
20
|
+
{ ...target, protocol: name },
|
|
21
|
+
])),
|
|
22
|
+
]);
|
|
24
23
|
}
|
|
25
|
-
export function programToSdkReferences(projectProgram) {
|
|
24
|
+
export function programToSdkReferences(projectProgram, protocols) {
|
|
25
|
+
const targets = protocolsToDecoratorTargets(protocols);
|
|
26
|
+
const renamed = new Map(protocols.flatMap(({ renamedExports }) => Object.entries(renamedExports ?? {})));
|
|
26
27
|
const checker = projectProgram.program.getTypeChecker();
|
|
27
|
-
const
|
|
28
|
+
const sdk = programToSdkModule(projectProgram, checker);
|
|
28
29
|
const found = [];
|
|
29
|
-
|
|
30
|
+
const unexported = [];
|
|
31
|
+
if (sdk === undefined) {
|
|
30
32
|
return [];
|
|
31
33
|
}
|
|
32
34
|
const pending = programToUserFiles(projectProgram)
|
|
33
35
|
.filter((file) => !file.isDeclarationFile)
|
|
34
36
|
.reverse();
|
|
35
37
|
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
|
38
|
+
unexported.push(...nodeToUnexported(node, checker, sdk, renamed));
|
|
36
39
|
if (ts.isImportDeclaration(node) ||
|
|
37
40
|
ts.isImportEqualsDeclaration(node) ||
|
|
38
41
|
ts.isExportDeclaration(node) ||
|
|
@@ -41,7 +44,7 @@ export function programToSdkReferences(projectProgram) {
|
|
|
41
44
|
continue;
|
|
42
45
|
}
|
|
43
46
|
if (ts.isIdentifier(node)) {
|
|
44
|
-
const name = identifierToSdkExportName(node, checker,
|
|
47
|
+
const name = identifierToSdkExportName(node, checker, sdk.functions);
|
|
45
48
|
if (name !== undefined) {
|
|
46
49
|
const expression = identifierToExpression(node);
|
|
47
50
|
found.push({
|
|
@@ -58,7 +61,95 @@ export function programToSdkReferences(projectProgram) {
|
|
|
58
61
|
pending.push(children[index]);
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
|
-
return foundToSdkReferences(found);
|
|
64
|
+
return [...foundToSdkReferences(found, targets), ...unexported];
|
|
65
|
+
}
|
|
66
|
+
function nodeToUnexported(node, checker, sdk, renamed) {
|
|
67
|
+
const imported = nodeToImportedNames(node);
|
|
68
|
+
if (imported === undefined || !isSdkModule(imported.from, checker, sdk)) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
return imported.names
|
|
72
|
+
.filter(({ name }) => !sdk.names.has(name.text))
|
|
73
|
+
.map(({ name, typeOnly }) => ({
|
|
74
|
+
use: 'unexported',
|
|
75
|
+
name: name.text,
|
|
76
|
+
message: unexportedToMessage(name.text, typeOnly, renamed),
|
|
77
|
+
location: nodeToLocation(name),
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
function unexportedToMessage(name, typeOnly, renamed) {
|
|
81
|
+
const current = renamed.get(name);
|
|
82
|
+
if (current !== undefined) {
|
|
83
|
+
return `${name} is not exported by ${sdkPackage}: it is now ${current}`;
|
|
84
|
+
}
|
|
85
|
+
if (typeOnly) {
|
|
86
|
+
return `${name} is not exported by ${sdkPackage}`;
|
|
87
|
+
}
|
|
88
|
+
return `${name} is not exported by ${sdkPackage}, so it is not read`;
|
|
89
|
+
}
|
|
90
|
+
function nodeToImportedNames(node) {
|
|
91
|
+
if (ts.isImportDeclaration(node) &&
|
|
92
|
+
node.importClause?.namedBindings !== undefined &&
|
|
93
|
+
ts.isNamedImports(node.importClause.namedBindings)) {
|
|
94
|
+
const isTypeOnly = node.importClause.phaseModifier === ts.SyntaxKind.TypeKeyword;
|
|
95
|
+
return {
|
|
96
|
+
from: node.moduleSpecifier,
|
|
97
|
+
names: node.importClause.namedBindings.elements.map((specifier) => specifierToName(specifier, isTypeOnly)),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (ts.isExportDeclaration(node) &&
|
|
101
|
+
node.moduleSpecifier !== undefined &&
|
|
102
|
+
node.exportClause !== undefined &&
|
|
103
|
+
ts.isNamedExports(node.exportClause)) {
|
|
104
|
+
const { isTypeOnly } = node;
|
|
105
|
+
return {
|
|
106
|
+
from: node.moduleSpecifier,
|
|
107
|
+
names: node.exportClause.elements.map((specifier) => specifierToName(specifier, isTypeOnly)),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (ts.isImportEqualsDeclaration(node) &&
|
|
111
|
+
ts.isQualifiedName(node.moduleReference)) {
|
|
112
|
+
return {
|
|
113
|
+
from: node.moduleReference.left,
|
|
114
|
+
names: [{ name: node.moduleReference.right, typeOnly: node.isTypeOnly }],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
118
|
+
return {
|
|
119
|
+
from: node.expression,
|
|
120
|
+
names: [{ name: node.name, typeOnly: false }],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (ts.isBindingElement(node)) {
|
|
124
|
+
return requireBindingToImportedNames(node);
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
function specifierToName(specifier, declarationIsTypeOnly) {
|
|
129
|
+
return {
|
|
130
|
+
name: specifier.propertyName ?? specifier.name,
|
|
131
|
+
typeOnly: declarationIsTypeOnly || specifier.isTypeOnly,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function requireBindingToImportedNames(binding) {
|
|
135
|
+
const name = binding.propertyName ?? binding.name;
|
|
136
|
+
const declaration = binding.parent.parent;
|
|
137
|
+
if (!ts.isObjectBindingPattern(binding.parent) ||
|
|
138
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
139
|
+
declaration.initializer === undefined ||
|
|
140
|
+
!ts.isCallExpression(declaration.initializer) ||
|
|
141
|
+
!(ts.isIdentifier(name) || ts.isStringLiteral(name))) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const specifier = declaration.initializer.arguments.at(0);
|
|
145
|
+
if (specifier === undefined) {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
return { from: specifier, names: [{ name, typeOnly: false }] };
|
|
149
|
+
}
|
|
150
|
+
function isSdkModule(node, checker, sdk) {
|
|
151
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
152
|
+
return symbol !== undefined && symbolToTarget(symbol, checker) === sdk.symbol;
|
|
62
153
|
}
|
|
63
154
|
function nodeToChildren(node) {
|
|
64
155
|
const children = [];
|
|
@@ -77,32 +168,50 @@ function isImportBinding(node, checker) {
|
|
|
77
168
|
export function isStatic(property) {
|
|
78
169
|
return (ts.getCombinedModifierFlags(property) & ts.ModifierFlags.Static) !== 0;
|
|
79
170
|
}
|
|
80
|
-
function foundToSdkReferences(found) {
|
|
171
|
+
function foundToSdkReferences(found, targets) {
|
|
81
172
|
return found.map(({ name, expression, call, location }) => {
|
|
173
|
+
const target = targets.get(name);
|
|
174
|
+
if (target === undefined) {
|
|
175
|
+
return {
|
|
176
|
+
use: 'unregistered',
|
|
177
|
+
name,
|
|
178
|
+
message: `${name}: no registered protocol reads it`,
|
|
179
|
+
location,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
82
182
|
if (call !== undefined &&
|
|
83
183
|
ts.isDecorator(call.parent) &&
|
|
84
|
-
|
|
85
|
-
|
|
184
|
+
target.isTarget(call.parent.parent)) {
|
|
185
|
+
const decorator = {
|
|
186
|
+
use: 'decorator',
|
|
187
|
+
name,
|
|
188
|
+
call,
|
|
189
|
+
decorator: call.parent,
|
|
190
|
+
location,
|
|
191
|
+
};
|
|
192
|
+
if (target.protocol !== undefined) {
|
|
193
|
+
decorator.protocol = target.protocol;
|
|
194
|
+
}
|
|
195
|
+
return decorator;
|
|
86
196
|
}
|
|
87
197
|
return {
|
|
88
198
|
use: 'misplaced',
|
|
89
199
|
name,
|
|
90
|
-
message: misplacedToMessage(name, expression, call),
|
|
200
|
+
message: misplacedToMessage(name, target, targets, expression, call),
|
|
91
201
|
location,
|
|
92
202
|
};
|
|
93
203
|
});
|
|
94
204
|
}
|
|
95
|
-
function misplacedToMessage(name, expression, call) {
|
|
96
|
-
const target = decoratorTargets[name];
|
|
205
|
+
function misplacedToMessage(name, target, targets, expression, call) {
|
|
97
206
|
if (call !== undefined && ts.isDecorator(call.parent)) {
|
|
98
|
-
return `@${name} is on ${elementToDescription(call.parent.parent)}, so it is not read: put it on ${target.description}`;
|
|
207
|
+
return `@${name} is on ${elementToDescription(call.parent.parent, targets)}, so it is not read: put it on ${target.description}`;
|
|
99
208
|
}
|
|
100
209
|
if (ts.isDecorator(expression.parent)) {
|
|
101
210
|
return `@${name} is not called, so it is not read: write @${name}()`;
|
|
102
211
|
}
|
|
103
212
|
return `${name} is not written as a decorator, so it is not read: write @${name}() on ${target.description}`;
|
|
104
213
|
}
|
|
105
|
-
function elementToDescription(element) {
|
|
214
|
+
function elementToDescription(element, targets) {
|
|
106
215
|
if (ts.isClassExpression(element)) {
|
|
107
216
|
return 'a class expression';
|
|
108
217
|
}
|
|
@@ -115,26 +224,27 @@ function elementToDescription(element) {
|
|
|
115
224
|
if (ts.isPropertyDeclaration(element) && isStatic(element)) {
|
|
116
225
|
return 'a static property';
|
|
117
226
|
}
|
|
118
|
-
const target =
|
|
227
|
+
const target = [...targets.values()].find(({ isTarget }) => isTarget(element));
|
|
119
228
|
return target?.description ?? 'this element';
|
|
120
229
|
}
|
|
121
|
-
function
|
|
122
|
-
const sdkExports = new Map();
|
|
230
|
+
function programToSdkModule({ program, sdkEntry }, checker) {
|
|
123
231
|
const entry = program.getSourceFile(sdkEntry);
|
|
124
232
|
if (entry === undefined) {
|
|
125
|
-
return
|
|
233
|
+
return undefined;
|
|
126
234
|
}
|
|
127
|
-
const
|
|
128
|
-
if (
|
|
129
|
-
return
|
|
235
|
+
const symbol = checker.getSymbolAtLocation(entry);
|
|
236
|
+
if (symbol === undefined) {
|
|
237
|
+
return undefined;
|
|
130
238
|
}
|
|
131
|
-
|
|
239
|
+
const exports = checker.getExportsOfModule(symbol);
|
|
240
|
+
const functions = new Map();
|
|
241
|
+
for (const exported of exports) {
|
|
132
242
|
const target = symbolToTarget(exported, checker);
|
|
133
243
|
if (target.flags & ts.SymbolFlags.Function) {
|
|
134
|
-
|
|
244
|
+
functions.set(target, exported.name);
|
|
135
245
|
}
|
|
136
246
|
}
|
|
137
|
-
return
|
|
247
|
+
return { symbol, names: new Set(exports.map(({ name }) => name)), functions };
|
|
138
248
|
}
|
|
139
249
|
export function symbolToTarget(symbol, checker) {
|
|
140
250
|
if (symbol.flags & ts.SymbolFlags.Alias) {
|