Skip to content

Commit c50dde4

Browse files
committed
fixes type errors that started to complain for some reason
1 parent 04b8432 commit c50dde4

File tree

7 files changed

+70
-48
lines changed

7 files changed

+70
-48
lines changed

packages/core/src/generators/mutator-info.ts

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path';
22

33
import { type ecmaVersion, Parser, type Program } from 'acorn';
44
import { build, type BuildOptions } from 'esbuild';
5+
import { isArray } from 'remeda';
56

67
import type {
78
GeneratorMutatorParsingInfo,
@@ -101,7 +102,9 @@ function parseFunction(
101102
): GeneratorMutatorParsingInfo | undefined {
102103
const node = ast.body.find((childNode) => {
103104
if (childNode.type === 'VariableDeclaration') {
104-
return childNode.declarations.find((d) => d.id.name === funcName);
105+
return childNode.declarations.find(
106+
(d) => d.id.type === 'Identifier' && d.id.name === funcName,
107+
);
105108
}
106109
if (
107110
childNode.type === 'FunctionDeclaration' &&
@@ -121,7 +124,7 @@ function parseFunction(
121124
);
122125

123126
// If the function directly returns an arrow function
124-
if (returnStatement?.argument?.params) {
127+
if (returnStatement?.argument && 'params' in returnStatement.argument) {
125128
return {
126129
numberOfParams: node.params.length,
127130
returnNumberOfParams: returnStatement.argument.params.length,
@@ -141,42 +144,59 @@ function parseFunction(
141144
};
142145
}
143146

144-
const declaration = node.declarations.find((d) => d.id.name === funcName);
147+
const declaration =
148+
'declarations' in node
149+
? node.declarations.find(
150+
(d) => d.id.type === 'Identifier' && d.id.name === funcName,
151+
)
152+
: undefined;
145153

146-
if (declaration.init.name) {
147-
return parseFunction(ast, declaration.init.name);
148-
}
154+
if (declaration?.init) {
155+
if ('name' in declaration.init) {
156+
return parseFunction(ast, declaration.init.name);
157+
}
149158

150-
if (declaration.init.body.type === 'ArrowFunctionExpression') {
151-
return {
152-
numberOfParams: declaration.init.params.length,
153-
returnNumberOfParams: declaration.init.body.params.length,
154-
};
155-
}
159+
if (
160+
'body' in declaration.init &&
161+
'params' in declaration.init &&
162+
declaration.init.body.type === 'ArrowFunctionExpression'
163+
) {
164+
return {
165+
numberOfParams: declaration.init.params.length,
166+
returnNumberOfParams: declaration.init.body.params.length,
167+
};
168+
}
156169

157-
const returnStatement = declaration.init.body?.body?.find(
158-
(b: any) => b.type === 'ReturnStatement',
159-
);
170+
const returnStatement =
171+
'body' in declaration.init &&
172+
'body' in declaration.init.body &&
173+
isArray(declaration.init.body.body)
174+
? declaration.init.body.body.find((b) => b.type === 'ReturnStatement')
175+
: undefined;
160176

161-
if (returnStatement?.argument?.params) {
162-
return {
163-
numberOfParams: declaration.init.params.length,
164-
returnNumberOfParams: returnStatement.argument.params.length,
165-
};
166-
} else if (
167-
returnStatement?.argument?.type === 'CallExpression' &&
168-
returnStatement.argument.arguments?.[0]?.type === 'ArrowFunctionExpression'
169-
) {
170-
const arrowFn = returnStatement.argument.arguments[0];
171-
return {
172-
numberOfParams: declaration.init.params.length,
173-
returnNumberOfParams: arrowFn.params.length,
174-
};
175-
}
177+
if ('params' in declaration.init) {
178+
if (returnStatement?.argument && 'params' in returnStatement.argument) {
179+
return {
180+
numberOfParams: declaration.init.params.length,
181+
returnNumberOfParams: returnStatement.argument.params.length,
182+
};
183+
} else if (
184+
returnStatement?.argument?.type === 'CallExpression' &&
185+
returnStatement.argument.arguments[0]?.type ===
186+
'ArrowFunctionExpression'
187+
) {
188+
const arrowFn = returnStatement.argument.arguments[0];
189+
return {
190+
numberOfParams: declaration.init.params.length,
191+
returnNumberOfParams: arrowFn.params.length,
192+
};
193+
}
176194

177-
return {
178-
numberOfParams: declaration.init.params.length,
179-
};
195+
return {
196+
numberOfParams: declaration.init.params.length,
197+
};
198+
}
199+
}
180200
}
181201

182202
function getEcmaVersion(target?: TsConfigTarget): ecmaVersion | undefined {

packages/core/src/generators/verbs-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ const generateVerbOptions = async ({
6666
summary,
6767
} = operation;
6868
const operationId = getOperationId(operation, route, verb);
69-
const overrideOperation = output.override.operations[operation.operationId!];
69+
const overrideOperation = output.override.operations[operation.operationId!]!;
7070
const overrideTag = Object.entries(
7171
output.override.tags,
7272
).reduce<NormalizedOperationOptions>(
7373
(acc, [tag, options]) =>
74-
tags.includes(tag) ? mergeDeep(acc, options) : acc,
74+
tags.includes(tag) && options ? mergeDeep(acc, options) : acc,
7575
{},
7676
);
7777

packages/core/src/writers/single-mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const writeSingleMode = async ({
7878
isAllowSyntheticDefaultImports,
7979
hasGlobalMutator: !!output.override.mutator,
8080
hasTagsMutator: Object.values(output.override.tags).some(
81-
(tag) => !!tag.mutator,
81+
(tag) => !!tag?.mutator,
8282
),
8383
hasParamsSerializerOptions: !!output.override.paramsSerializerOptions,
8484
packageJson: output.packageJson,

packages/core/src/writers/split-mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const writeSplitMode = async ({
7676
isAllowSyntheticDefaultImports,
7777
hasGlobalMutator: !!output.override.mutator,
7878
hasTagsMutator: Object.values(output.override.tags).some(
79-
(tag) => !!tag.mutator,
79+
(tag) => !!tag?.mutator,
8080
),
8181
hasParamsSerializerOptions: !!output.override.paramsSerializerOptions,
8282
packageJson: output.packageJson,

packages/core/src/writers/split-tags-mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const writeSplitTagsMode = async ({
9090
isAllowSyntheticDefaultImports,
9191
hasGlobalMutator: !!output.override.mutator,
9292
hasTagsMutator: Object.values(output.override.tags).some(
93-
(tag) => !!tag.mutator,
93+
(tag) => !!tag?.mutator,
9494
),
9595
hasParamsSerializerOptions: !!output.override.paramsSerializerOptions,
9696
packageJson: output.packageJson,

packages/core/src/writers/tags-mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const writeTagsMode = async ({
7878
isAllowSyntheticDefaultImports,
7979
hasGlobalMutator: !!output.override.mutator,
8080
hasTagsMutator: Object.values(output.override.tags).some(
81-
(tag) => !!tag.mutator,
81+
(tag) => !!tag?.mutator,
8282
),
8383
hasParamsSerializerOptions: !!output.override.paramsSerializerOptions,
8484
packageJson: output.packageJson,

packages/zod/src/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ import {
2222
type ZodCoerceType,
2323
} from '@orval/core';
2424
import {
25-
isSchemaObject,
2625
type ParameterObject,
2726
type PathItemObject,
2827
type ReferenceObject,
2928
type RequestBodyObject,
3029
type ResponseObject,
3130
type SchemaObject,
3231
} from 'openapi3-ts/oas30';
33-
import {
34-
isSchemaObject as isSchemaObject31,
35-
type SchemaObject as SchemaObject31,
36-
} from 'openapi3-ts/oas31';
32+
import { type SchemaObject as SchemaObject31 } from 'openapi3-ts/oas31';
3733
import { unique } from 'remeda';
3834

3935
import {
@@ -805,8 +801,10 @@ ${Object.entries(mergedProperties)
805801
}
806802
if (fn === 'oneOf' || fn === 'anyOf') {
807803
// Can't use zod.union() with a single item
808-
if (args.length === 1) {
809-
return args[0].functions.map((prop) => parseProperty(prop)).join('');
804+
if (args?.length === 1) {
805+
return args[0].functions
806+
.map((prop: any) => parseProperty(prop))
807+
.join('');
810808
}
811809

812810
const union = args.map(
@@ -829,7 +827,9 @@ ${Object.entries(mergedProperties)
829827
}
830828

831829
if (fn === 'additionalProperties') {
832-
const value = args.functions.map((prop) => parseProperty(prop)).join('');
830+
const value = args.functions
831+
.map((prop: any) => parseProperty(prop))
832+
.join('');
833833
const valueWithZod = `${value.startsWith('.') ? 'zod' : ''}${value}`;
834834
consts += args.consts;
835835
return `zod.record(zod.string(), ${valueWithZod})`;
@@ -851,7 +851,9 @@ ${Object.entries(args)
851851
})`;
852852
}
853853
if (fn === 'array') {
854-
const value = args.functions.map((prop) => parseProperty(prop)).join('');
854+
const value = args.functions
855+
.map((prop: any) => parseProperty(prop))
856+
.join('');
855857
if (typeof args.consts === 'string') {
856858
consts += args.consts;
857859
} else if (Array.isArray(args.consts)) {

0 commit comments

Comments
 (0)