From 9aa5c7350d19a0e63f82bd051ae61cf60f31c26c Mon Sep 17 00:00:00 2001 From: Alice Pote Date: Thu, 3 Nov 2022 09:51:11 -0400 Subject: [PATCH] some code cleanup --- src/cli/parse-flags.ts | 50 ++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/cli/parse-flags.ts b/src/cli/parse-flags.ts index 8f1bb3141d1a..a0cc0749a21e 100644 --- a/src/cli/parse-flags.ts +++ b/src/cli/parse-flags.ts @@ -83,7 +83,7 @@ export const parseFlags = (args: string[], _sys?: CompilerSystem): ConfigFlags = * | /^[a-zA-Z0-9]+$/ ; * * There are additional constraints (not shown in the grammar for brevity's sake) - * on the type of CLIValue which will be associated with a particular argument. + * on the type of `CLIValue` which will be associated with a particular argument. * We enforce this by declaring lists of boolean, string, etc arguments and * checking that types of values before setting them. * @@ -177,7 +177,7 @@ const parseCLITerm = (flags: ConfigFlags, args: string[]) => { /** * Normalize a 'negative' flag name, just to do a little pre-processing before - * we pass it to `setValue`. + * we pass it to `setCLIArg`. * * @param flagName the flag name to normalize * @returns a normalized flag name @@ -215,7 +215,8 @@ const normalizeFlagName = (flagName: string): string => { * * @param flags a {@link ConfigFlags} object * @param rawArg the raw argument name matched by the parser - * @param normalizedArg an argument with leading control characters (`--`, `--no-`, etc) removed + * @param normalizedArg an argument with leading control characters (`--`, + * `--no-`, etc) removed * @param value the raw value to be set onto the config flags object */ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, value: CLIValueResult) => { @@ -228,10 +229,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va : // no value was supplied, default to true true; flags.knownArgs.push(rawArg); - return; - } - - if (readOnlyArrayHasStringMember(STRING_CLI_FLAGS, normalizedArg)) { + } else if (readOnlyArrayHasStringMember(STRING_CLI_FLAGS, normalizedArg)) { if (typeof value === 'string') { flags[normalizedArg] = value; flags.knownArgs.push(rawArg); @@ -239,10 +237,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va } else { throwCLIParsingError(rawArg, 'expected a string argument but received nothing'); } - return; - } - - if (readOnlyArrayHasStringMember(STRING_ARRAY_CLI_FLAGS, normalizedArg)) { + } else if (readOnlyArrayHasStringMember(STRING_ARRAY_CLI_FLAGS, normalizedArg)) { if (typeof value === 'string') { if (!Array.isArray(flags[normalizedArg])) { flags[normalizedArg] = []; @@ -261,10 +256,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va } else { throwCLIParsingError(rawArg, 'expected a string argument but received nothing'); } - return; - } - - if (readOnlyArrayHasStringMember(NUMBER_CLI_FLAGS, normalizedArg)) { + } else if (readOnlyArrayHasStringMember(NUMBER_CLI_FLAGS, normalizedArg)) { if (typeof value === 'string') { const parsed = parseInt(value, 10); @@ -280,10 +272,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va } else { throwCLIParsingError(rawArg, 'expected a number argument but received nothing'); } - return; - } - - if (readOnlyArrayHasStringMember(STRING_NUMBER_CLI_FLAGS, normalizedArg)) { + } else if (readOnlyArrayHasStringMember(STRING_NUMBER_CLI_FLAGS, normalizedArg)) { if (typeof value === 'string') { if (CLI_ARG_STRING_REGEX.test(value)) { // if it matches the regex we treat it like a string @@ -306,10 +295,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va } else { throwCLIParsingError(rawArg, 'expected a string or a number but received nothing'); } - return; - } - - if (readOnlyArrayHasStringMember(LOG_LEVEL_CLI_FLAGS, normalizedArg)) { + } else if (readOnlyArrayHasStringMember(LOG_LEVEL_CLI_FLAGS, normalizedArg)) { if (typeof value === 'string') { if (isLogLevel(value)) { flags[normalizedArg] = value; @@ -321,7 +307,6 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va } else { throwCLIParsingError(rawArg, 'expected to receive a valid log level but received nothing'); } - return; } }; @@ -347,9 +332,9 @@ const CLI_ARG_STRING_REGEX = /[^\d\.Ee\+\-]+/g; export const Empty = Symbol('Empty'); /** - * The result of trying to parse a CLI arg. `ArgValue` wraps up an argument if - * present, while `Empty` indicates that nothing was matched or that the input - * was malformed. + * The result of trying to parse a CLI arg. This will be a `string` if a + * well-formed value is present, or `Empty` to indicate that nothing was matched + * or that the input was malformed. */ type CLIValueResult = string | typeof Empty; @@ -377,6 +362,7 @@ const parseCLIValue = (args: string[]): CLIValueResult => { // until later on. const value = args.shift(); if (typeof value === 'string') { + // this `if { }` block is just to keep TS happy :/ return value; } } @@ -434,7 +420,8 @@ const isLogLevel = (maybeLogLevel: string): maybeLogLevel is LogLevel => readOnlyArrayHasStringMember(LOG_LEVELS, maybeLogLevel); /** - * A little helper for constructing and throwing an error message with info about what went wrong + * A little helper for constructing and throwing an error message with info + * about what went wrong * * @param flag the flag which encountered the error * @param message a message specific to the error which was encountered @@ -447,7 +434,7 @@ const throwCLIParsingError = (flag: string, message: string) => { * Throw a specific error for the situation where we ran into an issue parsing * a number. * - * @param flag the flag for which we encounted the issue + * @param flag the flag for which we encountered the issue * @param value what we were trying to parse */ const throwNumberParsingError = (flag: string, value: string) => { @@ -456,11 +443,12 @@ const throwNumberParsingError = (flag: string, value: string) => { /** * A little helper to 'dereference' a flag alias, which if you squint a little - * you can think of like a pointer to a full flag name. This 'c' is like a + * you can think of like a pointer to a full flag name. Thus 'c' is like a * pointer to 'config', so here we're doing something like `*c`. Of course, this * being JS, this is just a metaphor! * - * If no 'dereference' is found for the possible alias we just return the passed string unmodified. + * If no 'dereference' is found for the possible alias we just return the + * passed string unmodified. * * @param maybeAlias a string which _could_ be an alias to a full flag name * @returns the full aliased flag name, if found, or the passed string if not