Skip to content

Commit

Permalink
refactor(permission-controller): Clean up use of any (#4171)
Browse files Browse the repository at this point in the history
## Explanation

Replaces almost all `any` type casts with `@ts-expect-error` directives
or more specific types. Of the original 81 `any` type casts in the
permission controller and its tests, only 3 remain. No behavioral
changes.

## Changelog

None.
  • Loading branch information
rekmarks authored Apr 26, 2024
1 parent 46e9351 commit 843a794
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 463 deletions.
4 changes: 1 addition & 3 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,7 @@ export class AccountsController extends BaseController<
...account,
metadata: { ...account.metadata, name: accountName },
};
currentState.internalAccounts.accounts[accountId] =
// @ts-expect-error Assigning a complex type `T` to `Draft<T>` causes an excessive type instantiation depth error.
internalAccount as Draft<InternalAccount>;
currentState.internalAccounts.accounts[accountId] = internalAccount;
});
}

Expand Down
42 changes: 21 additions & 21 deletions packages/permission-controller/src/Caveat.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PermissionConstraint } from '.';
import type { Caveat, PermissionConstraint } from '.';
import { decorateWithCaveats, PermissionType } from '.';
import * as errors from './errors';

Expand All @@ -9,11 +9,11 @@ describe('decorateWithCaveats', () => {
const caveatSpecifications = {
reverse: {
type: 'reverse',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decorator: (method: any, _caveat: any) => async () => {
return (await method()).reverse();
},
decorator:
(method: () => Promise<unknown[]>, _caveat: Caveat<string, null>) =>
async () => {
return (await method()).reverse();
},
},
};

Expand Down Expand Up @@ -46,19 +46,19 @@ describe('decorateWithCaveats', () => {
const caveatSpecifications = {
reverse: {
type: 'reverse',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decorator: (method: any, _caveat: any) => async () => {
return (await method()).reverse();
},
decorator:
(method: () => Promise<unknown[]>, _caveat: Caveat<string, null>) =>
async () => {
return (await method()).reverse();
},
},
slice: {
type: 'slice',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decorator: (method: any, caveat: any) => async () => {
return (await method()).slice(0, caveat.value);
},
decorator:
(method: () => Promise<unknown[]>, caveat: Caveat<string, number>) =>
async () => {
return (await method()).slice(0, caveat.value);
},
},
};

Expand Down Expand Up @@ -114,11 +114,11 @@ describe('decorateWithCaveats', () => {
const caveatSpecifications = {
reverse: {
type: 'reverse',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decorator: (method: any, _caveat: any) => async () => {
return (await method()).reverse();
},
decorator:
(method: () => Promise<unknown[]>, _caveat: Caveat<string, null>) =>
async () => {
return (await method()).reverse();
},
},
};

Expand Down
61 changes: 26 additions & 35 deletions packages/permission-controller/src/Caveat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,16 @@ export type CaveatDecorator<ParentCaveat extends CaveatConstraint> = (
* @template Decorator - The {@link CaveatDecorator} to extract a caveat value
* type from.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ExtractCaveatValueFromDecorator<Decorator extends CaveatDecorator<any>> =
Decorator extends (
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decorated: any,
caveat: infer ParentCaveat,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => AsyncRestrictedMethod<any, any>
? ParentCaveat extends CaveatConstraint
? ParentCaveat['value']
: never
: never;
type ExtractCaveatValueFromDecorator<
Decorator extends CaveatDecorator<CaveatConstraint>,
> = Decorator extends (
decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,
caveat: infer ParentCaveat,
) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>
? ParentCaveat extends CaveatConstraint
? ParentCaveat['value']
: never
: never;

/**
* A function for validating caveats of a particular type.
Expand Down Expand Up @@ -138,9 +133,7 @@ export type RestrictedMethodCaveatSpecificationConstraint =
* The decorator function used to apply the caveat to restricted method
* requests.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decorator: CaveatDecorator<any>;
decorator: CaveatDecorator<CaveatConstraint>;
};

export type EndowmentCaveatSpecificationConstraint = CaveatSpecificationBase;
Expand Down Expand Up @@ -178,9 +171,10 @@ type CaveatSpecificationBuilderOptions<
* tailored to their requirements.
*/
export type CaveatSpecificationBuilder<
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Options extends CaveatSpecificationBuilderOptions<any, any>,
Options extends CaveatSpecificationBuilderOptions<
Record<string, unknown>,
Record<string, unknown>
>,
Specification extends CaveatSpecificationConstraint,
> = (options: Options) => Specification;

Expand All @@ -190,9 +184,10 @@ export type CaveatSpecificationBuilder<
*/
export type CaveatSpecificationBuilderExportConstraint = {
specificationBuilder: CaveatSpecificationBuilder<
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
CaveatSpecificationBuilderOptions<any, any>,
CaveatSpecificationBuilderOptions<
Record<string, unknown>,
Record<string, unknown>
>,
CaveatSpecificationConstraint
>;
decoratorHookNames?: Record<string, true>;
Expand All @@ -218,18 +213,14 @@ export type CaveatSpecificationMap<
*/
export type ExtractCaveats<
CaveatSpecification extends CaveatSpecificationConstraint,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
> = CaveatSpecification extends any
? CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint
? Caveat<
CaveatSpecification['type'],
ExtractCaveatValueFromDecorator<
RestrictedMethodCaveatSpecificationConstraint['decorator']
>
> = CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint
? Caveat<
CaveatSpecification['type'],
ExtractCaveatValueFromDecorator<
RestrictedMethodCaveatSpecificationConstraint['decorator']
>
: Caveat<CaveatSpecification['type'], Json>
: never;
>
: Caveat<CaveatSpecification['type'], Json>;

/**
* Extracts the type of a specific {@link Caveat} from a union of caveat
Expand Down
41 changes: 19 additions & 22 deletions packages/permission-controller/src/Permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ export type ValidPermission<
*/
type ExtractArrayMembers<ArrayType> = ArrayType extends []
? never
: // TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ArrayType extends any[] | readonly any[]
: ArrayType extends unknown[] | readonly unknown[]
? ArrayType[number]
: never;

Expand Down Expand Up @@ -207,9 +205,7 @@ export type RequestedPermissions = Record<TargetName, RequestedPermission>;
*/
type RestrictedMethodContext = Readonly<{
origin: OriginString;
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
[key: string]: unknown;
}>;

export type RestrictedMethodParameters = Json[] | Record<string, Json>;
Expand Down Expand Up @@ -263,9 +259,10 @@ export type RestrictedMethod<
| AsyncRestrictedMethod<Params, Result>;

export type ValidRestrictedMethod<
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
MethodImplementation extends RestrictedMethod<any, any>,
MethodImplementation extends RestrictedMethod<
RestrictedMethodParameters,
Json
>,
> = MethodImplementation extends (args: infer Options) => Json | Promise<Json>
? Options extends RestrictedMethodOptions<RestrictedMethodParameters>
? MethodImplementation
Expand Down Expand Up @@ -421,9 +418,7 @@ type PermissionSpecificationBase<Type extends PermissionType> = {
*
* If the side-effect action fails, the permission that triggered it is revoked.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sideEffect?: PermissionSideEffect<any, any>;
sideEffect?: PermissionSideEffect<ActionConstraint, EventConstraint>;

/**
* The Permission may be available to only a subset of the subject types. If so, specify the subject types as an array.
Expand All @@ -449,7 +444,7 @@ export type RestrictedMethodSpecificationConstraint =
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
methodImplementation: RestrictedMethod<any, any>;
methodImplementation: RestrictedMethod<any, Json>;
};

/**
Expand All @@ -467,9 +462,7 @@ export type EndowmentSpecificationConstraint =
* permission is invoked, after which the host can apply the endowments to
* the requesting subject in the intended manner.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
endowmentGetter: EndowmentGetter<any>;
endowmentGetter: EndowmentGetter<Json>;
};

/**
Expand Down Expand Up @@ -509,9 +502,11 @@ type PermissionSpecificationBuilderOptions<
*/
export type PermissionSpecificationBuilder<
Type extends PermissionType,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Options extends PermissionSpecificationBuilderOptions<any, any, any>,
Options extends PermissionSpecificationBuilderOptions<
Record<string, unknown>,
Record<string, unknown>,
Record<string, unknown>
>,
Specification extends PermissionSpecificationConstraint & {
permissionType: Type;
},
Expand All @@ -525,9 +520,11 @@ export type PermissionSpecificationBuilderExportConstraint = {
targetName: string;
specificationBuilder: PermissionSpecificationBuilder<
PermissionType,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
PermissionSpecificationBuilderOptions<any, any, any>,
PermissionSpecificationBuilderOptions<
Record<string, unknown>,
Record<string, unknown>,
Record<string, unknown>
>,
PermissionSpecificationConstraint
>;
factoryHookNames?: Record<string, true>;
Expand Down
Loading

0 comments on commit 843a794

Please sign in to comment.