Skip to content

Commit

Permalink
Remove explicit any types from the codebase (#208)
Browse files Browse the repository at this point in the history
This change removes all use of `any` from the code and updates the `no-explicit-any` eslint rule to be an error.
  • Loading branch information
petebacondarwin authored Jan 7, 2022
1 parent d381fed commit fe4b099
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 32 deletions.
7 changes: 7 additions & 0 deletions .changeset/cyan-crews-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

Remove explicit `any` types from the codebase

This change removes all use of `any` from the code and updates the `no-explicit-any` eslint rule to be an error.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"cSpell.words": [
"cfetch",
"esbuild",
"estree",
"execa",
"iarna",
"keyvalue",
"middlewares",
"Miniflare",
"outfile",
"Positionals"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
],
"no-shadow": "error",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-explicit-any": "error",
"no-empty": "off",
"require-yield": "off",
"no-empty-function": "off",
Expand Down
18 changes: 7 additions & 11 deletions packages/wrangler/pages/functions/filepath-routing.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import path from "path";
import fs from "fs/promises";
import { transform } from "esbuild";
import * as acorn from "acorn";
import * as acornWalk from "acorn-walk";
import type { Config } from "./routes";
import type { Identifier } from "estree";
import type { ExportNamedDeclaration } from "@babel/types";
import type { Config, RouteConfig } from "./routes";
import type { ExportNamedDeclaration, Identifier } from "estree";

type Arguments = {
baseDir: string;
Expand All @@ -18,10 +15,7 @@ export async function generateConfigFromFileTree({
baseDir,
baseURL,
}: Arguments) {
let routeEntries: [
string,
{ [key in "module" | "middleware"]?: string[] }
][] = [] as any;
let routeEntries: [string, RouteConfig][] = [];

if (!baseURL.startsWith("/")) {
baseURL = `/${baseURL}`;
Expand All @@ -45,8 +39,10 @@ export async function generateConfigFromFileTree({
sourceType: "module",
});
acornWalk.simple(ast, {
ExportNamedDeclaration(_node) {
const node: ExportNamedDeclaration = _node as any;
ExportNamedDeclaration(_node: unknown) {
// This dynamic cast assumes that the AST generated by acornWalk will generate nodes that
// are compatible with the eslint AST nodes.
const node = _node as ExportNamedDeclaration;

// this is an array because multiple things can be exported from a single statement
// i.e. `export {foo, bar}` or `export const foo = "f", bar = "b"`
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/pages/functions/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const validIdentifierRegex = new RegExp(
"u"
);

export const isValidIdentifer = (identifier: string) =>
export const isValidIdentifier = (identifier: string) =>
validIdentifierRegex.test(identifier);

export const normalizeIdentifier = (identifier: string) =>
Expand Down
20 changes: 10 additions & 10 deletions packages/wrangler/pages/functions/routes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import path from "path";
import fs from "fs/promises";
import { isValidIdentifer, normalizeIdentifier } from "./identifiers";
import { isValidIdentifier, normalizeIdentifier } from "./identifiers";

export const HTTP_METHODS = [
"HEAD",
Expand All @@ -17,7 +15,7 @@ export type HTTPMethod = typeof HTTP_METHODS[number];
export function isHTTPMethod(
maybeHTTPMethod: string
): maybeHTTPMethod is HTTPMethod {
return HTTP_METHODS.includes(maybeHTTPMethod as any);
return (HTTP_METHODS as readonly string[]).includes(maybeHTTPMethod);
}

export type RoutesCollection = Array<{
Expand All @@ -29,14 +27,16 @@ export type RoutesCollection = Array<{

export type Config = {
routes?: RoutesConfig;
schedules?: any;
schedules?: unknown;
};

export type RoutesConfig = {
[route: string]: {
middleware?: string | string[];
module?: string | string[];
};
[route: string]: RouteConfig;
};

export type RouteConfig = {
middleware?: string | string[];
module?: string | string[];
};

type ImportMap = Map<
Expand Down Expand Up @@ -93,7 +93,7 @@ export function parseConfig(config: Config, baseDir: string) {
}

// ensure the module name (if provided) is a valid identifier to guard against injection attacks
if (name !== "default" && !isValidIdentifer(name)) {
if (name !== "default" && !isValidIdentifier(name)) {
throw new Error(`Invalid module identifier "${name}"`);
}

Expand Down
18 changes: 10 additions & 8 deletions packages/wrangler/pages/functions/template-worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { match } from "path-to-regexp";
import type { HTTPMethod } from "./routes";

/* TODO: Grab these from @cloudflare/workers-types instead */
type Params<P extends string = any> = Record<P, string | string[]>;
type Params<P extends string = string> = Record<P, string | string[]>;

type EventContext<Env, P extends string, Data> = {
request: Request;
waitUntil: (promise: Promise<any>) => void;
waitUntil: (promise: Promise<unknown>) => void;
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
env: Env & { ASSETS: { fetch: typeof fetch } };
params: Params<P>;
Expand All @@ -17,7 +15,7 @@ type EventContext<Env, P extends string, Data> = {

declare type PagesFunction<
Env = unknown,
P extends string = any,
P extends string = string,
Data extends Record<string, unknown> = Record<string, unknown>
> = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
/* end @cloudflare/workers-types */
Expand All @@ -34,12 +32,12 @@ declare const routes: RouteHandler[];

// expect an ASSETS fetcher binding pointing to the asset-server stage
type Env = {
[name: string]: any;
[name: string]: unknown;
ASSETS: { fetch(url: string, init: RequestInit): Promise<Response> };
};

type WorkerContext = {
waitUntil: (promise: Promise<any>) => void;
waitUntil: (promise: Promise<unknown>) => void;
};

function* executeRequest(request: Request, env: Env) {
Expand Down Expand Up @@ -107,7 +105,11 @@ export default {
const { value } = handlerIterator.next();
if (value) {
const { handler, params } = value;
const context: EventContext<unknown, any, any> = {
const context: EventContext<
unknown,
string,
Record<string, unknown>
> = {
request: new Request(request.clone()),
next,
params,
Expand Down
3 changes: 1 addition & 2 deletions packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,5 @@ function throwFetchError(
}

function hasCursor(result_info: unknown): result_info is { cursor: string } {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (result_info as any)?.cursor !== undefined;
return (result_info as { cursor } | undefined)?.cursor !== undefined;
}

0 comments on commit fe4b099

Please sign in to comment.