Skip to content

Commit

Permalink
Merge pull request #507 from hey-api/fix/t-filter-not-function
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford authored Apr 29, 2024
2 parents e3b9876 + a3220ef commit aade9b0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-birds-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: issue causing code to not generate (t.filter is not a function)
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/compiler/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const createMethodDeclaration = ({
returnType ? createTypeNode(returnType) : undefined,
ts.factory.createBlock(statements, multiLine),
);
if (comment?.length) {
if (comment) {
addLeadingJSDocComment(node, comment);
}
return node;
Expand Down
7 changes: 3 additions & 4 deletions packages/openapi-ts/src/compiler/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const createTypeAliasDeclaration = (
[],
createTypeNode(type),
);
if (comments?.length) {
if (comments) {
addLeadingJSDocComment(node, comments);
}
return node;
Expand Down Expand Up @@ -80,9 +80,8 @@ export const createTypeInterfaceNode = (
: ts.factory.createToken(ts.SyntaxKind.QuestionToken),
createTypeNode(property.type),
);
const comment = property.comment;
if (comment) {
addLeadingJSDocComment(signature, comment);
if (property.comment) {
addLeadingJSDocComment(signature, property.comment);
}
return signature;
}),
Expand Down
35 changes: 22 additions & 13 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export const createArrayType = <T>({
* @returns ts.ObjectLiteralExpression
*/
export const createObjectType = <T extends object>({
comments = {},
comments,
identifiers = [],
leadingComment,
multiLine = true,
obj,
shorthand = false,
Expand All @@ -86,6 +87,7 @@ export const createObjectType = <T extends object>({
obj: T;
comments?: Record<string | number, Comments>;
identifiers?: string[];
leadingComment?: Comments;
multiLine?: boolean;
shorthand?: boolean;
unescape?: boolean;
Expand Down Expand Up @@ -126,17 +128,24 @@ export const createObjectType = <T extends object>({
shorthand && hasShorthandSupport
? ts.factory.createShorthandPropertyAssignment(value)
: ts.factory.createPropertyAssignment(key, initializer);
const c = comments?.[key];
if (c?.length) {
addLeadingJSDocComment(assignment, c);
const comment = comments?.[key];
if (comment) {
addLeadingJSDocComment(assignment, comment);
}
return assignment;
})
.filter(isType<ts.ShorthandPropertyAssignment | ts.PropertyAssignment>);
return ts.factory.createObjectLiteralExpression(

const expression = ts.factory.createObjectLiteralExpression(
properties as any[],
multiLine,
);

if (leadingComment) {
addLeadingJSDocComment(expression, leadingComment);
}

return expression;
};

/**
Expand All @@ -150,28 +159,28 @@ export const createObjectType = <T extends object>({
export const createEnumDeclaration = <T extends object>({
name,
obj,
leadingComment = [],
comments = {},
leadingComment,
comments,
}: {
name: string;
obj: T;
leadingComment: Comments;
comments: Record<string | number, Comments>;
leadingComment?: Comments;
comments?: Record<string | number, Comments>;
}): ts.EnumDeclaration => {
const declaration = ts.factory.createEnumDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier(name),
Object.entries(obj).map(([key, value]) => {
const initializer = toExpression({ unescape: true, value });
const assignment = ts.factory.createEnumMember(key, initializer);
const c = comments?.[key];
if (c) {
addLeadingJSDocComment(assignment, c);
const comment = comments?.[key];
if (comment) {
addLeadingJSDocComment(assignment, comment);
}
return assignment;
}),
);
if (leadingComment.length) {
if (leadingComment) {
addLeadingJSDocComment(declaration, leadingComment);
}
return declaration;
Expand Down
3 changes: 1 addition & 2 deletions packages/openapi-ts/src/utils/write/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type Node,
TypeScriptFile,
} from '../../compiler';
import { addLeadingJSDocComment } from '../../compiler/utils';
import type { Model, OperationParameter, Service } from '../../openApi';
import { ensureValidTypeScriptJavaScriptIdentifier } from '../../openApi/common/parser/sanitize';
import type { Client } from '../../types/client';
Expand Down Expand Up @@ -101,12 +100,12 @@ const processEnum = (
if (config.types.enums === 'javascript') {
const expression = compiler.types.object({
comments,
leadingComment: comment,
multiLine: true,
obj: properties,
unescape: true,
});
const node = compiler.export.asConst(name, expression);
addLeadingJSDocComment(node, comment);
onNode(node);
}
};
Expand Down

0 comments on commit aade9b0

Please sign in to comment.