Linter pass

new callback for when a command has been found with the ability to terminate the parsing
This commit is contained in:
Erik 2023-12-14 17:33:54 +02:00
parent d73e1e5731
commit f024f059f2
5 changed files with 61 additions and 37 deletions

View File

@ -11,7 +11,7 @@
"parser": "@typescript-eslint/parser",
"plugins": [ "@typescript-eslint" ],
"parserOptions": {
"ecmaVersion": 2022,
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
@ -87,9 +87,13 @@
"id-blacklist": "warn",
"id-match": "warn",
"implicit-arrow-linebreak": "warn",
"indent": ["warn", 4, {
"indent": [
"warn",
4,
{
"SwitchCase": 1
}],
}
],
"init-declarations": "warn",
"quotes": [
"error",
@ -133,7 +137,7 @@
],
"max-lines-per-function": [
"warn",
100
140
],
"max-depth": [
"warn",
@ -191,7 +195,10 @@
"no-restricted-modules": "warn",
"no-restricted-properties": "warn",
"no-restricted-syntax": "warn",
"no-return-assign": ["warn", "except-parens"],
"no-return-assign": [
"warn",
"except-parens"
],
"no-return-await": "warn",
"no-script-url": "warn",
"no-self-compare": "warn",
@ -202,6 +209,7 @@
"no-tabs": "warn",
"no-template-curly-in-string": "error",
"no-throw-literal": "warn",
"no-trailing-spaces": "warn",
"no-undef-init": "error",
"no-undefined": "error",
"no-unmodified-loop-condition": "warn",
@ -238,7 +246,6 @@
"warn",
"before"
],
"padding-line-between-statements": "warn",
"padded-blocks": [
"warn",
{

View File

@ -36,6 +36,6 @@
"build": "tsc && tsc -p tsconfig.cjs.json && node ./scripts/declareTypes.js",
"test": "yarn build && jest",
"release": "yarn build && yarn publish",
"lint": "eslint --fix"
"lint": "eslint --fix src/"
}
}

View File

@ -30,8 +30,9 @@ type ParserOptions = {
type ParseOptions = {
prefix?: string,
commandFilter?: (cmd: ICommand) => boolean,
guild?: unknown
commandFilter?: (cmd: ICommand) => boolean | Promise<boolean>,
commandFound?: <Cmd extends ICommand>(cmd: Cmd) => boolean | Promise<boolean>
}
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
@ -69,22 +70,21 @@ class Parser extends EventEmitter
}
/**
*
*
* @param {string} message The text to parse command from
* @param {string} [prefix=this.prefix] Optional prefix to look for in front of the message
* @param {unknown} [guild] The guild to pass to the parser if the command options require values that expect guild based structures, e.g. a guild member.
* @param {Function} [commandFilter] Function for filtering out commands, useful if you want to stop the parser on a command level before argument parsin, probably saves some processing time. If the function evaluates to true the parsing stops
* @return {Promise<ParseResult | null>} {(Promise<ParseResult | null>)}
* @param message The text to parse command from
* @param [prefix=this.prefix] Optional prefix to look for in front of the message
* @param [guild] The guild to pass to the parser if the command options require values that expect guild based structures, e.g. a guild member.
* @param [commandFilter] Function for filtering out commands, useful if you want to stop the parser on a command level before argument parsing, probably saves some processing time. If the function evaluates to true the parsing stops
* @param [commandFound] Callback that is called when a command is matched, can be used to halt further parsing by e.g. checking for necessary permissions, returning a truthy value will halt parsing
* @return
* @memberof Parser
*/
// eslint-disable-next-line @typescript-eslint/ban-types
async parseMessage (
message: string,
{
prefix = this.prefix,
guild,
commandFilter
commandFilter,
commandFound,
}: ParseOptions = {}
): Promise<ParseResult | null>
{
@ -100,7 +100,7 @@ class Parser extends EventEmitter
return null;
this.debug(`Matched command ${command.name}`);
if (commandFilter && typeof commandFilter === 'function')
if (typeof commandFilter === 'function')
{
const result = await commandFilter(command);
if (!result)
@ -110,6 +110,9 @@ class Parser extends EventEmitter
}
}
if (typeof commandFound === 'function' && await commandFound(command))
return null;
const { subcommands, subcommandGroups } = command;
const args: ArgsResult = {};
const parseResult: ParseResult = { args, command, subcommand: null, subcommandGroup: null };

View File

@ -1,5 +1,7 @@
export class ParserError extends Error {
constructor (message: string, options?: ErrorOptions) {
export class ParserError extends Error
{
constructor (message: string, options?: ErrorOptions)
{
super(message, options);
this.name = 'ParserError';
}

View File

@ -1,22 +1,34 @@
import { Parser, Command, OptionType } from '../build/esm/index.js';
// const command = new Command({
// name: 'create',
// options: [{
// name: 'registration-code',
// aliases: ['code'],
// type: OptionType.SUB_COMMAND,
// options: [{
// name: 'amount',
// flag: true,
// type: OptionType.INTEGER,
// defaultValue: 1,
// valueOptional: true,
// required: true
// }]
// }]
// });
const command = new Command({
name: 'create',
options: [{
name: 'registration-code',
aliases: ['code'],
type: OptionType.SUB_COMMAND,
options: [{
name: 'amount',
flag: true,
type: OptionType.INTEGER,
defaultValue: 1,
valueOptional: true
}]
}]
name: 'botban',
options: [
{ name: 'users', type: OptionType.STRING, required: true },
{ name: 'service', choices: ['support', 'reports'], valueAsAlias: true, required: true }
]
});
const parser = new Parser({ commands: [command], prefix: '', debug: true });
parser.on('debug', console.log)
console.log(await parser.parseMessage('create code -a 1'));
console.log(await parser.parseMessage('create code --help'));
console.log(await parser.parseMessage('create --help'));
// console.log(await parser.parseMessage('create code -a 1'));
// console.log(await parser.parseMessage('create code --help'));
// // console.log(await parser.parseMessage('create --help'));
// console.log(await parser.parseMessage('create code'));
console.log(await parser.parseMessage('botban support dingus'));