Skip to content

Commit ff58d84

Browse files
authored
Tighten lint rules (#1045)
1 parent 5756dfb commit ff58d84

File tree

10 files changed

+90
-101
lines changed

10 files changed

+90
-101
lines changed

.eslintrc.cjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ module.exports = {
44
parserOptions: {
55
project: ["./tsconfig.json"],
66
},
7-
extends: ["plugin:prettier/recommended", "eslint:recommended", "plugin:@typescript-eslint/recommended"],
7+
extends: ["plugin:prettier/recommended", "eslint:recommended", "plugin:@typescript-eslint/strict"],
88
plugins: ["@typescript-eslint"],
9+
rules: {
10+
"@typescript-eslint/consistent-indexed-object-style": "off", // sometimes naming keys is more user-friendly
11+
"@typescript-eslint/no-dynamic-delete": "off", // delete is OK
12+
"@typescript-eslint/no-unnecessary-condition": "off", // this gives bad advice
13+
"no-unused-vars": "off",
14+
},
915
overrides: [
1016
{
1117
files: ["**/*.test.*"],

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@types/node": "^18.15.0",
6161
"@typescript-eslint/eslint-plugin": "^5.54.1",
6262
"@typescript-eslint/parser": "^5.54.1",
63-
"@vitest/coverage-c8": "^0.28.5",
63+
"@vitest/coverage-c8": "^0.29.2",
6464
"degit": "^2.8.4",
6565
"del-cli": "^5.0.0",
6666
"eslint": "^8.35.0",
@@ -69,7 +69,7 @@
6969
"execa": "^6.1.0",
7070
"prettier": "^2.8.4",
7171
"typescript": "^4.9.5",
72-
"vite-node": "^0.28.5",
73-
"vitest": "^0.28.5"
72+
"vite-node": "^0.29.2",
73+
"vitest": "^0.29.2"
7474
}
7575
}

pnpm-lock.yaml

Lines changed: 38 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,23 @@ async function openapiTS(
3939
options: OpenAPITSOptions = {} as Partial<OpenAPITSOptions>
4040
): Promise<string> {
4141
const ctx: GlobalContext = {
42-
additionalProperties: options.additionalProperties || false,
43-
alphabetize: options.alphabetize || false,
44-
defaultNonNullable: options.defaultNonNullable || false,
42+
additionalProperties: options.additionalProperties ?? false,
43+
alphabetize: options.alphabetize ?? false,
44+
defaultNonNullable: options.defaultNonNullable ?? false,
4545
discriminators: {},
46-
transform: options && typeof options.transform === "function" ? options.transform : undefined,
47-
postTransform: options && typeof options.postTransform === "function" ? options.postTransform : undefined,
48-
immutableTypes: options.immutableTypes || false,
49-
emptyObjectsUnknown: options.emptyObjectsUnknown || false,
46+
transform: typeof options.transform === "function" ? options.transform : undefined,
47+
postTransform: typeof options.postTransform === "function" ? options.postTransform : undefined,
48+
immutableTypes: options.immutableTypes ?? false,
49+
emptyObjectsUnknown: options.emptyObjectsUnknown ?? false,
5050
indentLv: 0,
5151
operations: {},
52-
pathParamsAsTypes: options.pathParamsAsTypes || false,
53-
silent: options.silent || false,
54-
supportArrayLength: options.supportArrayLength || false,
52+
pathParamsAsTypes: options.pathParamsAsTypes ?? false,
53+
silent: options.silent ?? false,
54+
supportArrayLength: options.supportArrayLength ?? false,
5555
};
5656

5757
// note: we may be loading many large schemas into memory at once; take care to reuse references without cloning
58-
const isInlineSchema = typeof schema !== "string" && schema instanceof URL == false;
58+
const isInlineSchema = typeof schema !== "string" && schema instanceof URL === false; // eslint-disable-line @typescript-eslint/no-unnecessary-boolean-literal-compare
5959

6060
// 1. load schema (and subschemas)
6161
const allSchemas: { [id: string]: Subschema } = {};
@@ -90,7 +90,7 @@ async function openapiTS(
9090
const output: string[] = [];
9191

9292
// 2a. Start file, inject custom code (if any)
93-
if (options && "commentHeader" in options) {
93+
if ("commentHeader" in options) {
9494
if (options.commentHeader) output.push(options.commentHeader);
9595
} else {
9696
output.push(COMMENT_HEADER);

src/load.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import yaml from "js-yaml";
1919
import type { Dispatcher } from "undici";
2020
import { parseRef, error, makeTSIndex, walk, isRemoteURL, isFilepath } from "./utils.js";
2121

22-
type SchemaMap = { [id: string]: Subschema };
22+
interface SchemaMap {
23+
[id: string]: Subschema;
24+
}
2325

2426
export const VIRTUAL_JSON_URL = `file:///_json`; // fake URL reserved for dynamic JSON
2527

@@ -109,7 +111,7 @@ export default async function load(
109111
// 1. load contents
110112
// 1a. URL
111113
if (schema instanceof URL) {
112-
const hint = options.hint || "OpenAPI3";
114+
const hint = options.hint ?? "OpenAPI3";
113115

114116
// normalize ID
115117
if (schema.href !== options.rootURL.href) schemaID = relativePath(options.rootURL, schema);
@@ -136,12 +138,12 @@ export default async function load(
136138
headers,
137139
});
138140
const contentType = res.headers.get("content-type");
139-
if (ext === ".json" || (contentType && contentType.includes("json"))) {
141+
if (ext === ".json" || contentType?.includes("json")) {
140142
options.schemas[schemaID] = {
141143
hint,
142144
schema: parseJSON(await res.text()),
143145
};
144-
} else if (ext === ".yaml" || ext === ".yml" || (contentType && contentType.includes("yaml"))) {
146+
} else if (ext === ".yaml" || ext === ".yml" || contentType?.includes("yaml")) {
145147
options.schemas[schemaID] = {
146148
hint,
147149
schema: parseYAML(await res.text()),
@@ -180,7 +182,7 @@ export default async function load(
180182
// if file starts with '{' assume JSON
181183
options.schemas[schemaID] = {
182184
hint: "OpenAPI3",
183-
schema: contents.charAt(0) === "{" ? parseJSON(contents) : parseYAML(contents),
185+
schema: contents.startsWith("{") ? parseJSON(contents) : parseYAML(contents),
184186
};
185187
}
186188
// 1c. inline

src/transform/operation-object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default function transformOperationObject(
6565
const parts = parseTSIndex(p.$ref);
6666
const paramI = parts.indexOf("parameters");
6767
if (paramI === -1 || parts[paramI + 1] !== paramIn || !parts[paramI + 2]) continue;
68-
const key = parts.pop() as string;
68+
const key = parts.pop()!;
6969
const index = makeTSIndex(parts);
7070
if (!refs[index]) refs[index] = [key];
7171
else refs[index].push(key);

src/transform/path-item-object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function transformPathItemObject(
4242
}
4343

4444
// parameters
45-
if (pathItem.parameters && pathItem.parameters.length) {
45+
if (pathItem.parameters?.length) {
4646
output.push(
4747
indent(
4848
transformOperationObject({ parameters: pathItem.parameters }, { path, ctx, wrapObject: false }).trim(),

src/transform/schema-object.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function defaultSchemaObjectTransform(
144144
return ctx.immutableTypes || schemaObject.readOnly ? tsReadonly(itemType) : itemType;
145145
} else {
146146
return tsUnionOf(
147-
...Array.from({ length: (maxItems || 0) - minItems + 1 })
147+
...Array.from({ length: (maxItems ?? 0) - minItems + 1 })
148148
.map((_, i) => i + minItems)
149149
.map((n) => {
150150
const t = tsTupleOf(...Array.from({ length: n }).map(() => itemType));
@@ -169,7 +169,7 @@ export function defaultSchemaObjectTransform(
169169
("additionalProperties" in schemaObject && schemaObject.additionalProperties)
170170
) {
171171
indentLv++;
172-
for (const [k, v] of getEntries(schemaObject.properties || {}, ctx.alphabetize)) {
172+
for (const [k, v] of getEntries(schemaObject.properties ?? {}, ctx.alphabetize)) {
173173
const c = getSchemaObjectComment(v, indentLv);
174174
if (c) coreType.push(indent(c, indentLv));
175175
let key = escObjKey(k);
@@ -204,11 +204,11 @@ export function defaultSchemaObjectTransform(
204204
);
205205
if (discriminatorRef) {
206206
const discriminator = ctx.discriminators[discriminatorRef.$ref];
207-
let value = parseRef(path).path.pop() as string;
207+
let value = parseRef(path).path.pop()!;
208208
if (discriminator.mapping) {
209209
// Mapping value can either be a fully-qualified ref (#/components/schemas/XYZ) or a schema name (XYZ)
210210
const matchedValue = Object.entries(discriminator.mapping).find(
211-
([_, v]) => (v[0] !== "#" && v === value) || (v[0] === "#" && parseRef(v).path.pop() === value)
211+
([_, v]) => (!v.startsWith("#") && v === value) || (v.startsWith("#") && parseRef(v).path.pop() === value)
212212
);
213213
if (matchedValue) value = matchedValue[0]; // why was this designed backwards!?
214214
}

0 commit comments

Comments
 (0)