Skip to content

Commit

Permalink
Merge pull request #712 from hey-api/fix/transform-multiple-void
Browse files Browse the repository at this point in the history
fix: infinite loop in transforms
  • Loading branch information
mrlubos authored Jun 24, 2024
2 parents 83b6e99 + 0ce82e5 commit 9a1e8c9
Show file tree
Hide file tree
Showing 36 changed files with 1,386 additions and 761 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-trees-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: handle void responses in transformers
10 changes: 10 additions & 0 deletions packages/openapi-ts/src/compiler/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ts from 'typescript';

export const convertExpressionToStatement = ({
expression,
}: {
expression: ts.Expression;
}) => {
const statement = ts.factory.createExpressionStatement(expression);
return statement;
};
19 changes: 19 additions & 0 deletions packages/openapi-ts/src/compiler/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ts from 'typescript';

export const createCallExpression = ({
parameters,
functionName,
}: {
parameters: Array<string>;
functionName: string;
}) => {
const functionIdentifier = ts.factory.createIdentifier(functionName);

const callExpression = ts.factory.createCallExpression(
functionIdentifier,
undefined,
parameters.map((parameter) => ts.factory.createIdentifier(parameter)),
);

return callExpression;
};
22 changes: 21 additions & 1 deletion packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from 'node:path';
import ts from 'typescript';

import * as classes from './classes';
import * as convert from './convert';
import * as functions from './function';
import * as module from './module';
import * as _return from './return';
import * as transform from './transform';
Expand Down Expand Up @@ -78,6 +80,13 @@ export class TypeScriptFile {
rmSync(this._path, options);
}

/**
* Removes last node form the stack. Works as undo.
*/
public removeNode() {
this._items = this._items.slice(0, this._items.length - 1);
}

private _setName(fileName: string) {
if (fileName.includes('index')) {
return fileName;
Expand Down Expand Up @@ -124,16 +133,28 @@ export const compiler = {
create: classes.createClassDeclaration,
method: classes.createMethodDeclaration,
},
convert: {
expressionToStatement: convert.convertExpressionToStatement,
},
export: {
all: module.createExportAllDeclaration,
const: module.createExportConstVariable,
named: module.createNamedExportDeclarations,
},
function: {
call: functions.createCallExpression,
},
import: {
named: module.createNamedImportDeclarations,
},
logic: {
access: transform.createAccessExpression,
if: transform.createIfStatement,
safeAccess: transform.createSafeAccessExpression,
},
return: {
functionCall: _return.createReturnFunctionCall,
statement: _return.createReturnStatement,
},
transform: {
alias: transform.createAlias,
Expand All @@ -143,7 +164,6 @@ export const compiler = {
newDate: transform.createDateTransformerExpression,
responseArrayTransform: transform.createResponseArrayTransform,
transformItem: transform.createFunctionTransformMutation,
transformMutationFunction: transform.createTransformMutationFunction,
},
typedef: {
alias: typedef.createTypeAliasDeclaration,
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi-ts/src/compiler/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ export const createExportConstVariable = ({
constAssertion = false,
expression,
name,
typeName,
}: {
comment?: Comments;
constAssertion?: boolean;
expression: ts.Expression;
name: string;
typeName?: string;
}): ts.VariableStatement => {
const initializer = constAssertion
? ts.factory.createAsExpression(
Expand All @@ -84,7 +86,7 @@ export const createExportConstVariable = ({
const declaration = ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(name),
undefined,
undefined,
typeName ? ts.factory.createTypeReferenceNode(typeName) : undefined,
initializer,
);
const statement = ts.factory.createVariableStatement(
Expand Down
10 changes: 7 additions & 3 deletions packages/openapi-ts/src/compiler/return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export const createReturnFunctionCall = ({
)
.filter(isType<ts.Identifier | ts.Expression>),
);

const statement = ts.factory.createReturnStatement(expression);

const statement = createReturnStatement({ expression });
return statement;
};

export const createReturnStatement = ({
expression,
}: {
expression?: ts.Expression;
}) => ts.factory.createReturnStatement(expression);
Loading

0 comments on commit 9a1e8c9

Please sign in to comment.