From 5fcef05bbd8d046e29bbf61ab6aa84906ff077e1 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 20 Jan 2022 13:37:12 +0000 Subject: [PATCH] refactor: enable TypeScript strict-null checks The codebase is now strict-null compliant and the CI checks will fail if a PR tries to introduce code that is not. --- .changeset/quiet-pianos-try.md | 7 +++++++ .../wrangler/pages/functions/filepath-routing.ts | 4 +++- packages/wrangler/src/__tests__/dev.test.tsx | 2 +- packages/wrangler/src/cfetch/internal.ts | 2 +- packages/wrangler/src/inspect.ts | 12 ++++++++---- packages/wrangler/src/publish.ts | 4 +++- tsconfig.json | 3 ++- 7 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 .changeset/quiet-pianos-try.md diff --git a/.changeset/quiet-pianos-try.md b/.changeset/quiet-pianos-try.md new file mode 100644 index 000000000000..ea971fb32d8f --- /dev/null +++ b/.changeset/quiet-pianos-try.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +refactor: enable TypeScript strict-null checks + +The codebase is now strict-null compliant and the CI checks will fail if a PR tries to introduce code that is not. diff --git a/packages/wrangler/pages/functions/filepath-routing.ts b/packages/wrangler/pages/functions/filepath-routing.ts index dd37b1289678..a82aff4234c9 100644 --- a/packages/wrangler/pages/functions/filepath-routing.ts +++ b/packages/wrangler/pages/functions/filepath-routing.ts @@ -157,7 +157,9 @@ export async function generateConfigFromFileTree({ export function compareRoutes(a: string, b: string) { function parseRoutePath(routePath: string): [string | null, string[]] { const parts = routePath.split(" ", 2); - const segmentedPath = parts.pop(); + // split() will guarantee at least one element. + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const segmentedPath = parts.pop()!; const method = parts.pop() ?? null; const segments = segmentedPath.slice(1).split("/").filter(Boolean); diff --git a/packages/wrangler/src/__tests__/dev.test.tsx b/packages/wrangler/src/__tests__/dev.test.tsx index 0a8e7262d9d1..8b43c80c2bf1 100644 --- a/packages/wrangler/src/__tests__/dev.test.tsx +++ b/packages/wrangler/src/__tests__/dev.test.tsx @@ -15,7 +15,7 @@ describe("Dev component", () => { accountId: "some-account-id", public: "some/public/path", }); - expect(lastFrame().split("\n").slice(0, 2).join("\n")) + expect(lastFrame()?.split("\n").slice(0, 2).join("\n")) .toMatchInlineSnapshot(` "Something went wrong: Error: You cannot use the service worker format with a \`public\` directory." diff --git a/packages/wrangler/src/cfetch/internal.ts b/packages/wrangler/src/cfetch/internal.ts index d9c5324546bd..57704b81359d 100644 --- a/packages/wrangler/src/cfetch/internal.ts +++ b/packages/wrangler/src/cfetch/internal.ts @@ -40,7 +40,7 @@ export async function fetchInternal( } } -function cloneHeaders(headers: HeadersInit): HeadersInit { +function cloneHeaders(headers: HeadersInit | undefined): HeadersInit { return { ...headers }; } diff --git a/packages/wrangler/src/inspect.ts b/packages/wrangler/src/inspect.ts index c709b67114a5..24cd7945cf0e 100644 --- a/packages/wrangler/src/inspect.ts +++ b/packages/wrangler/src/inspect.ts @@ -470,8 +470,10 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void { case "map": args.push( "{\n" + - ro.preview.entries - .map(({ key, value }) => { + // Maps always have entries + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ro.preview + .entries!.map(({ key, value }) => { return ` ${key?.description ?? ""} => ${ value.description }`; @@ -485,8 +487,10 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void { case "set": args.push( "{ " + - ro.preview.entries - .map(({ value }) => { + // Sets always have entries + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ro.preview + .entries!.map(({ value }) => { return `${value.description}`; }) .join(", ") + diff --git a/packages/wrangler/src/publish.ts b/packages/wrangler/src/publish.ts index ffba03ef924e..c4c00b1533c4 100644 --- a/packages/wrangler/src/publish.ts +++ b/packages/wrangler/src/publish.ts @@ -155,7 +155,9 @@ export default async function publish(props: Props): Promise { const entryPointExports = entryPoints[0].exports; const resolvedEntryPointPath = path.resolve( destination.path, - entryPoints[0].entryPoint + // We know that entryPoint is not null because we filtered out those without above. + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + entryPoints[0].entryPoint! ); const { format } = props; const bundle = { diff --git a/tsconfig.json b/tsconfig.json index 8702fbd43876..8b57b04b61f4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,8 @@ "lib": ["esnext"], "jsx": "react", "resolveJsonModule": true, - "incremental": true + "incremental": true, + "strictNullChecks": true }, "exclude": ["node_modules/", "packages/*/vendor", "packages/*/*-dist"] }