Skip to content

Commit

Permalink
refactor(permission-controller): Refactor "any" typecasts
Browse files Browse the repository at this point in the history
Either replaces `any` with `@ts-expect-error` directives or leaves them
in place without a TODO to replace them. All remaining instances of
`any` are defensible.
  • Loading branch information
rekmarks committed Apr 16, 2024
1 parent bdc755f commit 69c78bb
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 278 deletions.
4 changes: 0 additions & 4 deletions packages/permission-controller/src/Caveat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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();
Expand Down Expand Up @@ -46,15 +45,13 @@ 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();
},
},
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);
Expand Down Expand Up @@ -114,7 +111,6 @@ 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();
Expand Down
8 changes: 0 additions & 8 deletions packages/permission-controller/src/Caveat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ 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
Expand Down Expand Up @@ -129,7 +126,6 @@ export type CaveatSpecificationBase = {
* performed. Although caveats can also be validated by permission validators,
* validating caveat values separately is strongly recommended.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validator?: CaveatValidator<any>;
};
Expand All @@ -140,7 +136,6 @@ 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>;
};
Expand Down Expand Up @@ -180,7 +175,6 @@ 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>,
Specification extends CaveatSpecificationConstraint,
Expand All @@ -192,7 +186,6 @@ 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>,
CaveatSpecificationConstraint
Expand Down Expand Up @@ -220,7 +213,6 @@ 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
Expand Down
21 changes: 6 additions & 15 deletions packages/permission-controller/src/Permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,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 @@ -209,9 +207,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 @@ -265,9 +261,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 @@ -404,7 +401,6 @@ type PermissionSpecificationBase<Type extends PermissionType> = {
* used, and the validator function (if specified) will be called on newly
* constructed permissions.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
factory?: PermissionFactory<any, Record<string, unknown>>;

Expand All @@ -423,7 +419,6 @@ 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>;

Expand All @@ -449,7 +444,6 @@ export type RestrictedMethodSpecificationConstraint =
* The implementation of the restricted method that the permission
* corresponds to.
*/
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
methodImplementation: RestrictedMethod<any, any>;
};
Expand All @@ -469,7 +463,6 @@ 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>;
};
Expand Down Expand Up @@ -511,7 +504,6 @@ 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>,
Specification extends PermissionSpecificationConstraint & {
Expand All @@ -527,7 +519,6 @@ 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>,
PermissionSpecificationConstraint
Expand Down
Loading

0 comments on commit 69c78bb

Please sign in to comment.