Skip to content

Commit

Permalink
refactor(cli): prepare for noUncheckedIndexedAccess (#4121)
Browse files Browse the repository at this point in the history
* refactor(cli): prepare for noUncheckedIndexedAccess

* replace typeof with key !== undefined

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* replace all typeof !== "undefined" with !== undefined

* use assertExists

* tweaks

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
syhol and iuioiua authored Jan 10, 2024
1 parent 668856c commit cdab4c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
63 changes: 35 additions & 28 deletions cli/parse_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @module
*/
import { assert } from "../assert/assert.ts";
import { assertExists } from "../assert/assert_exists.ts";

/** Combines recursively all intersection types and returns a new single type. */
type Id<TRecord> = TRecord extends Record<string, unknown>
Expand Down Expand Up @@ -355,7 +355,7 @@ function get<TValue>(

function getForce<TValue>(obj: Record<string, TValue>, key: string): TValue {
const v = get(obj, key);
assert(v !== undefined);
assertExists(v);
return v;
}

Expand All @@ -371,8 +371,8 @@ function hasKey(obj: NestedMapping, keys: string[]): boolean {
o = (get(o, key) ?? {}) as NestedMapping;
});

const key = keys[keys.length - 1];
return hasOwn(o, key);
const key = keys.at(-1);
return key !== undefined && hasOwn(o, key);
}

/** Take a set of command line arguments, optionally with a set of options, and
Expand Down Expand Up @@ -467,8 +467,9 @@ export function parseArgs<
} else {
aliases[key] = val as Array<string>;
}
for (const alias of getForce(aliases, key)) {
aliases[alias] = [key].concat(aliases[key].filter((y) => alias !== y));
const aliasesForKey = getForce(aliases, key);
for (const alias of aliasesForKey) {
aliases[alias] = [key].concat(aliasesForKey.filter((y) => alias !== y));
}
}
}
Expand Down Expand Up @@ -567,7 +568,7 @@ export function parseArgs<
o = get(o, key) as NestedMapping;
});

const key = keys[keys.length - 1];
const key = keys.at(-1)!;
const collectable = collect && !!get(flags.collect, name);

if (!collectable) {
Expand Down Expand Up @@ -618,11 +619,13 @@ export function parseArgs<

for (let i = 0; i < args.length; i++) {
const arg = args[i];
assertExists(arg);

if (/^--.+=/.test(arg)) {
const m = arg.match(/^--([^=]+)=(.*)$/s);
assert(m !== null);
assertExists(m);
const [, key, value] = m;
assertExists(key);

if (flags.bools[key]) {
const booleanValue = value !== "false";
Expand All @@ -634,11 +637,13 @@ export function parseArgs<
/^--no-.+/.test(arg) && get(flags.negatable, arg.replace(/^--no-/, ""))
) {
const m = arg.match(/^--no-(.+)/);
assert(m !== null);
assertExists(m);
assertExists(m[1]);
setArg(m[1], false, arg, false);
} else if (/^--.+/.test(arg)) {
const m = arg.match(/^--(.+)/);
assert(m !== null);
assertExists(m);
assertExists(m[1]);
const [, key] = m;
const next = args[i + 1];
if (
Expand All @@ -650,7 +655,7 @@ export function parseArgs<
) {
setArg(key, next, arg);
i++;
} else if (/^(true|false)$/.test(next)) {
} else if (next !== undefined && (next === "true" || next === "false")) {
setArg(key, next === "true", arg);
i++;
} else {
Expand All @@ -660,50 +665,51 @@ export function parseArgs<
const letters = arg.slice(1, -1).split("");

let broken = false;
for (let j = 0; j < letters.length; j++) {
for (const [j, letter] of letters.entries()) {
const next = arg.slice(j + 2);

if (next === "-") {
setArg(letters[j], next, arg);
setArg(letter, next, arg);
continue;
}

if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) {
setArg(letters[j], next.split(/=(.+)/)[1], arg);
if (/[A-Za-z]/.test(letter) && next.includes("=")) {
setArg(letter, next.split(/=(.+)/)[1], arg);
broken = true;
break;
}

if (
/[A-Za-z]/.test(letters[j]) &&
/[A-Za-z]/.test(letter) &&
/-?\d+(\.\d*)?(e-?\d+)?$/.test(next)
) {
setArg(letters[j], next, arg);
setArg(letter, next, arg);
broken = true;
break;
}

if (letters[j + 1] && letters[j + 1].match(/\W/)) {
setArg(letters[j], arg.slice(j + 2), arg);
if (letters[j + 1]?.match(/\W/)) {
setArg(letter, arg.slice(j + 2), arg);
broken = true;
break;
} else {
setArg(letters[j], get(flags.strings, letters[j]) ? "" : true, arg);
setArg(letter, get(flags.strings, letter) ? "" : true, arg);
}
}

const [key] = arg.slice(-1);
const key = arg.at(-1)!;
if (!broken && key !== "-") {
const nextArg = args[i + 1];
if (
args[i + 1] &&
!/^(-|--)[^-]/.test(args[i + 1]) &&
nextArg &&
!/^(-|--)[^-]/.test(nextArg) &&
!get(flags.bools, key) &&
(get(aliases, key) ? !aliasIsBoolean(key) : true)
) {
setArg(key, args[i + 1], arg);
setArg(key, nextArg, arg);
i++;
} else if (args[i + 1] && /^(true|false)$/.test(args[i + 1])) {
setArg(key, args[i + 1] === "true", arg);
} else if (nextArg && (nextArg === "true" || nextArg === "false")) {
setArg(key, nextArg === "true", arg);
i++;
} else {
setArg(key, get(flags.strings, key) ? "" : true, arg);
Expand All @@ -724,8 +730,9 @@ export function parseArgs<
if (!hasKey(argv, key.split("."))) {
setKey(argv, key, value, false);

if (aliases[key]) {
for (const x of aliases[key]) {
const alias = aliases[key];
if (alias !== undefined) {
for (const x of alias) {
setKey(argv, x, value, false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/prompt_secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function readLineFromStdinSync(callback?: (n: number) => void): string {
if (c[0] === BS || c[0] === DEL) {
buf.pop();
} else {
buf.push(c[0]);
buf.push(c[0]!);
}
if (callback) callback(buf.length);
}
Expand Down

0 comments on commit cdab4c6

Please sign in to comment.