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

chore(npm): bump typescript from 4.8.4 to 4.9.3 #610

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"make-dir-cli": "^3.0.0",
"prettier": "^2.7.1",
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
"typescript": "^4.9.3",
"vitest": "^0.25.2"
}
}
51 changes: 22 additions & 29 deletions src/adaptor/proxy/command/schema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
Param,
Params,
ParamsValues,
ParseError,
ParsedSchema,
ParsedSubCommand,
Schema,
SubCommand,
SubCommandEntries,
SubCommandGroup,
SubCommands,
makeError
} from '../../../model/command-schema.js';

Expand All @@ -18,12 +18,12 @@ const hasOwn = <O extends Record<PropertyKey, unknown>>(
key: PropertyKey
): key is keyof O => Object.hasOwn(object, key);

const parseParams = <P extends readonly Param[]>(
const parseParams = <S extends Schema | SubCommand>(
args: string[],
schema: Schema<Record<string, unknown>, P> | SubCommand<P>
): ['Ok', ParamsValues<P>] | ['Err', ParseError] => {
schema: S
): ['Ok', ParamsValues<Params<S>>] | ['Err', ParseError] => {
if (!schema.params) {
return ['Ok', [] as ParamsValues<P>];
return ['Ok', [] as ParamsValues<Params<S>>];
}
const values: unknown[] = [];
for (const param of schema.params) {
Expand Down Expand Up @@ -108,13 +108,17 @@ const parseParams = <P extends readonly Param[]>(
break;
}
}
return ['Ok', values as ParamsValues<P>];
return ['Ok', values as ParamsValues<Params<S>>];
};

const parseSubCommand = <E>(
const isSubCommandGroup = (subCommand: {
type?: string;
}): subCommand is SubCommandGroup => subCommand.type === 'SUB_COMMAND_GROUP';

const parseSubCommand = <S extends Schema | SubCommandGroup>(
args: string[],
schema: Schema<E> | SubCommandGroup<E>
): ['Ok', ParsedSubCommand<E>] | ['Err', ParseError] => {
schema: S
): ['Ok', ParsedSubCommand<SubCommands<S>>] | ['Err', ParseError] => {
const subCommandNames = Object.getOwnPropertyNames(schema.subCommands);
const arg = args.shift();

Expand All @@ -126,9 +130,9 @@ const parseSubCommand = <E>(
if (!hasOwn(schema.subCommands, arg)) {
return ['Err', ['UNKNOWN_COMMAND', Object.keys(schema.subCommands), arg]];
}
const subCommandKey: keyof E = arg;
const subCommandKey: keyof SubCommands<S> = arg;

const subCommand = schema.subCommands[subCommandKey];
const subCommand = (schema.subCommands as SubCommands<S>)[subCommandKey];
if (
!(
typeof subCommand === 'object' &&
Expand All @@ -138,11 +142,8 @@ const parseSubCommand = <E>(
) {
return ['Err', ['OTHERS', 'unreachable']];
}
if (subCommand['type'] === 'SUB_COMMAND_GROUP') {
const subSubCommand = parseSubCommand(
args,
subCommand as SubCommandGroup<SubCommandEntries>
);
if (isSubCommandGroup(subCommand)) {
const subSubCommand = parseSubCommand(args, subCommand);
if (subSubCommand[0] === 'Err') {
return subSubCommand;
}
Expand All @@ -152,7 +153,7 @@ const parseSubCommand = <E>(
name: subCommandKey,
type: 'SUB_COMMAND',
subCommand: subSubCommand[1]
} as unknown as ParsedSubCommand<E>
} as unknown as ParsedSubCommand<SubCommands<S>>
];
}
const params = parseParams(args, subCommand);
Expand All @@ -165,17 +166,13 @@ const parseSubCommand = <E>(
name: subCommandKey,
type: 'PARAMS',
params: params[1]
} as ParsedSubCommand<E>
} as unknown as ParsedSubCommand<SubCommands<S>>
];
}
return ['Err', ['UNKNOWN_COMMAND', Object.keys(schema.subCommands), arg]];
};

export const parseStrings = <
E,
P extends readonly Param[],
S extends Schema<E, P>
>(
export const parseStrings = <S extends Schema>(
args: string[],
schema: S
): ['Ok', ParsedSchema<S>] | ['Err', ParseError] => {
Expand Down Expand Up @@ -225,11 +222,7 @@ export const parseStrings = <
return subCommandRes;
};

export const parseStringsOrThrow = <
E,
P extends readonly Param[],
S extends Schema<E, P>
>(
export const parseStringsOrThrow = <S extends Schema>(
args: string[],
schema: S
): ParsedSchema<S> => {
Expand Down
57 changes: 24 additions & 33 deletions src/model/command-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,12 @@ export type ParamsValues<S> = Equal<S, readonly Param[]> extends true
* @export
* @interface SubCommand
*/
export interface SubCommand<P> {
export interface SubCommand {
type: 'SUB_COMMAND';
params?: P;
params?: readonly Param[];
}

export const isValidSubCommand = <P extends readonly Param[]>(
sc: SubCommand<P>
): boolean => {
export const isValidSubCommand = (sc: SubCommand): boolean => {
if (!sc.params) {
return true;
}
Expand All @@ -156,29 +154,24 @@ export const isValidSubCommand = <P extends readonly Param[]>(
return lastRequiredParam < firstOptionalParam;
};

export const assertValidSubCommand = <P extends readonly Param[]>(
sc: SubCommand<P>
): void => {
export const assertValidSubCommand = (sc: SubCommand): void => {
if (!isValidSubCommand(sc)) {
console.dir(sc);
throw new Error('assertion failure');
}
};

export type SubCommandEntries = Record<
string,
SubCommand<readonly Param[]> | SubCommandGroup<Record<string, unknown>>
>;
export type SubCommandEntries = Record<string, SubCommand | SubCommandGroup>;

/**
* サブコマンドが属するグループ。これ単体ではコマンドとして実行できない。
*
* @export
* @interface SubCommandGroup
*/
export interface SubCommandGroup<S> {
export interface SubCommandGroup {
type: 'SUB_COMMAND_GROUP';
subCommands: Readonly<S>;
subCommands: SubCommandEntries;
}

/**
Expand All @@ -189,10 +182,10 @@ export interface SubCommandGroup<S> {
* @export
* @interface Schema
*/
export interface Schema<S = Record<string, unknown>, P = readonly Param[]> {
export interface Schema {
names: readonly string[];
subCommands: Readonly<S>;
params?: P;
subCommands: SubCommandEntries;
params?: readonly Param[];
}

/**
Expand All @@ -215,23 +208,19 @@ export type ParsedSchema<S extends Schema> = {
* @typedef ParsedParameter
* @template S コマンドスキーマの型
*/
export type ParsedParameter<S> = S extends SubCommand<infer P>
export type ParsedParameter<S> = S extends SubCommand
? {
type: 'PARAMS';
params: ParamsValues<P>;
params: ParamsValues<Params<S>>;
}
: S extends SubCommandGroup
? {
type: 'SUB_COMMAND';
subCommand: ParsedSubCommand<SubCommands<S>>;
}
: S extends SubCommandGroup<infer R>
? R extends SubCommandEntries
? {
type: 'SUB_COMMAND';
subCommand: ParsedSubCommand<R>;
}
: never
: never;

export type HasSubCommand =
| Schema<Record<string, never>>
| SubCommandGroup<Record<string, never>>;
export type HasSubCommand = Schema | SubCommandGroup;

/**
* コマンドのスキーマ `S` のサブコマンドのみに対応するパース結果の型を返す。
Expand All @@ -246,10 +235,12 @@ export type ParsedSubCommand<E> = {
} & ParsedParameter<E[K]>;
}[keyof E];

export type SubCommands<S> = S extends Schema<infer C>
? C
: S extends SubCommandGroup<infer C>
? C
export type Params<S> = S extends { params: readonly Param[] }
? S['params']
: never;

export type SubCommands<S> = S extends { subCommands: SubCommandEntries }
? S['subCommands']
: never;

/**
Expand Down
6 changes: 2 additions & 4 deletions src/service/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from './command/kaere.js';
import { KokuseiChousa, MemberStats } from './command/kokusei-chousa.js';
import { MembersWithRoleRepository, RoleRank } from './command/role-rank.js';
import type { Param, Schema } from '../model/command-schema.js';
import { Ping, PingCommand } from './command/ping.js';
import { RoleCreate, RoleCreateManager } from './command/role-create.js';
import { RoleInfo, RoleStatsRepository } from './command/role-info.js';
Expand All @@ -29,6 +28,7 @@ import type { CommandResponder } from './command/command-message.js';
import type { CommandRunner } from '../runner/command.js';
import { HelpCommand } from './command/help.js';
import { Meme } from './command/meme.js';
import type { Schema } from '../model/command-schema.js';
import type { VoiceConnectionFactory } from './voice-connection.js';

export const registerAllCommandResponder = ({
Expand Down Expand Up @@ -99,9 +99,7 @@ export const registerAllCommandResponder = ({
];
for (const responder of allResponders) {
commandRunner.addResponder(
responder as unknown as CommandResponder<
Schema<Record<string, unknown>, readonly Param[]>
>
responder as unknown as CommandResponder<Schema>
);
}
};
6 changes: 3 additions & 3 deletions src/service/command/command-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Snowflake } from '../../model/id.js';
* @interface CommandMessage
* @template S スキーマの型
*/
export interface CommandMessage<S extends Schema<Record<string, unknown>>> {
export interface CommandMessage<S extends Schema> {
/**
* コマンドの送信者の ID。
*
Expand Down Expand Up @@ -96,13 +96,13 @@ export interface HelpInfo {
description: string;
}

export interface CommandResponder<S extends Schema<Record<string, unknown>>> {
export interface CommandResponder<S extends Schema> {
help: Readonly<HelpInfo>;
schema: Readonly<S>;
on(message: CommandMessage<S>): Promise<void>;
}

export const createMockMessage = <S extends Schema<Record<string, unknown>>>(
export const createMockMessage = <S extends Schema>(
args: Readonly<ParsedSchema<S>>,
reply?: (message: EmbedMessage) => void | Promise<SentMessage | void>,
partial?: Readonly<Partial<Omit<CommandMessage<S>, 'reply'>>>
Expand Down
6 changes: 2 additions & 4 deletions src/service/command/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type {
CommandResponder,
HelpInfo
} from './command-message.js';
import type { Param, Schema } from '../../model/command-schema.js';

import type { CommandRunner } from '../../runner/command.js';
import type { EmbedPage } from '../../model/embed-message.js';
import type { Schema } from '../../model/command-schema.js';

const SCHEMA = {
names: ['help', 'h'],
Expand Down Expand Up @@ -37,9 +37,7 @@ export class HelpCommand implements CommandResponder<typeof SCHEMA> {
description,
names,
params
}: Readonly<
HelpInfo & Schema<Record<string, unknown>, readonly Param[]>
>): EmbedPage {
}: Readonly<HelpInfo & Schema>): EmbedPage {
const patternsWithDesc: [string, string][] =
params?.map(({ name, description, defaultValue }) => [
defaultValue === undefined
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3007,10 +3007,10 @@ type-fest@^1.0.1, type-fest@^1.2.1, type-fest@^1.2.2:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1"
integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==

typescript@^4.8.4:
version "4.8.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==
typescript@^4.9.3:
version "4.9.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.3.tgz#3aea307c1746b8c384435d8ac36b8a2e580d85db"
integrity sha512-CIfGzTelbKNEnLpLdGFgdyKhG23CKdKgQPOBc+OUNrkJ2vr+KSzsSV5kq5iWhEQbok+quxgGzrAtGWCyU7tHnA==

uglify-js@^3.1.4:
version "3.16.3"
Expand Down