@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.
Files changed (37) hide show
  1. package/README.md +73 -41
  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/description.js +16 -0
  6. package/dist/model/protocols/graphql/block-to-references.js +8 -0
  7. package/dist/model/protocols/graphql/blocks-to-merged.js +77 -0
  8. package/dist/model/protocols/graphql/contract.js +1 -0
  9. package/dist/model/protocols/graphql/field-extra.js +46 -0
  10. package/dist/model/protocols/graphql/returns-to-schema.js +77 -0
  11. package/dist/model/protocols/rest/contract.js +1 -0
  12. package/dist/model/{resources-to-merged.js → protocols/rest/resources-to-merged.js} +15 -10
  13. package/dist/model/protocols/rest/resources-to-references.js +10 -0
  14. package/dist/protocols.js +49 -0
  15. package/dist/reader/comment-tags.js +77 -37
  16. package/dist/reader/literal-options.js +17 -2
  17. package/dist/reader/program.js +1 -1
  18. package/dist/reader/project-to-contract.js +51 -23
  19. package/dist/reader/protocols/graphql/decorators-to-blocks.js +401 -0
  20. package/dist/reader/protocols/graphql/protocol-input-to-blocks.js +11 -0
  21. package/dist/reader/protocols/graphql/tags-to-blocks.js +239 -0
  22. package/dist/reader/{handler-methods.js → protocols/rest/handler-methods.js} +36 -26
  23. package/dist/reader/protocols/rest/protocol-input-to-resources.js +11 -0
  24. package/dist/reader/protocols/rest/tag-handlers.js +152 -0
  25. package/dist/reader/schema-classes.js +193 -31
  26. package/dist/reader/schema-context.js +10 -5
  27. package/dist/reader/sdk-references.js +149 -39
  28. package/dist/reader/tag-handlers.js +36 -146
  29. package/dist/reader/tag-schemas.js +93 -43
  30. package/dist/reader/type-text-to-schema-node.js +4 -1
  31. package/dist/reader/type-to-schema-node.js +60 -10
  32. package/dist/writer/contract-to-yaml.js +38 -59
  33. package/dist/writer/protocols/graphql/block-to-yaml.js +31 -0
  34. package/dist/writer/protocols/graphql/field-extra-to-yaml.js +13 -0
  35. package/dist/writer/protocols/rest/resources-to-yaml.js +36 -0
  36. package/package.json +2 -2
  37. /package/dist/model/{schema-names.js → protocols/rest/schema-names.js} +0 -0
@@ -1,40 +1,28 @@
1
1
  import { isDiagnostic } from "../model/diagnostic.js";
2
- import { listToNamedSchema } from "../model/schema-names.js";
3
2
  import { isHandlerTag, tagToText } from "./comment-tags.js";
4
- import { declaredToRead, isHandlerDecorator, methodToUppercaseDiagnostic, statusToNumberDiagnostic, } from "./handler-methods.js";
5
- import { fieldOptionsToNode } from "./schema-classes.js";
3
+ import { namesToChoice } from "./literal-options.js";
4
+ import { fieldOptionsToLeaf, fieldOptionsToProperty, withLeaf, } from "./schema-classes.js";
6
5
  import { argumentsToOptions, blockFieldKeys } from "./tag-schemas.js";
