Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alicewriteswrongs committed Nov 3, 2022
1 parent 59cd113 commit 6c47f6b
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/cli/parse-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ 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.
* checking the types of values before setting them.
*
* We don't need to turn the list of CLI arg tokens into any kind of
* intermediate representation since we aren't concerned with doing anything
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -213,36 +213,36 @@ const normalizeFlagName = (flagName: string): string => {
* which make sure that the string value can be properly parsed into a JS
* runtime value of the right type (e.g. number, string, etc).
*
* @throws if a value cannot be parsed to the right type for a given flag
* @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) => {
normalizedArg = dereferenceAlias(normalizedArg);

if (readOnlyArrayHasStringMember(BOOLEAN_CLI_FLAGS, normalizedArg)) {
// We're setting a boolean!
flags[normalizedArg] =
typeof value === 'string'
? Boolean(value)
: // 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)) {
// We're setting a string!
if (typeof value === 'string') {
flags[normalizedArg] = value;
flags.knownArgs.push(rawArg);
flags.knownArgs.push(value);
} 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)) {
// We're setting a string, but it's one where the user can pass multiple values,
// like `--reporters="default" --reporters="jest-junit"`
if (typeof value === 'string') {
if (!Array.isArray(flags[normalizedArg])) {
flags[normalizedArg] = [];
Expand All @@ -261,10 +261,8 @@ 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)) {
// We're setting a number!
if (typeof value === 'string') {
const parsed = parseInt(value, 10);

Expand All @@ -280,10 +278,8 @@ 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)) {
// We're setting a value which could be either a string _or_ a number
if (typeof value === 'string') {
if (CLI_ARG_STRING_REGEX.test(value)) {
// if it matches the regex we treat it like a string
Expand All @@ -306,10 +302,8 @@ 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)) {
// We're setting the log level, which can only be a set of specific string values
if (typeof value === 'string') {
if (isLogLevel(value)) {
flags[normalizedArg] = value;
Expand All @@ -321,7 +315,6 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
} else {
throwCLIParsingError(rawArg, 'expected to receive a valid log level but received nothing');
}
return;
}
};

Expand All @@ -347,9 +340,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;

Expand Down Expand Up @@ -377,6 +370,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;
}
}
Expand Down Expand Up @@ -434,7 +428,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
Expand All @@ -447,7 +442,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) => {
Expand All @@ -456,11 +451,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
Expand Down

0 comments on commit 6c47f6b

Please sign in to comment.