Skip to content

Commit

Permalink
Enable (and fix) strict ESLint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Apr 9, 2023
1 parent a2e909f commit 30666a0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended"
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/strict"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint"],
"rules": {
Expand Down
4 changes: 2 additions & 2 deletions build/build-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function buildAndPack(
let entry: string;
let esm: boolean;
let packageOverrides: Record<string, unknown>;
let filesToCopy: ReadonlyArray<string>;
let filesToCopy: readonly string[];

if (middlewareToBuild) {
const middlewareDir = path.join(rootDir, "middlewares", middlewareToBuild);
Expand Down Expand Up @@ -239,7 +239,7 @@ async function readJson(path: fsOriginal.PathLike): Promise<any> {
async function copyStaticFiles({
filesToCopy,
distDir,
}: Readonly<{ filesToCopy: ReadonlyArray<string>; distDir: string }>) {
}: Readonly<{ filesToCopy: readonly string[]; distDir: string }>) {
await Promise.all(
filesToCopy.map(async (source) => {
const basename = path.basename(source);
Expand Down
2 changes: 1 addition & 1 deletion build/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as childProcess from "child_process";

export const npm = (
args: ReadonlyArray<string>,
args: readonly string[],
{ cwd }: Readonly<{ cwd: string }>
): Promise<void> =>
new Promise((resolve, reject) => {
Expand Down
15 changes: 8 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ export interface HelmetOptions {
xssFilter?: boolean;
}

interface MiddlewareFunction {
(
req: IncomingMessage,
res: ServerResponse,
next: (error?: Error) => void
): void;
}
type MiddlewareFunction = (
req: IncomingMessage,
res: ServerResponse,
next: (error?: Error) => void
) => void;

interface Helmet {
(options?: Readonly<HelmetOptions>): (
Expand Down Expand Up @@ -219,6 +217,9 @@ function getMiddlewareFunctionsFromOptions(

const helmet: Helmet = Object.assign(
function helmet(options: Readonly<HelmetOptions> = {}) {
// People should be able to pass an options object with no prototype,
// so we want this optional chaining.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (options.constructor?.name === "IncomingMessage") {
throw new Error(
"It appears you have done something like `app.use(helmet)`, but it should be `app.use(helmet())`."
Expand Down
7 changes: 4 additions & 3 deletions middlewares/content-security-policy/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IncomingMessage, ServerResponse } from "http";

interface ContentSecurityPolicyDirectiveValueFunction {
(req: IncomingMessage, res: ServerResponse): string;
}
type ContentSecurityPolicyDirectiveValueFunction = (
req: IncomingMessage,
res: ServerResponse
) => string;

type ContentSecurityPolicyDirectiveValue =
| string
Expand Down
10 changes: 6 additions & 4 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { IncomingMessage, ServerResponse } from "http";
import connect from "connect";
import supertest from "supertest";

interface MiddlewareFunction {
(req: IncomingMessage, res: ServerResponse, next: () => void): void;
}
type MiddlewareFunction = (
req: IncomingMessage,
res: ServerResponse,
next: () => void
) => void;

export async function check(
middleware: MiddlewareFunction,
expectedHeaders: Readonly<{ [headerName: string]: string | null }>
expectedHeaders: Readonly<Record<string, string | null>>
) {
const app = connect()
.use(middleware)
Expand Down

0 comments on commit 30666a0

Please sign in to comment.