@bidirekt/typescript-sdk-cli 0.2.0 → 0.4.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.
Files changed (36) hide show
  1. package/README.md +77 -37
  2. package/dist/command.js +14 -13
  3. package/dist/model/contract.js +7 -1
  4. package/dist/model/declarations-to-contract.js +97 -37
  5. package/dist/model/protocols/graphql/block-to-references.js +8 -0
  6. package/dist/model/protocols/graphql/blocks-to-merged.js +77 -0
  7. package/dist/model/protocols/graphql/contract.js +1 -0
  8. package/dist/model/protocols/graphql/field-extra.js +46 -0
  9. package/dist/model/protocols/graphql/returns-to-schema.js +77 -0
  10. package/dist/model/protocols/rest/contract.js +1 -0
  11. package/dist/model/{resources-to-merged.js → protocols/rest/resources-to-merged.js} +15 -10
  12. package/dist/model/protocols/rest/resources-to-references.js +10 -0
  13. package/dist/protocols.js +49 -0
  14. package/dist/reader/comment-tags.js +77 -37
  15. package/dist/reader/literal-options.js +8 -2
  16. package/dist/reader/program.js +1 -1
  17. package/dist/reader/project-to-contract.js +51 -23
  18. package/dist/reader/protocols/graphql/decorators-to-blocks.js +412 -0
  19. package/dist/reader/protocols/graphql/protocol-input-to-blocks.js +11 -0
  20. package/dist/reader/protocols/graphql/tags-to-blocks.js +239 -0
  21. package/dist/reader/{handler-methods.js → protocols/rest/handler-methods.js} +36 -26
  22. package/dist/reader/protocols/rest/protocol-input-to-resources.js +11 -0
  23. package/dist/reader/protocols/rest/tag-handlers.js +152 -0
  24. package/dist/reader/schema-classes.js +191 -29
  25. package/dist/reader/schema-context.js +10 -5
  26. package/dist/reader/sdk-references.js +149 -39
  27. package/dist/reader/tag-handlers.js +36 -146
  28. package/dist/reader/tag-schemas.js +82 -40
  29. package/dist/reader/type-text-to-schema-node.js +4 -1
  30. package/dist/reader/type-to-schema-node.js +60 -10
  31. package/dist/writer/contract-to-yaml.js +38 -59
  32. package/dist/writer/protocols/graphql/block-to-yaml.js +31 -0
  33. package/dist/writer/protocols/graphql/field-extra-to-yaml.js +13 -0
  34. package/dist/writer/protocols/rest/resources-to-yaml.js +36 -0
  35. package/package.json +2 -2
  36. /package/dist/model/{schema-names.js → protocols/rest/schema-names.js} +0 -0