7
- const endpointForms = {
8
- provides: { usage: '@bidirekt provides <METHOD> <path>', arguments: 2 },
9
- consumes: {
10
- usage: '@bidirekt consumes <provider> <METHOD> <path>',
11
- arguments: 3,
12
- },
13
- };
14
- const bodyPattern = /^([^\s[\]]+)(?:\[\](?: as [^\s[\]]+)?)?$/;
15
- export function taggedCommentsToResources(comments, references) {
6
+ export function taggedCommentsToHandlerComments(comments, decorators, protocols) {
16
7
  const read = {
17
- provides: [],
18
- consumes: [],
8
+ comments: new Map(),
19
9
  schemas: [],
20
10
  errors: [],
21
11
  };
22
- const decorated = Map.groupBy(references.filter(isHandlerDecorator), (reference) => reference.decorator.parent);
12
+ const decorated = Map.groupBy(decorators, (reference) => reference.decorator.parent);
23
13
  const handled = new Set();
24
14
  for (const { element, tags } of comments) {
25
15
  const handlers = tags.filter(isHandlerTag);
26
- const bodies = tags.filter(({ keyword }) => keyword === 'request' || keyword === 'response');
16
+ const protocolTags = tags.filter((tag) => tag.protocol !== undefined && !isHandlerTag(tag));
27
17
  if (handlers.length === 0) {
28
- read.errors.push(...bodies.map((tag) => ({
29
- message: `"@bidirekt ${tag.keyword}" needs a "@bidirekt provides" or "@bidirekt consumes" tag in the same comment`,
30
- location: tag.location,
31
- })));
18
+ tagsToProtocolComments(element, protocolTags, read);
32
19
  continue;
33
20
  }
34
21
  const [handler, ...extra] = handlers;
35
22
  if (handled.has(element)) {
23
+ const handlerTags = protocols.map(({ name }) => `"@bidirekt ${name}"`);
36
24
  read.errors.push({
37
- message: 'a handler is declared more than once on this element: keep one "@bidirekt provides" or "@bidirekt consumes" comment',
25
+ message: `a handler is declared more than once on this element: keep one ${namesToChoice(handlerTags)} comment`,
38
26
  location: handler.location,
39
27
  });
40
28
  continue;
@@ -44,9 +32,9 @@ export function taggedCommentsToResources(comments, references) {
44
32
  message: `a comment declares one handler: move "${tagToText(tag)}" to its own comment`,
45
33
  location: tag.location,
46
34
  })));
47
- const decorators = decorated.get(element) ?? [];
48
- if (decorators.length > 0) {
49
- read.errors.push(...decorators.flatMap(({ name, location }) => {
35
+ const onElement = decorated.get(element) ?? [];
36
+ if (onElement.length > 0) {
37
+ read.errors.push(...onElement.flatMap(({ name, location }) => {
50
38
  const message = `@${name} and "${tagToText(handler)}" are both on this handler: keep one`;
51
39
  return [
52
40
  { message, location },
@@ -55,12 +43,22 @@ export function taggedCommentsToResources(comments, references) {
55
43
  }));
56
44
  continue;
57
45
  }
58
- tagsToResource(handler, bodies, read);
59
- read.schemas.push(...tagsToBlockSchemas(tags, read.errors));
46
+ tagsToProtocolComments(element, [handler, ...protocolTags], read);
47
+ read.schemas.push(...tagsToBlockSchemas(tags, read.errors, protocols));
60
48
  }
61
49
  return read;
62
50
  }
63
- function tagsToBlockSchemas(tags, errors) {
51
+ function tagsToProtocolComments(element, tags, read) {
52
+ for (const [protocol, own] of Map.groupBy(tags, (tag) => tag.protocol)) {
53
+ if (protocol === undefined) {
54
+ continue;
55
+ }
56
+ const comments = read.comments.get(protocol) ?? [];
57
+ comments.push({ element, tags: own });
58
+ read.comments.set(protocol, comments);
59
+ }
60
+ }
61
+ function tagsToBlockSchemas(tags, errors, protocols) {
64
62
  const blocks = [];
65
63
  let fields;
66
64
  for (const tag of tags) {
@@ -84,7 +82,7 @@ function tagsToBlockSchemas(tags, errors) {
84
82
  });
85
83
  continue;
86
84
  }
87
- const property = tagToBlockProperty(tag);
85
+ const property = tagToBlockProperty(tag, protocols);
88
86
  if (isDiagnostic(property)) {
89
87
  errors.push(property);
90
88
  fields.hasUnreadField = true;
@@ -119,8 +117,8 @@ function tagToBlockSchemaName(tag) {
119
117
  }
120
118
  return name;
121
119
  }
122
- function tagToBlockProperty(tag) {
123
- const [name, ...rest] = tag.arguments;
120
+ function tagToBlockProperty(tag, protocols) {
121
+ const [name, ...remaining] = tag.arguments;
124
122
  if (tag.error !== undefined) {
125
123
  return tag.error;
126
124
  }
@@ -130,128 +128,20 @@ function tagToBlockProperty(tag) {
130
128
  location: tag.location,
131
129
  };
132
130
  }
133
- const options = argumentsToOptions(rest, tag, blockFieldKeys);
131
+ const options = argumentsToOptions(remaining, tag, blockFieldKeys, protocols);
134
132
  if (isDiagnostic(options)) {
135
133
  return options;
136
134
  }
137
- if (options.declared === undefined) {
138
- return {
139
- message: `field "${name}" in a handler's comment needs its type: declare it with "type=<type>"`,
140
- location: tag.location,
141
- };
142
- }
143
- return {
144
- name,
145
- node: fieldOptionsToNode(options.declared, options),
146
- location: tag.location,
147
- };
148
- }
149
- function tagsToResource(handler, bodies, read) {
150
- const errors = [];
151
- const derived = [];
152
- const endpoint = handlerToEndpoint(handler);
153
- if (isDiagnostic(endpoint)) {
154
- errors.push(endpoint);
155
- }
156
- let request;
157
- const responses = [];
158
- for (const tag of bodies) {
159
- if (tag.error !== undefined) {
160
- errors.push(tag.error);
161
- continue;
162
- }
163
- if (tag.keyword === 'request') {
164
- if (request !== undefined) {
165
- errors.push({
166
- message: `"@bidirekt request" is declared more than once in this comment: keep one`,
167
- location: tag.location,
168
- });
169
- continue;
170
- }
171
- const body = argumentsToBody(tag.arguments, tag.location, derived);
172
- if (isDiagnostic(body)) {
173
- errors.push(body);
174
- continue;
175
- }
176
- request = body;
177
- continue;
178
- }
179
- const response = tagToResponse(tag, responses, derived);
180
- if (isDiagnostic(response)) {
181
- errors.push(response);
182
- continue;
183
- }
184
- responses.push(response);
185
- }
186
- read.errors.push(...errors);
187
- if (isDiagnostic(endpoint) || errors.length > 0) {
188
- return;
189
- }
190
- declaredToRead({ ...endpoint, request, responses, location: handler.location }, derived, read);
191
- }
192
- function handlerToEndpoint(handler) {
193
- const { usage, arguments: count } = endpointForms[handler.keyword];
194
- if (handler.error !== undefined) {
195
- return handler.error;
196
- }
197
- if (handler.arguments.length !== count) {
198
- return {
199
- message: `cannot read "${tagToText(handler)}": write "${usage}"`,
200
- location: handler.location,
201
- };
202
- }
203
- const [method, path] = handler.arguments.slice(-2);
204
- const uppercase = methodToUppercaseDiagnostic(method, handler.location);
205
- if (uppercase !== undefined) {
206
- return uppercase;
207
- }
208
- const endpoint = { method, path };
209
- if (handler.keyword === 'consumes') {
210
- endpoint.provider = handler.arguments[0];
135
+ const leaf = fieldOptionsToLeaf(options);
136
+ let read = leaf;
137
+ if (options.declared !== undefined) {
138
+ read = withLeaf(options.declared, leaf);
211
139
  }
212
- return endpoint;
213
- }
214
- function tagToResponse(tag, declared, derived) {
215
- if (tag.arguments.length === 0) {
140
+ if (read === undefined) {
216
141
  return {
217
- message: `cannot read "${tagToText(tag)}": write "@bidirekt response <status> [schema]"`,
218
- location: tag.location,
219
- };
220
- }
221
- const [status, ...body] = tag.arguments;
222
- const number = statusToNumberDiagnostic(status, tag.location);
223
- if (number !== undefined) {
224
- return number;
225
- }
226
- if (declared.some((response) => response.status === Number(status))) {
227
- return {
228
- message: `response status "${status}" is declared more than once in this comment: keep one`,
142
+ message: `field "${name}" in a handler's comment needs its type: declare it with "type=<type>"`,
229
143
  location: tag.location,
230
144
  };
231
145
  }
232
- const read = argumentsToBody(body, tag.location, derived);
233
- if (isDiagnostic(read)) {
234
- return read;
235
- }
236
- return { ...read, status: Number(status) };
237
- }
238
- function argumentsToBody(body, location, derived) {
239
- if (body.length === 0) {
240
- return { schema: null, location };
241
- }
242
- const text = body.join(' ');
243
- const match = bodyPattern.exec(text);
244
- if (match === null) {
245
- return {
246
- message: `cannot read "${text}" as a body: write "<schema>", "<schema>[]" or "<schema>[] as <name>"`,
247
- location,
248
- };
249
- }
250
- const [, schema] = match;
251
- if (!text.includes('[]')) {
252
- return { schema, location };
253
- }
254
- const list = listToNamedSchema(schema, / as (\S+)$/.exec(text)?.[1], location);
255
- derived.push(list);
256
- return { schema: list.name, location };
146
+ return fieldOptionsToProperty(name, read, options, tag.location);
257
147
  }
@@ -1,8 +1,9 @@
1
1
  import ts from 'typescript';
2
+ import { descriptionToDiagnostic } from "../model/description.js";
2
3
  import { isDiagnostic } from "../model/diagnostic.js";
3
- import { argumentToOption, isHandlerTag } from "./comment-tags.js";
4
- import { namesToChoice } from "./literal-options.js";
5
- import { declarationToDescription } from "./schema-classes.js";
4
+ import { argumentToOption, isHandlerTag, tagToLabel } from "./comment-tags.js";
5
+ import { hintToSuffix, namesToChoice } from "./literal-options.js";
6
+ import { declarationToDescription, optionToOwningProtocol, } from "./schema-classes.js";
6
7
  import { isObjectDeclaration, isShadowedByScript, memberToOwner, } from "./schema-context.js";
7
8
  import { isStatic } from "./sdk-references.js";
8
9
  import { isSchemaName, tagTypeTextToSchemaNode, tagTypeTextUsage, } from "./type-text-to-schema-node.js";
@@ -17,28 +18,31 @@ const optionUsages = {
17
18
  };
18
19
  const fieldKeys = Object.keys(optionUsages);
19
20
  export const blockFieldKeys = fieldKeys.filter((key) => key !== 'name');
21
+ const schemaKeys = ['name', 'union'];
22
+ const enumSchemaKeys = ['name'];
20
23
  const unionPattern = /^\{([^{}]*)\}$/;
21
24
  const unionMemberPattern = /^([^\s:,]+)\s*:\s*([^\s:,]+)$/;
22
- export function taggedCommentsToSchemaTags(comments, context) {
25
+ export function taggedCommentsToSchemaTags(comments, context, protocols) {
23
26
  const read = {
24
27
  schemas: new Set(),
28
+ unions: new Map(),
25
29
  fields: new Map(),
26
30
  malformedFields: new Set(),
27
31
  errors: [],
28
32
  warnings: [],
29
33
  };
30
34
  const tagged = { schemas: new Set(), fields: new Set() };
31
- for (const { element, tags } of comments) {
35
+ for (const { element, tags, hasUnknownTag = false } of comments) {
32
36
  if (tags.some(isHandlerTag) || isDeclaredByScript(element, context)) {
33
37
  continue;
34
38
  }
35
39
  const schemaTag = tagsToSingle(tags, 'schema', read);
36
40
  if (schemaTag !== undefined) {
37
- schemaTagToName(schemaTag, element, context, read, tagged.schemas);
41
+ schemaTagToName(schemaTag, element, context, read, tagged.schemas, hasUnknownTag);
38
42
  }
39
43
  const fieldTag = tagsToSingle(tags, 'field', read);
40
44
  if (fieldTag !== undefined) {
41
- fieldTagToOptions(fieldTag, element, read, tagged.fields);
45
+ fieldTagToOptions(fieldTag, element, read, tagged.fields, protocols, hasUnknownTag);
42
46
  }
43
47
  }
44
48
  return read;
@@ -61,7 +65,7 @@ function tagsToSingle(tags, keyword, read) {
61
65
  })));
62
66
  return tag;
63
67
  }
64
- function schemaTagToName(tag, element, context, read, taggedSchemas) {
68
+ function schemaTagToName(tag, element, context, read, taggedSchemas, hasUnknownTag) {
65
69
  if (ts.isJSDocTypedefTag(element) || ts.isJSDocCallbackTag(element)) {
66
70
  read.warnings.push({
67
71
  message: `"@bidirekt schema" is on a @${element.tagName.text}, so it is not read: declare the schema as a class with "@bidirekt schema"`,
@@ -70,10 +74,7 @@ function schemaTagToName(tag, element, context, read, taggedSchemas) {
70
74
  return;
71
75
  }
72
76
  if (!ts.isEnumDeclaration(element) && !isObjectDeclaration(element)) {
73
- read.warnings.push({
74
- message: '"@bidirekt schema" is not above an enum, a class, an interface or an object type, so it is not read: put it above one of them',
75
- location: tag.location,
76
- });
77
+ warnMisplaced(read, hasUnknownTag, '"@bidirekt schema" is not above an enum, a class, an interface or an object type, so it is not read: put it above one of them', tag);
77
78
  return;
78
79
  }
79
80
  if (ts.isTypeAliasDeclaration(element) &&
@@ -97,16 +98,16 @@ function schemaTagToName(tag, element, context, read, taggedSchemas) {
97
98
  taggedSchemas.add(tagged);
98
99
  read.schemas.add(element);
99
100
  }
100
- const declared = tagToName(tag);
101
- if (isDiagnostic(declared)) {
102
- read.errors.push(declared);
101
+ const options = argumentsToOptions(tag.arguments, tag, elementToSchemaKeys(element));
102
+ if (isDiagnostic(options)) {
103
+ read.errors.push(options);
103
104
  return;
104
105
  }
105
106
  if (ts.isEnumDeclaration(element)) {
106
- enumTagToSchema(tag, element, declared, context, read, taggedSchemas);
107
+ enumTagToSchema(tag, element, options.name, context, read, taggedSchemas);
107
108
  return;
108
109
  }
109
- const name = declared ?? element.name?.text;
110
+ const name = options.name ?? element.name?.text;
110
111
  if (name === undefined) {
111
112
  read.errors.push({
112
113
  message: 'cannot name an anonymous class: declare it with "@bidirekt schema name=<name>"',
@@ -115,6 +116,18 @@ function schemaTagToName(tag, element, context, read, taggedSchemas) {
115
116
  return;
116
117
  }
117
118
  context.schemaDeclarations.set(element, name);
119
+ if (options.declared?.type === 'union') {
120
+ read.unions.set(element, {
121
+ members: options.declared.members,
122
+ location: tag.location,
123
+ });
124
+ }
125
+ }
126
+ function elementToSchemaKeys(element) {
127
+ if (ts.isEnumDeclaration(element)) {
128
+ return enumSchemaKeys;
129
+ }
130
+ return schemaKeys;
118
131
  }
119
132
  function enumTagToSchema(tag, element, declared, context, read, taggedSchemas) {
120
133
  const symbol = context.checker.getSymbolAtLocation(element.name);
@@ -140,25 +153,22 @@ function enumTagToSchema(tag, element, declared, context, read, taggedSchemas) {
140
153
  });
141
154
  }
142
155
  }
156
+ // An unknown tag can be the handler of its comment, whose schema and field tags are then the handler's.
157
+ function warnMisplaced(read, hasUnknownTag, message, tag) {
158
+ if (hasUnknownTag) {
159
+ return;
160
+ }
161
+ read.warnings.push({ message, location: tag.location });
162
+ }
143
163
  function declarationToTagged(declaration, context) {
144
164
  if (!ts.isInterfaceDeclaration(declaration)) {
145
165
  return declaration;
146
166
  }
147
167
  return context.checker.getSymbolAtLocation(declaration.name) ?? declaration;
148
168
  }
149
- function tagToName(tag) {
150
- const options = argumentsToOptions(tag.arguments, tag, ['name']);
151
- if (isDiagnostic(options)) {
152
- return options;
153
- }
154
- return options.name;
155
- }
156
- function fieldTagToOptions(tag, element, read, taggedFields) {
169
+ function fieldTagToOptions(tag, element, read, taggedFields, protocols, hasUnknownTag) {
157
170
  if (!ts.isPropertyDeclaration(element) && !ts.isPropertySignature(element)) {
158
- read.warnings.push({
159
- message: '"@bidirekt field" is not above a property, so it is not read: put it above a property of a class, an interface or an object type',
160
- location: tag.location,
161
- });
171
+ warnMisplaced(read, hasUnknownTag, '"@bidirekt field" is not above a property, so it is not read: put it above a property of a class, an interface or an object type', tag);
162
172
  return;
163
173
  }
164
174
  if (isStatic(element)) {
@@ -176,39 +186,56 @@ function fieldTagToOptions(tag, element, read, taggedFields) {
176
186
  return;
177
187
  }
178
188
  taggedFields.add(element);
179
- const options = argumentsToOptions(tag.arguments, tag, fieldKeys);
189
+ const options = argumentsToOptions(tag.arguments, tag, fieldKeys, protocols);
180
190
  if (isDiagnostic(options)) {
181
191
  read.errors.push(options);
182
192
  read.malformedFields.add(element);
183
193
  return;
184
194
  }
185
- read.fields.set(element, { options, location: tag.location });
195
+ read.fields.set(element, {
196
+ label: tagToLabel(tag),
197
+ options,
198
+ location: tag.location,
199
+ });
200
+ }
201
+ function tagToVocabulary(tag, protocols) {
202
+ return protocols.find(({ name }) => name === tag.vocabulary)?.vocabulary;
186
203
  }
187
- export function argumentsToOptions(tagArguments, tag, keys) {
204
+ export function argumentsToOptions(tagArguments, tag, keys, protocols = []) {
188
205
  if (tag.error !== undefined) {
189
206
  return tag.error;
190
207
  }
191
208
  const options = {};
192
209
  const seen = new Set();
210
+ const vocabulary = tagToVocabulary(tag, protocols);
211
+ const known = [...keys, ...(vocabulary?.fieldOptions ?? [])];
212
+ const vocabularyOptions = {};
193
213
  for (const argument of tagArguments) {
194
214
  const option = argumentToOption(argument);
195
215
  const key = option?.key ?? '';
196
216
  const fail = (usage) => ({
197
- message: `cannot read "${argument}" in "@bidirekt ${tag.keyword}": write ${usage}`,
217
+ message: `cannot read "${argument}" in "${tagToLabel(tag)}": write ${usage}`,
198
218
  location: tag.location,
199
219
  });
200
- if (option === undefined || !keys.includes(key)) {
201
- return fail(keysToUsage(keys));
220
+ if (option === undefined || !known.includes(key)) {
221
+ return fail(`${keysToUsage(known)}${hintToSuffix(optionToFieldTagHint(key, protocols))}`);
202
222
  }
203
223
  if (seen.has(key)) {
204
224
  return {
205
- message: `"${key}=" is declared more than once in "@bidirekt ${tag.keyword}": keep one`,
225
+ message: `"${key}=" is declared more than once in "${tagToLabel(tag)}": keep one`,
206
226
  location: tag.location,
207
227
  };
208
228
  }
209
229
  seen.add(key);
210
230
  const value = option.value;
211
- const usage = fail(optionUsages[key]);
231
+ const usage = fail(keyToUsage(key));
232
+ if (!keys.includes(key)) {
233
+ if (value === '') {
234
+ return usage;
235
+ }
236
+ vocabularyOptions[key] = value;
237
+ continue;
238
+ }
212
239
  switch (key) {
213
240
  case 'type': {
214
241
  const declared = tagTypeTextToSchemaNode(value);
@@ -234,31 +261,54 @@ export function argumentsToOptions(tagArguments, tag, keys) {
234
261
  options[key] = value === 'true';
235
262
  break;
236
263
  }
237
- case 'name':
238
- case 'description': {
264
+ case 'name': {
239
265
  if (value === '') {
240
266
  return usage;
241
267
  }
242
- options[key] = value;
268
+ options.name = value;
269
+ break;
270
+ }
271
+ case 'description': {
272
+ const invalid = descriptionToDiagnostic(value, tag.location);
273
+ if (invalid !== undefined) {
274
+ return invalid;
275
+ }
276
+ options.description = value;
243
277
  break;
244
278
  }
245
279
  }
246
280
  }
247
281
  if (seen.has('type') && seen.has('union')) {
248
282
  return {
249
- message: 'declare either "type=" or "union=" in "@bidirekt field", not both',
283
+ message: `declare either "type=" or "union=" in "${tagToLabel(tag)}", not both`,
250
284
  location: tag.location,
251
285
  };
252
286
  }
287
+ if (vocabulary !== undefined) {
288
+ options.extension = { vocabulary, options: vocabularyOptions };
289
+ }
253
290
  return options;
254
291
  }
292
+ function optionToFieldTagHint(key, protocols) {
293
+ const owner = optionToOwningProtocol(key, protocols);
294
+ if (owner?.vocabulary === undefined) {
295
+ return undefined;
296
+ }
297
+ return `for ${owner.vocabulary.label}, use "@bidirekt ${owner.name} field"`;
298
+ }
255
299
  function keysToUsage(keys) {
256
300
  if (keys.length === 1) {
257
- return optionUsages[keys[0]];
301
+ return keyToUsage(keys[0]);
258
302
  }
259
303
  return `"<key>=<value>" with key ${namesToChoice(keys)}`;
260
304
  }
261
- function valueToUnionMembers(value) {
305
+ function keyToUsage(key) {
306
+ if (Object.hasOwn(optionUsages, key)) {
307
+ return optionUsages[key];
308
+ }
309
+ return `"${key}=<value>"`;
310
+ }
311
+ export function valueToUnionMembers(value) {
262
312
  const braces = unionPattern.exec(value);
263
313
  if (braces === null) {
264
314
  return undefined;
@@ -36,8 +36,11 @@ function nestedTextToSchemaNode(text, leafToSchemaNode) {
36
36
  }
37
37
  return leafToSchemaNode(text);
38
38
  }
39
+ export function textToPrimitive(text) {
40
+ return primitiveTypes.find((type) => type === text);
41
+ }
39
42
  function primitiveToSchemaNode(text) {
40
- const primitive = primitiveTypes.find((type) => type === text);
43
+ const primitive = textToPrimitive(text);
41
44
  if (primitive === undefined) {
42
45
  return undefined;
43
46
  }
@@ -3,6 +3,7 @@ import { isDiagnostic } from "../model/diagnostic.js";
3
3
  import { nodeToLocation } from "./location.js";
4
4
  import { builtDeclarationToDiagnostic, classToDiagnostic, classToSchemaName, isDefaultLibrarySymbol, isLibrarySymbol, nodeToSymbol, symbolToSchemaName, typeToUntranslatableMessage, } from "./schema-context.js";
5
5
  const boxedPrimitives = ['String', 'Number', 'Boolean'];
6
+ const listNames = ['Array', 'ReadonlyArray'];
6
7
  function startWalk(union) {
7
8
  return { expanding: new Set(), bindings: new Map(), union };
8
9
  }
@@ -121,6 +122,59 @@ function walkToTranslatedType(typeNode, context, walk) {
121
122
  }
122
123
  return { node, optional: parts.optional || translated.optional };
123
124
  }
125
+ export function typeNodeToShapedLeaf(typeNode, leaf, context) {
126
+ return walkToShapedLeaf(typeNode, leaf, context, startWalk(undefined));
127
+ }
128
+ function walkToShapedLeaf(typeNode, leaf, context, walk) {
129
+ const parts = typeNodeToParts(typeNode);
130
+ let shaped = { node: leaf, optional: false };
131
+ if (parts.members.length === 1) {
132
+ shaped = memberToShapedLeaf(parts.members[0], leaf, context, walk);
133
+ }
134
+ if (isDiagnostic(shaped)) {
135
+ return shaped;
136
+ }
137
+ const flags = walkToFlags(typeNode, context, walk);
138
+ let node = shaped.node;
139
+ if (flags.nullable) {
140
+ node = { ...node, nullable: true };
141
+ }
142
+ return { node, optional: flags.optional || shaped.optional };
143
+ }
144
+ function memberToShapedLeaf(member, leaf, context, walk) {
145
+ const items = memberToListItems(member, context);
146
+ if (items !== undefined) {
147
+ const node = translatedToNestedNode(walkToShapedLeaf(items, leaf, context, walk), items, context);
148
+ if (isDiagnostic(node)) {
149
+ return node;
150
+ }
151
+ return { node: { type: 'array', items: node }, optional: false };
152
+ }
153
+ const followed = memberToFollowed(member, context, walk);
154
+ if (followed !== undefined) {
155
+ return walkToShapedLeaf(followed.typeNode, leaf, context, followed.walk);
156
+ }
157
+ return { node: leaf, optional: false };
158
+ }
159
+ function memberToListItems(member, context) {
160
+ if (ts.isArrayTypeNode(member)) {
161
+ return member.elementType;
162
+ }
163
+ if (ts.isTypeOperatorNode(member) &&
164
+ member.operator === ts.SyntaxKind.ReadonlyKeyword) {
165
+ return memberToListItems(member.type, context);
166
+ }
167
+ if (!isReference(member) || member.typeArguments?.length !== 1) {
168
+ return undefined;
169
+ }
170
+ const symbol = referenceToSymbol(member, context);
171
+ if (symbol === undefined ||
172
+ !isLibrarySymbol(symbol, context) ||
173
+ !listNames.includes(symbol.name)) {
174
+ return undefined;
175
+ }
176
+ return member.typeArguments[0];
177
+ }
124
178
  function typeNodeToParts(typeNode) {
125
179
  const parts = { members: [], nullable: false, optional: false };
126
180
  const add = (node) => {
@@ -155,8 +209,7 @@ function typeNodeToParts(typeNode) {
155
209
  add(typeNode);
156
210
  return parts;
157
211
  }
158
- function typeNodeToNestedNode(typeNode, context, walk) {
159
- const translated = walkToTranslatedType(typeNode, context, walk);
212
+ function translatedToNestedNode(translated, typeNode, context) {
160
213
  if (isDiagnostic(translated)) {
161
214
  return translated;
162
215
  }
@@ -217,8 +270,9 @@ function memberToTranslatedType(member, context, walk) {
217
270
  if (isNumericLiteralType(member)) {
218
271
  return { message: numericLiteralMessage(member.getText(), context.hints) };
219
272
  }
220
- if (ts.isArrayTypeNode(member)) {
221
- return arrayToTranslatedType(member.elementType, context, walk);
273
+ const items = memberToListItems(member, context);
274
+ if (items !== undefined) {
275
+ return arrayToTranslatedType(items, context, walk);
222
276
  }
223
277
  if (ts.isTypeOperatorNode(member) &&
224
278
  member.operator === ts.SyntaxKind.ReadonlyKeyword) {
@@ -291,10 +345,6 @@ function referenceToTranslatedType(reference, context, walk) {
291
345
  };
292
346
  }
293
347
  function libraryReferenceToTranslatedType(reference, name, typeArguments, context, walk) {
294
- if ((name === 'Array' || name === 'ReadonlyArray') &&
295
- typeArguments.length === 1) {
296
- return arrayToTranslatedType(typeArguments[0], context, walk);
297
- }
298
348
  if ((name === 'Record' || name === 'Object') && typeArguments.length === 2) {
299
349
  const [key, values] = typeArguments;
300
350
  if (!isStringType(key, context)) {
@@ -314,14 +364,14 @@ function libraryReferenceToTranslatedType(reference, name, typeArguments, contex
314
364
  };
315
365
  }
316
366
  function arrayToTranslatedType(items, context, walk) {
317
- const node = typeNodeToNestedNode(items, context, walk);
367
+ const node = translatedToNestedNode(walkToTranslatedType(items, context, walk), items, context);
318
368
  if (isDiagnostic(node)) {
319
369
  return node;
320
370
  }
321
371
  return { node: { type: 'array', items: node }, optional: false };
322
372
  }
323
373
  function mapToTranslatedType(values, context, walk) {
324
- const node = typeNodeToNestedNode(values, context, walk);
374
+ const node = translatedToNestedNode(walkToTranslatedType(values, context, walk), values, context);
325
375
  if (isDiagnostic(node)) {
326
376
  return node;
327
377
  }