Skip to content

Commit b747e4b

Browse files
chore: Clean up OptionDefinition (#2747)
* expose enum option value map * update option.type to optionType union * add comments to OptionDefinition
1 parent ea36c22 commit b747e4b

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

packages/quicktype-core/src/RendererOptions/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class BooleanOption<Name extends string> extends Option<Name, boolean> {
7777
super({
7878
name,
7979
kind,
80-
type: Boolean,
80+
optionType: "boolean",
8181
description,
8282
defaultValue,
8383
});
@@ -134,15 +134,14 @@ export class StringOption<Name extends string> extends Option<Name, string> {
134134
defaultValue: string,
135135
kind: OptionKind = "primary",
136136
) {
137-
const definition = {
137+
super({
138138
name,
139139
kind,
140-
type: String,
140+
optionType: "string",
141141
description,
142142
typeLabel,
143143
defaultValue,
144-
};
145-
super(definition);
144+
});
146145
}
147146
}
148147

@@ -163,15 +162,15 @@ export class EnumOption<
163162
defaultValue: NoInfer<EnumKey>,
164163
kind: OptionKind = "primary",
165164
) {
166-
const definition = {
165+
super({
167166
name,
168167
kind,
169-
type: String,
168+
optionType: "enum",
170169
description,
171170
typeLabel: Object.keys(values).join("|"),
172171
defaultValue,
173-
};
174-
super(definition);
172+
values,
173+
});
175174

176175
this._values = values;
177176
}

packages/quicktype-core/src/RendererOptions/types.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ import type { EnumOption, Option } from "./index";
88
export type OptionKind = "primary" | "secondary" | "cli";
99

1010
export interface OptionDefinition<Name extends string = string, T = unknown> {
11+
/** Option Name */
12+
name: Name;
13+
/** Option Alias for CLI */
1114
alias?: string;
12-
defaultOption?: boolean;
13-
defaultValue?: T;
15+
/** Option Description */
1416
description: string;
17+
/** Category of Option */
18+
optionType: "string" | "boolean" | "enum";
19+
/** Default Value for Option */
20+
defaultValue?: T;
21+
/** Enum only, map of possible keys and values */
22+
values?: Record<string, unknown>;
23+
24+
/** Primary, Secondary, or CLI */
1525
kind?: OptionKind;
26+
/** Whether multiple CLI inputs are allowed for this option */
1627
multiple?: boolean;
17-
name: Name;
18-
type: StringConstructor | BooleanConstructor;
28+
29+
// Unknown
1930
typeLabel?: string;
2031
}
2132