@@ -0,0 +1,412 @@
1
+ import ts from 'typescript';
2
+ import { isDiagnostic } from "../../../model/diagnostic.js";
3
+ import { returnsToSchema } from "../../../model/protocols/graphql/returns-to-schema.js";
4
+ import { expressionToSchemaRef, expressionToUnionMembers, } from "../../expression-to-schema-node.js";
5
+ import { expressionToLiteralOptions, literalOptionToBoolean, literalOptionToName, literalOptionToString, } from "../../literal-options.js";
6
+ import { nodeToLocation } from "../../location.js";
7
+ import { fieldTarget } from "../../sdk-references.js";
8
+ import { textToPrimitive } from "../../type-text-to-schema-node.js";
9
+ export const graphqlDecoratorTargets = {
10
+ BidirektGraphqlConsumer: {
11
+ isTarget: ts.isMethodDeclaration,
12
+ description: 'a method',
13
+ },
14
+ BidirektGraphqlField: fieldTarget,
15
+ BidirektGraph: {
16
+ isTarget: ts.isClassDeclaration,
17
+ description: 'a class declaration',
18
+ },
19
+ };
20
+ const consumerOptions = [
21
+ 'provider',
22
+ 'query',
23
+ 'mutation',
24
+ 'arguments',
25
+ 'returns',
26
+ ];
27
+ const listKeys = ['list', 'nullable', 'nullableItems', 'name'];
28
+ const nullableKeys = ['nullable', 'name'];
29
+ const scalarKeys = ['scalar', 'name'];
30
+ const primitiveKeys = ['type', 'name'];
31
+ const enumKeys = ['enum', 'name'];
32
+ const unionKeys = ['union', 'name'];
33
+ const wrapperPlaces = {
34
+ nullable: '"nullable"',
35
+ list: 'the list',
36
+ };
37
+ export function sdkDecoratorsToBlocks(decorators, context) {
38
+ const read = {
39
+ blocks: [],
40
+ derived: [],
41
+ errors: [],
42
+ };
43
+ for (const decorator of decorators) {
44
+ if (decorator.name === 'BidirektGraphqlConsumer') {
45
+ consumerToRead(decorator, context, read);
46
+ }
47
+ else if (decorator.name === 'BidirektGraph') {
48
+ graphToRead(decorator, read);
49
+ }
50
+ }
51
+ return read;
52
+ }
53
+ function consumerToRead({ name, call }, context, read) {
54
+ const what = `@${name} options`;
55
+ const options = expressionToLiteralOptions(call.arguments[0], what, consumerOptions);
56
+ if (isDiagnostic(options)) {
57
+ read.errors.push(options);
58
+ return;
59
+ }
60
+ const location = nodeToLocation(call);
61
+ const errors = [];
62
+ const withoutDiagnostic = (value) => {
63
+ if (isDiagnostic(value)) {
64
+ errors.push(value);
65
+ return undefined;
66
+ }
67
+ return value;
68
+ };
69
+ const provider = withoutDiagnostic(literalOptionToString(options, 'provider') ?? {
70
+ message: `${what} must declare "provider"`,
71
+ location,
72
+ });
73
+ const operation = withoutDiagnostic(optionsToOperation(options, what, call));
74
+ const argumentsExpression = options.get('arguments');
75
+ let argumentsUse;
76
+ if (argumentsExpression !== undefined) {
77
+ argumentsUse = withoutDiagnostic(expressionToSchemaUse(argumentsExpression, context));
78
+ }
79
+ const returnsExpression = options.get('returns');
80
+ let returns;
81
+ if (returnsExpression === undefined) {
82
+ errors.push({ message: `${what} must declare "returns"`, location });
83
+ }
84
+ else {
85
+ returns = withoutDiagnostic(expressionToReturns(returnsExpression, context));
86
+ }
87
+ read.errors.push(...errors);
88
+ if (provider === undefined ||
89
+ operation === undefined ||
90
+ returnsExpression === undefined ||
91
+ returns === undefined ||
92
+ errors.length > 0) {
93
+ return;
94
+ }
95
+ consumedToRead({
96
+ provider,
97
+ ...operation,
98
+ arguments: argumentsUse,
99
+ returns,
100
+ returnsLocation: nodeToLocation(returnsExpression),
101
+ location,
102
+ }, read);
103
+ }
104
+ export function consumedToRead({ provider, kind, name, arguments: argumentsUse, returns, returnsLocation, location, }, read) {
105
+ const { schema, derived } = returnsToSchema(returns, returnsLocation);
106
+ read.derived.push(...derived);
107
+ const operation = {
108
+ kind,
109
+ name,
110
+ returns: { schema, location: returnsLocation },
111
+ location,
112
+ };
113
+ if (argumentsUse !== undefined) {
114
+ operation.arguments = argumentsUse;
115
+ }
116
+ read.blocks.push({ provider, block: { operations: [operation] } });
117
+ }
118
+ function optionsToOperation(options, what, call) {
119
+ const query = literalOptionToString(options, 'query');
120
+ const mutation = literalOptionToString(options, 'mutation');
121
+ if (isDiagnostic(query)) {
122
+ return query;
123
+ }
124
+ if (isDiagnostic(mutation)) {
125
+ return mutation;
126
+ }
127
+ if (query !== undefined && mutation !== undefined) {
128
+ return {
129
+ message: `${what} declare both "query" and "mutation": write one`,
130
+ location: nodeToLocation(call),
131
+ };
132
+ }
133
+ if (query !== undefined) {
134
+ return { kind: 'query', name: query };
135
+ }
136
+ if (mutation !== undefined) {
137
+ return { kind: 'mutation', name: mutation };
138
+ }
139
+ return {
140
+ message: `${what} must declare "query" or "mutation"`,
141
+ location: nodeToLocation(call),
142
+ };
143
+ }
144
+ function graphToRead({ name, call }, read) {
145
+ const [graph] = call.arguments;
146
+ const location = nodeToLocation(call);
147
+ if (call.arguments.length !== 1 || !ts.isStringLiteralLike(graph)) {
148
+ read.errors.push({
149
+ message: `@${name} needs the graph name as a string literal written in place: write @${name}('<name>')`,
150
+ location,
151
+ });
152
+ return;
153
+ }
154
+ if (graph.text === '') {
155
+ read.errors.push({
156
+ message: `the graph name of @${name} is empty: write @${name}('<name>')`,
157
+ location,
158
+ });
159
+ return;
160
+ }
161
+ read.blocks.push({
162
+ block: { operations: [], graph: { name: graph.text, location } },
163
+ });
164
+ }
165
+ function expressionToSchemaUse(expression, context) {
166
+ const ref = expressionToSchemaRef(expression, context);
167
+ if (isDiagnostic(ref)) {
168
+ return ref;
169
+ }
170
+ return { schema: ref.schema, location: nodeToLocation(expression) };
171
+ }
172
+ function expressionToReturns(expression, context) {
173
+ if (ts.isStringLiteralLike(expression) && expression.text.endsWith('[]')) {
174
+ const type = textToReturnedPrimitive(expression.text.slice(0, -'[]'.length));
175
+ if (type === undefined) {
176
+ return unreadableReturns(expression);
177
+ }
178
+ return { form: 'list', type, nullable: false, nullableItems: false };
179
+ }
180
+ if (ts.isArrayLiteralExpression(expression)) {
181
+ const [items] = expression.elements;
182
+ if (expression.elements.length !== 1 || ts.isSpreadElement(items)) {
183
+ return unreadableReturns(expression);
184
+ }
185
+ const type = expressionToReturnedType(items, context, 'list');
186
+ if (isDiagnostic(type)) {
187
+ return type;
188
+ }
189
+ return { form: 'list', type, nullable: false, nullableItems: false };
190
+ }
191
+ if (ts.isObjectLiteralExpression(expression)) {
192
+ const options = expressionToLiteralOptions(expression, '"returns"');
193
+ if (isDiagnostic(options)) {
194
+ return options;
195
+ }
196
+ if (options.has('list')) {
197
+ return optionsToList(options, expression, context);
198
+ }
199
+ if (options.has('nullable')) {
200
+ return optionsToNullable(options, expression, context);
201
+ }
202
+ if (options.has('scalar') && hasOnlyKeys(options, scalarKeys)) {
203
+ return optionsToNamedTypeReturns(options, expression, optionsToScalar);
204
+ }
205
+ if (options.has('type') && hasOnlyKeys(options, primitiveKeys)) {
206
+ return optionsToNamedTypeReturns(options, expression, optionsToPrimitive);
207
+ }
208
+ }
209
+ const type = expressionToReturnedType(expression, context);
210
+ if (isDiagnostic(type)) {
211
+ return type;
212
+ }
213
+ return { form: 'type', type };
214
+ }
215
+ function optionsToList(options, expression, context) {
216
+ const items = options.get('list');
217
+ if (items === undefined || !hasOnlyKeys(options, listKeys)) {
218
+ return unreadableReturns(expression);
219
+ }
220
+ const type = expressionToReturnedType(items, context, 'list');
221
+ if (isDiagnostic(type)) {
222
+ return type;
223
+ }
224
+ const nullable = literalOptionToBoolean(options, 'nullable');
225
+ if (isDiagnostic(nullable)) {
226
+ return nullable;
227
+ }
228
+ const nullableItems = literalOptionToBoolean(options, 'nullableItems');
229
+ if (isDiagnostic(nullableItems)) {
230
+ return nullableItems;
231
+ }
232
+ const name = literalOptionToName(options, 'name');
233
+ if (isDiagnostic(name)) {
234
+ return name;
235
+ }
236
+ const returns = {
237
+ form: 'list',
238
+ type,
239
+ nullable: nullable ?? false,
240
+ nullableItems: nullableItems ?? false,
241
+ };
242
+ if (name !== undefined) {
243
+ returns.name = name;
244
+ }
245
+ return returns;
246
+ }
247
+ function optionsToNullable(options, expression, context) {
248
+ const nullable = options.get('nullable');
249
+ if (nullable === undefined || !hasOnlyKeys(options, nullableKeys)) {
250
+ return unreadableReturns(expression);
251
+ }
252
+ const type = expressionToReturnedType(nullable, context, 'nullable');
253
+ if (isDiagnostic(type)) {
254
+ return type;
255
+ }
256
+ const name = literalOptionToName(options, 'name');
257
+ if (isDiagnostic(name)) {
258
+ return name;
259
+ }
260
+ const returns = { form: 'nullable', type };
261
+ if (name !== undefined) {
262
+ returns.name = name;
263
+ }
264
+ return returns;
265
+ }
266
+ function optionsToNamedTypeReturns(options, expression, readType) {
267
+ const type = readType(options, expression);
268
+ if (isDiagnostic(type)) {
269
+ return type;
270
+ }
271
+ const name = literalOptionToName(options, 'name');
272
+ if (isDiagnostic(name)) {
273
+ return name;
274
+ }
275
+ const returns = { form: 'type', type };
276
+ if (name !== undefined) {
277
+ returns.name = name;
278
+ }
279
+ return returns;
280
+ }
281
+ function optionsToPrimitive(options, expression) {
282
+ const written = options.get('type');
283
+ if (written === undefined || !ts.isStringLiteralLike(written)) {
284
+ return unreadableReturns(expression);
285
+ }
286
+ return textToReturnedPrimitive(written.text) ?? unreadableReturns(expression);
287
+ }
288
+ function expressionToReturnedType(expression, context, wrapper) {
289
+ if (ts.isIdentifier(expression) ||
290
+ ts.isPropertyAccessExpression(expression)) {
291
+ const ref = expressionToSchemaRef(expression, context);
292
+ if (isDiagnostic(ref)) {
293
+ return ref;
294
+ }
295
+ return { kind: 'schema', schema: ref.schema };
296
+ }
297
+ if (ts.isStringLiteralLike(expression)) {
298
+ return (textToReturnedPrimitive(expression.text) ?? unreadableReturns(expression));
299
+ }
300
+ if (!ts.isObjectLiteralExpression(expression)) {
301
+ return unreadableReturns(expression);
302
+ }
303
+ const options = expressionToLiteralOptions(expression, '"returns"');
304
+ if (isDiagnostic(options)) {
305
+ return options;
306
+ }
307
+ if (options.has('scalar') && hasOnlyKeys(options, scalarKeys)) {
308
+ return optionsToScalar(options, expression, wrapper);
309
+ }
310
+ if (options.has('enum') && hasOnlyKeys(options, enumKeys)) {
311
+ return optionsToEnum(options, expression);
312
+ }
313
+ if (options.has('union') && hasOnlyKeys(options, unionKeys)) {
314
+ return optionsToUnion(options, expression, context);
315
+ }
316
+ return unreadableReturns(expression);
317
+ }
318
+ function optionsToScalar(options, expression, wrapper) {
319
+ const scalar = literalOptionToName(options, 'scalar');
320
+ if (isDiagnostic(scalar)) {
321
+ return scalar;
322
+ }
323
+ if (scalar === undefined) {
324
+ return unreadableReturns(expression);
325
+ }
326
+ if (wrapper === undefined || !options.has('name')) {
327
+ return { kind: 'scalar', scalar };
328
+ }
329
+ const name = literalOptionToName(options, 'name');
330
+ if (isDiagnostic(name)) {
331
+ return name;
332
+ }
333
+ return {
334
+ message: `a scalar's "name" goes on ${wrapperPlaces[wrapper]}: write { ${wrapper}: { scalar: '${scalar}' }, name: '${name}' }`,
335
+ location: nodeToLocation(expression),
336
+ };
337
+ }
338
+ function optionsToEnum(options, expression) {
339
+ const written = options.get('enum') ?? expression;
340
+ const members = expressionToStringLiterals(written);
341
+ if (members === undefined) {
342
+ return {
343
+ message: '"enum" must be an array of string literals written in place',
344
+ location: nodeToLocation(written),
345
+ };
346
+ }
347
+ const name = literalOptionToName(options, 'name');
348
+ if (isDiagnostic(name)) {
349
+ return name;
350
+ }
351
+ if (name === undefined) {
352
+ return {
353
+ message: `an enum in "returns" needs a name: write { enum: [...], name: '<name>' }`,
354
+ location: nodeToLocation(expression),
355
+ };
356
+ }
357
+ return {
358
+ kind: 'schema',
359
+ schema: name,
360
+ declared: { type: 'enum', members },
361
+ };
362
+ }
363
+ function optionsToUnion(options, expression, context) {
364
+ const members = expressionToUnionMembers(options.get('union'), '"union"', context);
365
+ if (isDiagnostic(members)) {
366
+ return members;
367
+ }
368
+ const name = literalOptionToName(options, 'name');
369
+ if (isDiagnostic(name)) {
370
+ return name;
371
+ }
372
+ if (name === undefined) {
373
+ return {
374
+ message: `a union in "returns" needs a name: write { union: {...}, name: '<name>' }`,
375
+ location: nodeToLocation(expression),
376
+ };
377
+ }
378
+ return {
379
+ kind: 'schema',
380
+ schema: name,
381
+ declared: { type: 'union', members },
382
+ };
383
+ }
384
+ function expressionToStringLiterals(expression) {
385
+ if (!ts.isArrayLiteralExpression(expression)) {
386
+ return undefined;
387
+ }
388
+ const members = [];
389
+ for (const element of expression.elements) {
390
+ if (!ts.isStringLiteralLike(element)) {
391
+ return undefined;
392
+ }
393
+ members.push(element.text);
394
+ }
395
+ return members;
396
+ }
397
+ function textToReturnedPrimitive(text) {
398
+ const type = textToPrimitive(text);
399
+ if (type === undefined) {
400
+ return undefined;
401
+ }
402
+ return { kind: 'primitive', type };
403
+ }
404
+ function hasOnlyKeys(options, keys) {
405
+ return [...options.keys()].every((key) => keys.includes(key));
406
+ }
407
+ function unreadableReturns(expression) {
408
+ return {
409
+ message: `cannot read "${expression.getText()}" as returns: write a @BidirektSchema class, a primitive such as 'boolean', { type: '<primitive>', name: '<name>' }, [Schema], { list: Schema }, { nullable: Schema }, { scalar: '<name>' }, { enum: [...], name: '<name>' } or { union: {...}, name: '<name>' }`,
410
+ location: nodeToLocation(expression),
411
+ };
412
+ }
@@ -0,0 +1,11 @@
1
+ import { sdkDecoratorsToBlocks } from "./decorators-to-blocks.js";
2
+ import { taggedCommentsToBlocks } from "./tags-to-blocks.js";
3
+ export function protocolInputToBlocks(input) {
4
+ const decorated = sdkDecoratorsToBlocks(input.decorators, input.context);
5
+ const tagged = taggedCommentsToBlocks(input.comments);
6
+ return {
7
+ blocks: [...decorated.blocks, ...tagged.blocks],
8
+ derived: [...decorated.derived, ...tagged.derived],
9
+ errors: [...decorated.errors, ...tagged.errors],
10
+ };
11
+ }
@@ -0,0 +1,239 @@
1
+ import { isDiagnostic } from "../../../model/diagnostic.js";
2
+ import { isHandlerTag, tagToText } from "../../comment-tags.js";
3
+ import { valueToUnionMembers } from "../../tag-schemas.js";
4
+ import { isSchemaName, textToPrimitive, } from "../../type-text-to-schema-node.js";
5
+ import { consumedToRead } from "./decorators-to-blocks.js";
6
+ const consumesUsage = '@bidirekt graphql consumes <provider> query|mutation <name>';
7
+ const operationKinds = ['query', 'mutation'];
8
+ const returnsPattern = /^(union=\{[^{}]*\}|[^\s?[\]]+)(\?)?(?:(\[\])(\?)?)?(?: as ([^\s?[\]]+))?$/;
9
+ const returnsUsage = '"<type>", "<type>?", "<type>[]", "<type>[]?", "<type>?[]" or "<type>?[]?", each with an optional "as <name>", where <type> is a schema, a primitive, scalar:<name>, enum:<A>|<B> or union={<key>: <schema>, ...}';
10
+ export function taggedCommentsToBlocks(comments) {
11
+ const read = {
12
+ blocks: [],
13
+ derived: [],
14
+ errors: [],
15
+ };
16
+ for (const { tags } of comments) {
17
+ for (const tag of tags.filter(({ keyword }) => keyword === 'graph')) {
18
+ graphTagToRead(tag, read);
19
+ }
20
+ const handler = tags.find(isHandlerTag);
21
+ const uses = tags.filter(({ keyword }) => keyword === 'arguments' || keyword === 'returns');
22
+ if (handler === undefined) {
23
+ read.errors.push(...uses.map((tag) => ({
24
+ message: `"@bidirekt ${tag.keyword}" needs a "@bidirekt graphql consumes" tag in the same comment`,
25
+ location: tag.location,
26
+ })));
27
+ continue;
28
+ }
29
+ tagsToOperation(handler, uses, read);
30
+ }
31
+ return read;
32
+ }
33
+ function graphTagToRead(tag, read) {
34
+ if (tag.error !== undefined) {
35
+ read.errors.push(tag.error);
36
+ return;
37
+ }
38
+ const [name] = tag.arguments;
39
+ if (tag.arguments.length !== 1) {
40
+ read.errors.push({
41
+ message: `cannot read "${tagToText(tag)}": write "@bidirekt graph <name>"`,
42
+ location: tag.location,
43
+ });
44
+ return;
45
+ }
46
+ read.blocks.push({
47
+ block: { operations: [], graph: { name, location: tag.location } },
48
+ });
49
+ }
50
+ function tagsToOperation(handler, uses, read) {
51
+ const errors = [];
52
+ const consumed = handlerToConsumed(handler);
53
+ if (isDiagnostic(consumed)) {
54
+ errors.push(consumed);
55
+ }
56
+ let argumentsUse;
57
+ let returnsTag;
58
+ for (const tag of uses) {
59
+ if (tag.error !== undefined) {
60
+ errors.push(tag.error);
61
+ continue;
62
+ }
63
+ if (uses.slice(0, uses.indexOf(tag)).some(isSameKeyword(tag))) {
64
+ errors.push({
65
+ message: `"@bidirekt ${tag.keyword}" is declared more than once in this comment: keep one`,
66
+ location: tag.location,
67
+ });
68
+ continue;
69
+ }
70
+ if (tag.keyword === 'returns') {
71
+ returnsTag = tag;
72
+ continue;
73
+ }
74
+ const use = tagToArguments(tag);
75
+ if (isDiagnostic(use)) {
76
+ errors.push(use);
77
+ continue;
78
+ }
79
+ argumentsUse = use;
80
+ }
81
+ let returns = {
82
+ message: `"${tagToText(handler)}" needs a "@bidirekt returns <type>" tag in the same comment`,
83
+ location: handler.location,
84
+ };
85
+ if (returnsTag !== undefined) {
86
+ returns = tagToReturns(returnsTag);
87
+ }
88
+ if (isDiagnostic(returns)) {
89
+ errors.push(returns);
90
+ }
91
+ read.errors.push(...errors);
92
+ if (isDiagnostic(consumed) ||
93
+ isDiagnostic(returns) ||
94
+ returnsTag === undefined ||
95
+ errors.length > 0) {
96
+ return;
97
+ }
98
+ consumedToRead({
99
+ ...consumed,
100
+ arguments: argumentsUse,
101
+ returns,
102
+ returnsLocation: returnsTag.location,
103
+ location: handler.location,
104
+ }, read);
105
+ }
106
+ function isSameKeyword(tag) {
107
+ return (other) => other.keyword === tag.keyword;
108
+ }
109
+ function handlerToConsumed(handler) {
110
+ if (handler.error !== undefined) {
111
+ return handler.error;
112
+ }
113
+ const [form, provider, kind, name] = handler.arguments;
114
+ const operationKind = operationKinds.find((known) => known === kind);
115
+ if (form !== 'consumes' ||
116
+ handler.arguments.length !== 4 ||
117
+ operationKind === undefined) {
118
+ return {
119
+ message: `cannot read "${tagToText(handler)}": write "${consumesUsage}"`,
120
+ location: handler.location,
121
+ };
122
+ }
123
+ return { provider, kind: operationKind, name };
124
+ }
125
+ function tagToArguments(tag) {
126
+ const [schema] = tag.arguments;
127
+ if (tag.arguments.length !== 1 || !isSchemaName(schema)) {
128
+ return {
129
+ message: `cannot read "${tagToText(tag)}": write "@bidirekt arguments <schema>"`,
130
+ location: tag.location,
131
+ };
132
+ }
133
+ return { schema, location: tag.location };
134
+ }
135
+ function tagToReturns(tag) {
136
+ const text = tag.arguments.join(' ');
137
+ if (text === '') {
138
+ return {
139
+ message: `cannot read "${tagToText(tag)}": write "@bidirekt returns <type>"`,
140
+ location: tag.location,
141
+ };
142
+ }
143
+ const match = returnsPattern.exec(text);
144
+ if (match === null) {
145
+ return unreadableReturns(text, tag.location);
146
+ }
147
+ const [, written, typeMark, listMark, listMarkAfter] = match;
148
+ const typeNullable = typeMark === '?';
149
+ const isList = listMark === '[]';
150
+ const name = / as (\S+)$/.exec(text)?.[1];
151
+ const type = textToReturnedType(written, name, text, tag.location);
152
+ if (isDiagnostic(type)) {
153
+ return type;
154
+ }
155
+ const namesType = type.kind === 'schema' && type.declared !== undefined;
156
+ if (type.kind === 'schema' &&
157
+ !namesType &&
158
+ name !== undefined &&
159
+ !isList &&
160
+ !typeNullable) {
161
+ return {
162
+ message: `cannot read "${text}" as returns: a schema is returned by its own name, write "${written}"`,
163
+ location: tag.location,
164
+ };
165
+ }
166
+ let returns = { form: 'type', type };
167
+ if (isList) {
168
+ returns = {
169
+ form: 'list',
170
+ type,
171
+ nullable: listMarkAfter === '?',
172
+ nullableItems: typeNullable,
173
+ };
174
+ }
175
+ else if (typeNullable) {
176
+ returns = { form: 'nullable', type };
177
+ }
178
+ if (name !== undefined && !namesType) {
179
+ returns.name = name;
180
+ }
181
+ return returns;
182
+ }
183
+ function textToReturnedType(written, name, text, location) {
184
+ const primitive = textToPrimitive(written);
185
+ if (primitive !== undefined) {
186
+ return { kind: 'primitive', type: primitive };
187
+ }
188
+ if (written.startsWith('scalar:')) {
189
+ const scalar = written.slice('scalar:'.length);
190
+ if (!isSchemaName(scalar)) {
191
+ return unreadableReturns(text, location);
192
+ }
193
+ return { kind: 'scalar', scalar };
194
+ }
195
+ if (written.startsWith('enum:')) {
196
+ const members = written.slice('enum:'.length).split('|');
197
+ if (members.some((member) => member === '')) {
198
+ return unreadableReturns(text, location);
199
+ }
200
+ if (name === undefined) {
201
+ return {
202
+ message: `cannot read "${text}" as returns: an enum needs a name, write "${text} as <name>"`,
203
+ location,
204
+ };
205
+ }
206
+ return {
207
+ kind: 'schema',
208
+ schema: name,
209
+ declared: { type: 'enum', members },
210
+ };
211
+ }
212
+ if (written.startsWith('union=')) {
213
+ const members = valueToUnionMembers(written.slice('union='.length));
214
+ if (members === undefined) {
215
+ return unreadableReturns(text, location);
216
+ }
217
+ if (name === undefined) {
218
+ return {
219
+ message: `cannot read "${text}" as returns: a union needs a name, write "${text} as <name>"`,
220
+ location,
221
+ };
222
+ }
223
+ return {
224
+ kind: 'schema',
225
+ schema: name,
226
+ declared: { type: 'union', members },
227
+ };
228
+ }
229
+ if (!isSchemaName(written)) {
230
+ return unreadableReturns(text, location);
231
+ }
232
+ return { kind: 'schema', schema: written };
233
+ }
234
+ function unreadableReturns(text, location) {
235
+ return {
236
+ message: `cannot read "${text}" as returns: write ${returnsUsage}`,
237
+ location,
238
+ };
239
+ }