Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: infinite loop in transforms #712

Merged
merged 5 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};

Check warning on line 10 in packages/openapi-ts/src/compiler/convert.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/convert.ts#L4-L10

Added lines #L4 - L10 were not covered by tests
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;
};

Check warning on line 19 in packages/openapi-ts/src/compiler/function.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/function.ts#L4-L19

Added lines #L4 - L19 were not covered by tests
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 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 @@
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);
}

Check warning on line 88 in packages/openapi-ts/src/compiler/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/index.ts#L87-L88

Added lines #L87 - L88 were not covered by tests

private _setName(fileName: string) {
if (fileName.includes('index')) {
return fileName;
Expand Down Expand Up @@ -124,16 +133,28 @@
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 @@
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 @@
)
.filter(isType<ts.Identifier | ts.Expression>),
);

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

const statement = createReturnStatement({ expression });

Check warning on line 31 in packages/openapi-ts/src/compiler/return.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/return.ts#L31

Added line #L31 was not covered by tests
return statement;
};

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

Check warning on line 39 in packages/openapi-ts/src/compiler/return.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/return.ts#L36-L39

Added lines #L36 - L39 were not covered by tests
Loading