Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types to options.js #126

Closed
wants to merge 13 commits into from
4 changes: 2 additions & 2 deletions lib/anonymize.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ module.exports = {
/**
* Strip the version and paths out of the given string.
* This is only used for tests to make them pass even when the version/paths change.
* @param {Options} options
* @param {Options | null} options
* @param {string} string
* @returns {string}
*/
function pathsAndVersions(options, string) {
if (options.forTests) {
if (options && options.forTests) {
const root = path.dirname(__dirname);
return replaceVersion(string.split(root).join('<local-path>'));
}
Expand Down
9 changes: 6 additions & 3 deletions lib/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class CustomError extends Error {
}

/**
* @param {Options} options
* @param {Options | null} options
* @param {CustomError} err
* @param {Path} [defaultPath]
* @returns {string}
*/
function report(options, err, defaultPath) {
if (options.report === 'json') {
if (options && options.report === 'json') {
henriquecbuss marked this conversation as resolved.
Show resolved Hide resolved
return Anonymize.pathsAndVersions(
options,
Anonymize.pathsAndVersions(
Expand All @@ -42,7 +42,10 @@ function report(options, err, defaultPath) {
);
}

return Anonymize.pathsAndVersions(options, formatHuman(options.debug, err));
return Anonymize.pathsAndVersions(
options,
formatHuman(options?.debug ?? false, err)
henriquecbuss marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
Expand Down
77 changes: 70 additions & 7 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ function compute(processArgv) {
alias: Flags.flags
.filter((flag) => flag.alias)
.reduce((object, flag) => {
object[flag.name] = flag.alias;
if (flag.alias) {
object[flag.name] = flag.alias;
Comment on lines +49 to +50
Copy link
Owner

@jfmengels jfmengels May 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has to be a way to tell TS that flag.alias is always false, right? 🤔
(Not that I necessarily want to remove it, I'm mostly intellectually curious 😅 )

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I meant. I don't know how smart ts-reset makes filter in this case, but I'd imagine that in this case TS will not remember that flag.alias is non-falsy. If it does, then yes, otherwise, it's so close 😁

Copy link
Contributor

@lishaduck lishaduck Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it locally, I tossed it into a gist:
https://gist.github.com/lishaduck/8cabda9ae913535f702a4376712d4608

I'd just give a patch, but I'm tweaking some other types. I'll PR soon (unless I forget 😉) EDIT: #155
I'm planning on rebasing that on this.

It's a patch now!

}

return object;
}, {}),
}, /** @type {Record<string, string>} */ ({})),
boolean: Flags.flags
.filter((flag) => flag.boolean)
.map((flag) => flag.name),
Expand Down Expand Up @@ -201,7 +204,7 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
forceBuild: args['force-build'],
report: args.report === 'json' || args.report === 'ndjson' ? 'json' : null,
reportOnOneLine: args.report === 'ndjson',
rulesFilter: listOfStrings(args.rules),
rulesFilter: listOfStrings(args.rules) || [],
henriquecbuss marked this conversation as resolved.
Show resolved Hide resolved
ignoredDirs: listOfStrings(args['ignore-dirs']) || [],
ignoredFiles: listOfStrings(args['ignore-files']) || [],

Expand Down Expand Up @@ -289,14 +292,24 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
};
}

/**
*
* @param {minimist.ParsedArgs} args
* @returns {boolean}
*/
function parseUnsuppress(args) {
if (args.unsuppress) {
return true;
}

return listOfStrings(args['unsuppress-rules']) || false;
return Boolean(listOfStrings(args['unsuppress-rules']));
}

/**
*
* @param {string | string[]} input
* @returns {string[] | null}
*/
function listOfStrings(input) {
if (typeof input === 'string') {
return input.split(',');
Expand All @@ -305,13 +318,19 @@ function listOfStrings(input) {
if (Array.isArray(input)) {
return input.reduce(
(acc, subArray) => [...acc, ...subArray.split(',')],
[]
/** @type {string[]} */ ([])
);
}

return null;
}

/**
*
* @param {Subcommand | null} subcommand
* @param {string} string
* @returns {{ repoName: string, pathToFolder: string, reference: string }}
henriquecbuss marked this conversation as resolved.
Show resolved Hide resolved
*/
function parseTemplate(subcommand, string) {
const match = /([^/]+\/[^#/]+)(\/[^#]+)?(#(.+))?/.exec(string);
if (!match) {
Expand All @@ -338,6 +357,12 @@ ${Flags.buildFlag(subcommand, Flags.templateFlag)}`
};
}

/**
*
* @param {minimist.ParsedArgs} args
* @param {Subcommand | null} subcommand
* @returns {string | null}
*/
function findElmJsonPath(args, subcommand) {
if (args.elmjson) return args.elmjson;
// Shortcutting the search for elm.json when `--help` since we won't need it
Expand All @@ -355,6 +380,10 @@ function findElmJsonPath(args, subcommand) {
return findUp.sync('elm.json') || null;
}

/**
*
* @returns {(flag: string) => true}
henriquecbuss marked this conversation as resolved.
Show resolved Hide resolved
*/
function unknownCheck() {
const allFlagNames = new Set(Flags.flags.map((flag) => flag.name));

Expand All @@ -376,6 +405,11 @@ function unknownCheck() {
};
}

/**
*
* @param {string} name
* @returns {string}
*/
function unknownFlagMessage(name) {
if (name === 'suppress') {
const orange = chalk.keyword('orange');
Expand Down Expand Up @@ -403,6 +437,11 @@ function unknownFlagMessage(name) {
].join('\n');
}

/**
*
* @param {string} flag
* @returns {true}
*/
function unknownShortHandFlagCheck(flag) {
const flagRegex = /^-(?<name>\w+)/;
const match = flagRegex.exec(flag);
Expand Down Expand Up @@ -442,6 +481,11 @@ function unknownShortHandFlagCheck(flag) {
return true;
}

/**
*
* @param {string} flag
* @returns {string[]}
*/
function suggestions(flag) {
return Flags.flags
.map((f) => ({
Expand All @@ -463,6 +507,11 @@ function suggestions(flag) {
.map((f) => chalk.greenBright(` --${f.name}${Flags.buildFlagsArgs(f)}`));
}

/**
*
* @param {Subcommand | null} subcommand
* @param {minimist.ParsedArgs} args
*/
function checkForMissingArgs(subcommand, args) {
Flags.flags
.filter((flag) => flag.boolean === false)
Expand Down Expand Up @@ -502,6 +551,11 @@ ${Flags.buildFlag(subcommand, flag)}`
});
}

/**
*
* @param {Subcommand | null} subcommand
* @param {minimist.ParsedArgs} args
*/
function checkForInvalidArgs(subcommand, args) {
if (args.report && !['json', 'ndjson', 'human'].includes(args.report)) {
reportErrorAndExit(
Expand All @@ -519,6 +573,12 @@ ${Flags.buildFlag(subcommand, Flags.reportFlag)}`
}
}

/**
*
* @param {Subcommand | null} subcommand
* @param {string} gitHubAuth
* @returns {{ gitHubUser: string, gitHubPassword: string }}
*/
function parseGitHubAuth(subcommand, gitHubAuth) {
const split = gitHubAuth.split(':');
if (split.length !== 2) {
Expand All @@ -540,9 +600,12 @@ ${Flags.buildFlag(subcommand, Flags.gitHubAuthFlag)}`
return {gitHubUser, gitHubPassword};
}

/**
*
* @param {ErrorMessage.CustomError} errorToReport
henriquecbuss marked this conversation as resolved.
Show resolved Hide resolved
*/
function reportErrorAndExit(errorToReport) {
// @ts-expect-error - Handle this later
console.log(ErrorMessage.report({}, errorToReport));
console.log(ErrorMessage.report(null, errorToReport));

process.exit(1);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export type Options = {
templateElmModulePath: (string) => Path;
pathToTemplateElmJson: (string) => Path;
dependenciesCachePath: () => Path;
elmJsonPath: Path;
elmJsonPath: Path | null;
elmJsonPathWasSpecified: boolean;
readmePath: Path;
readmePath: Path | null;
projectToReview: () => Path;
directoriesToAnalyze: Path[];
fileCachePath: () => Path;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.no-implicit-any.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lib/hash.js",
"lib/help.js",
"lib/os-helpers.js",
"lib/options.js",
"jest.config.js"
]
}