From bca35c9eb25472d6b12ba39fa2c24e4b364520d0 Mon Sep 17 00:00:00 2001 From: Kyle Corbitt Date: Mon, 26 Jun 2023 23:40:05 -0700 Subject: [PATCH] tighter types and linting --- .eslintrc.cjs | 8 ++++++- .vscode/settings.json | 6 +++++ package.json | 1 + pnpm-lock.yaml | 23 +++++++++++++++++++ src/codegen/export-openai-schema.ts | 2 +- .../OutputsTable/NewVariantButton.tsx | 2 +- src/components/OutputsTable/OutputCell.tsx | 2 +- src/components/OutputsTable/VariantHeader.tsx | 2 +- src/components/OutputsTable/types.ts | 2 +- src/components/nav/AppShell.tsx | 5 +--- src/env.mjs | 8 +++---- src/pages/experiments/[id].tsx | 3 +-- src/server/ScenarioHeader.tsx | 6 ++--- src/server/api/routers/experiments.router.ts | 2 +- src/server/api/routers/modelOutputs.router.ts | 11 +++++---- .../api/routers/promptVariants.router.ts | 2 +- src/server/api/routers/scenarios.router.ts | 2 +- .../api/routers/templateVariables.router.ts | 2 +- src/server/utils/fillTemplate.ts | 2 +- src/server/utils/openai.ts | 2 +- 20 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.eslintrc.cjs b/.eslintrc.cjs index bd5e490..c6b11d3 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -20,7 +20,7 @@ const config = { parserOptions: { project: path.join(__dirname, "tsconfig.json"), }, - plugins: ["@typescript-eslint"], + plugins: ["@typescript-eslint", "unused-imports"], extends: ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"], rules: { "@typescript-eslint/consistent-type-imports": [ @@ -31,6 +31,12 @@ const config = { }, ], "@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }], + + "unused-imports/no-unused-imports": "error", + "unused-imports/no-unused-vars": [ + "warn", + { vars: "all", varsIgnorePattern: "^_", args: "after-used", argsIgnorePattern: "^_" }, + ], }, }; diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a9d566c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "eslint.format.enable": true, + "editor.codeActionsOnSave": { + "source.fixAll.eslint": true + } +} diff --git a/package.json b/package.json index 9b88257..99b1cc8 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@typescript-eslint/parser": "^5.59.6", "eslint": "^8.40.0", "eslint-config-next": "^13.4.2", + "eslint-plugin-unused-imports": "^2.0.0", "prisma": "^4.14.0", "typescript": "^5.0.4", "yaml": "^2.3.1" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bf73dcd..aba0b2c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -124,6 +124,9 @@ devDependencies: eslint-config-next: specifier: ^13.4.2 version: 13.4.2(eslint@8.40.0)(typescript@5.0.4) + eslint-plugin-unused-imports: + specifier: ^2.0.0 + version: 2.0.0(@typescript-eslint/eslint-plugin@5.59.6)(eslint@8.40.0) prisma: specifier: ^4.14.0 version: 4.14.0 @@ -2968,6 +2971,26 @@ packages: string.prototype.matchall: 4.0.8 dev: true + /eslint-plugin-unused-imports@2.0.0(@typescript-eslint/eslint-plugin@5.59.6)(eslint@8.40.0): + resolution: {integrity: sha512-3APeS/tQlTrFa167ThtP0Zm0vctjr4M44HMpeg1P4bK6wItarumq0Ma82xorMKdFsWpphQBlRPzw/pxiVELX1A==} + engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + peerDependencies: + '@typescript-eslint/eslint-plugin': ^5.0.0 + eslint: ^8.0.0 + peerDependenciesMeta: + '@typescript-eslint/eslint-plugin': + optional: true + dependencies: + '@typescript-eslint/eslint-plugin': 5.59.6(@typescript-eslint/parser@5.59.6)(eslint@8.40.0)(typescript@5.0.4) + eslint: 8.40.0 + eslint-rule-composer: 0.3.0 + dev: true + + /eslint-rule-composer@0.3.0: + resolution: {integrity: sha512-bt+Sh8CtDmn2OajxvNO+BX7Wn4CIWMpTRm3MaiKPCQcnnlm0CS2mhui6QaoeQugs+3Kj2ESKEEGJUdVafwhiCg==} + engines: {node: '>=4.0.0'} + dev: true + /eslint-scope@5.1.1: resolution: {integrity: sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==} engines: {node: '>=8.0.0'} diff --git a/src/codegen/export-openai-schema.ts b/src/codegen/export-openai-schema.ts index 9fcef20..f06aa83 100644 --- a/src/codegen/export-openai-schema.ts +++ b/src/codegen/export-openai-schema.ts @@ -5,7 +5,7 @@ import fs from "fs"; import path from "path"; import { openapiSchemaToJsonSchema } from "@openapi-contrib/openapi-schema-to-json-schema"; import assert from "assert"; -import { AcceptibleInputSchema } from "@openapi-contrib/openapi-schema-to-json-schema/dist/mjs/openapi-schema-types"; +import { type AcceptibleInputSchema } from "@openapi-contrib/openapi-schema-to-json-schema/dist/mjs/openapi-schema-types"; const OPENAPI_URL = "https://raw.githubusercontent.com/openai/openai-openapi/0c432eb66fd0c758fd8b9bd69db41c1096e5f4db/openapi.yaml"; diff --git a/src/components/OutputsTable/NewVariantButton.tsx b/src/components/OutputsTable/NewVariantButton.tsx index 8cbbef0..34fac45 100644 --- a/src/components/OutputsTable/NewVariantButton.tsx +++ b/src/components/OutputsTable/NewVariantButton.tsx @@ -1,4 +1,4 @@ -import { Button, Tooltip } from "@chakra-ui/react"; +import { Button } from "@chakra-ui/react"; import { BsPlus } from "react-icons/bs"; import { api } from "~/utils/api"; import { useExperiment, useHandledAsyncCallback } from "~/utils/hooks"; diff --git a/src/components/OutputsTable/OutputCell.tsx b/src/components/OutputsTable/OutputCell.tsx index f45d915..f4d4865 100644 --- a/src/components/OutputsTable/OutputCell.tsx +++ b/src/components/OutputsTable/OutputCell.tsx @@ -1,5 +1,5 @@ import { api } from "~/utils/api"; -import { PromptVariant, Scenario } from "./types"; +import { type PromptVariant, type Scenario } from "./types"; import { Center, Spinner, Text } from "@chakra-ui/react"; import { useExperiment } from "~/utils/hooks"; import { cellPadding } from "../constants"; diff --git a/src/components/OutputsTable/VariantHeader.tsx b/src/components/OutputsTable/VariantHeader.tsx index 86d97bd..e448622 100644 --- a/src/components/OutputsTable/VariantHeader.tsx +++ b/src/components/OutputsTable/VariantHeader.tsx @@ -1,4 +1,4 @@ -import { useRef, useState, type DragEvent } from "react"; +import { useState, type DragEvent } from "react"; import { type PromptVariant } from "./types"; import { api } from "~/utils/api"; import { useHandledAsyncCallback } from "~/utils/hooks"; diff --git a/src/components/OutputsTable/types.ts b/src/components/OutputsTable/types.ts index 10f7623..0a7a895 100644 --- a/src/components/OutputsTable/types.ts +++ b/src/components/OutputsTable/types.ts @@ -1,4 +1,4 @@ -import { RouterOutputs } from "~/utils/api"; +import { type RouterOutputs } from "~/utils/api"; export type PromptVariant = NonNullable[0]; diff --git a/src/components/nav/AppShell.tsx b/src/components/nav/AppShell.tsx index f8230cd..45136a5 100644 --- a/src/components/nav/AppShell.tsx +++ b/src/components/nav/AppShell.tsx @@ -1,15 +1,12 @@ import { Box, Flex, - Stack, - Button, Heading, VStack, Icon, HStack, - BoxProps, + type BoxProps, forwardRef, - Divider, Image, } from "@chakra-ui/react"; import Head from "next/head"; diff --git a/src/env.mjs b/src/env.mjs index 30fa238..1c9bf6c 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -10,19 +10,18 @@ export const env = createEnv({ DATABASE_URL: z.string().url(), NODE_ENV: z.enum(["development", "test", "production"]), NEXTAUTH_SECRET: - process.env.NODE_ENV === "production" - ? z.string().min(1) - : z.string().min(1).optional(), + process.env.NODE_ENV === "production" ? z.string().min(1) : z.string().min(1).optional(), NEXTAUTH_URL: z.preprocess( // This makes Vercel deployments not fail if you don't set NEXTAUTH_URL // Since NextAuth.js automatically uses the VERCEL_URL if present. (str) => process.env.VERCEL_URL ?? str, // VERCEL_URL doesn't include `https` so it cant be validated as a URL - process.env.VERCEL ? z.string().min(1) : z.string().url(), + process.env.VERCEL ? z.string().min(1) : z.string().url() ), // Add `.min(1) on ID and SECRET if you want to make sure they're not empty DISCORD_CLIENT_ID: z.string(), DISCORD_CLIENT_SECRET: z.string(), + OPENAI_API_KEY: z.string().min(1), }, /** @@ -45,6 +44,7 @@ export const env = createEnv({ NEXTAUTH_URL: process.env.NEXTAUTH_URL, DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET, + OPENAI_API_KEY: process.env.OPENAI_API_KEY, }, /** * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. diff --git a/src/pages/experiments/[id].tsx b/src/pages/experiments/[id].tsx index 877b531..b3219d4 100644 --- a/src/pages/experiments/[id].tsx +++ b/src/pages/experiments/[id].tsx @@ -9,10 +9,9 @@ import { HStack, Icon, Input, - Tooltip, } from "@chakra-ui/react"; import { useRouter } from "next/router"; -import { useState, useRef, useEffect } from "react"; +import { useState, useEffect } from "react"; import { BsTrash } from "react-icons/bs"; import { RiFlaskLine } from "react-icons/ri"; import OutputsTable from "~/components/OutputsTable"; diff --git a/src/server/ScenarioHeader.tsx b/src/server/ScenarioHeader.tsx index cc2952f..ef72ee3 100644 --- a/src/server/ScenarioHeader.tsx +++ b/src/server/ScenarioHeader.tsx @@ -1,6 +1,6 @@ -import { Text, Box, Button, HStack, Heading, Icon, Input, Stack, Code } from "@chakra-ui/react"; +import { Text, Button, HStack, Heading, Icon, Input, Stack, Code } from "@chakra-ui/react"; import { useState } from "react"; -import { BsCheck, BsChevronDown, BsChevronUp, BsX } from "react-icons/bs"; +import { BsCheck, BsChevronDown, BsX } from "react-icons/bs"; import { cellPadding } from "~/components/constants"; import { api } from "~/utils/api"; import { useExperiment, useHandledAsyncCallback } from "~/utils/hooks"; @@ -85,7 +85,7 @@ export default function ScenarioHeader() { - {vars.map((variable, i) => ( + {vars.map((variable) => ( { diff --git a/src/server/api/routers/scenarios.router.ts b/src/server/api/routers/scenarios.router.ts index a121f20..e574bb3 100644 --- a/src/server/api/routers/scenarios.router.ts +++ b/src/server/api/routers/scenarios.router.ts @@ -1,5 +1,5 @@ import { z } from "zod"; -import { createTRPCRouter, publicProcedure, protectedProcedure } from "~/server/api/trpc"; +import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; import { prisma } from "~/server/db"; export const scenariosRouter = createTRPCRouter({ diff --git a/src/server/api/routers/templateVariables.router.ts b/src/server/api/routers/templateVariables.router.ts index a87d100..6762112 100644 --- a/src/server/api/routers/templateVariables.router.ts +++ b/src/server/api/routers/templateVariables.router.ts @@ -1,5 +1,5 @@ import { z } from "zod"; -import { createTRPCRouter, publicProcedure, protectedProcedure } from "~/server/api/trpc"; +import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; import { prisma } from "~/server/db"; export const templateVarsRouter = createTRPCRouter({ diff --git a/src/server/utils/fillTemplate.ts b/src/server/utils/fillTemplate.ts index d39cdce..923652c 100644 --- a/src/server/utils/fillTemplate.ts +++ b/src/server/utils/fillTemplate.ts @@ -1,4 +1,4 @@ -import { JSONSerializable } from "../types"; +import { type JSONSerializable } from "../types"; export type VariableMap = Record; diff --git a/src/server/utils/openai.ts b/src/server/utils/openai.ts index b83967a..7696905 100644 --- a/src/server/utils/openai.ts +++ b/src/server/utils/openai.ts @@ -1,4 +1,4 @@ -import { JSONSerializable } from "../types"; +import { type JSONSerializable } from "../types"; export async function getChatCompletion(payload: JSONSerializable, apiKey: string) { const response = await fetch("https://api.openai.com/v1/chat/completions", {