src/index.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,15 @@ function makeOptionDefinitions(
411411
{
412412
name: "out",
413413
alias: "o",
414-
type: String,
414+
optionType: "string",
415415
typeLabel: "FILE",
416416
description: "The output file. Determines --lang and --top-level.",
417417
kind: "cli",
418418
},
419419
{
420420
name: "top-level",
421421
alias: "t",
422-
type: String,
422+
optionType: "string",
423423
typeLabel: "NAME",
424424
description: "The name for the top level type.",
425425
kind: "cli",
@@ -432,7 +432,7 @@ function makeOptionDefinitions(
432432
{
433433
name: "lang",
434434
alias: "l",
435-
type: String,
435+
optionType: "string",
436436
typeLabel: "LANG",
437437
description: "The target language.",
438438
kind: "cli",
@@ -442,24 +442,23 @@ function makeOptionDefinitions(
442442
{
443443
name: "src-lang",
444444
alias: "s",
445-
type: String,
445+
optionType: "string",
446446
defaultValue: undefined,
447447
typeLabel: "SRC_LANG",
448448
description: "The source language (default is json).",
449449
kind: "cli",
450450
},
451451
{
452452
name: "src",
453-
type: String,
453+
optionType: "string",
454454
multiple: true,
455-
defaultOption: true,
456455
typeLabel: "FILE|URL|DIRECTORY",
457456
description: "The file, url, or data directory to type.",
458457
kind: "cli",
459458
},
460459
{
461460
name: "src-urls",
462-
type: String,
461+
optionType: "string",
463462
typeLabel: "FILE",
464463
description: "Tracery grammar describing URLs to crawl.",
465464
kind: "cli",
@@ -469,7 +468,7 @@ function makeOptionDefinitions(
469468
mapMap(mapFromObject(inferenceFlags), (flag, name) => {
470469
return {
471470
name: dashedFromCamelCase(negatedInferenceFlagName(name)),
472-
type: Boolean,
471+
optionType: "boolean" as const,
473472
description: flag.negationDescription + ".",
474473
kind: "cli" as const,
475474
};
@@ -478,29 +477,29 @@ function makeOptionDefinitions(
478477
const afterInference: OptionDefinition[] = [
479478
{
480479
name: "graphql-schema",
481-
type: String,
480+
optionType: "string",
482481
typeLabel: "FILE",
483482
description: "GraphQL introspection file.",
484483
kind: "cli",
485484
},
486485
{
487486
name: "graphql-introspect",
488-
type: String,
487+
optionType: "string",
489488
typeLabel: "URL",
490489
description: "Introspect GraphQL schema from a server.",
491490
kind: "cli",
492491
},
493492
{
494493
name: "http-method",
495-
type: String,
494+
optionType: "string",
496495
typeLabel: "METHOD",
497496
description:
498497
"HTTP method to use for the GraphQL introspection query.",
499498
kind: "cli",
500499
},
501500
{
502501
name: "http-header",
503-
type: String,
502+
optionType: "string",
504503
multiple: true,
505504
typeLabel: "HEADER",
506505
description:
@@ -510,69 +509,69 @@ function makeOptionDefinitions(
510509
{
511510
name: "additional-schema",
512511
alias: "S",
513-
type: String,
512+
optionType: "string",
514513
multiple: true,
515514
typeLabel: "FILE",
516515
description: "Register the $id's of additional JSON Schema files.",
517516
kind: "cli",
518517
},
519518
{
520519
name: "no-render",
521-
type: Boolean,
520+
optionType: "boolean",
522521
description: "Don't render output.",
523522
kind: "cli",
524523
},
525524
{
526525
name: "alphabetize-properties",
527-
type: Boolean,
526+
optionType: "boolean",
528527
description: "Alphabetize order of class properties.",
529528
kind: "cli",
530529
},
531530
{
532531
name: "all-properties-optional",
533-
type: Boolean,
532+
optionType: "boolean",
534533
description: "Make all class properties optional.",
535534
kind: "cli",
536535
},
537536
{
538537
name: "build-markov-chain",
539-
type: String,
538+
optionType: "string",
540539
typeLabel: "FILE",
541540
description: "Markov chain corpus filename.",
542541
kind: "cli",
543542
},
544543
{
545544
name: "quiet",
546-
type: Boolean,
545+
optionType: "boolean",
547546
description: "Don't show issues in the generated code.",
548547
kind: "cli",
549548
},
550549
{
551550
name: "debug",
552-
type: String,
551+
optionType: "string",
553552
typeLabel: "OPTIONS or all",
554553
description:
555554
"Comma separated debug options: print-graph, print-reconstitution, print-gather-names, print-transformations, print-schema-resolving, print-times, provenance",
556555
kind: "cli",
557556
},
558557
{
559558
name: "telemetry",
560-
type: String,
559+
optionType: "string",
561560
typeLabel: "enable|disable",
562561
description: "Enable anonymous telemetry to help improve quicktype",
563562
kind: "cli",
564563
},
565564
{
566565
name: "help",
567566
alias: "h",
568-
type: Boolean,
567+
optionType: "boolean",
569568
description: "Get some help.",
570569
kind: "cli",
571570
},
572571
{
573572
name: "version",
574573
alias: "v",
575-
type: Boolean,
574+
optionType: "boolean",
576575
description: "Display the version of quicktype",
577576
kind: "cli",
578577
},

0 commit comments

Comments
 (0)