Skip to content

Commit

Permalink
feat: rewrite response transformers to new parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Oct 24, 2024
1 parent 2834ccf commit 7a41471
Show file tree
Hide file tree
Showing 56 changed files with 17,229 additions and 13,725 deletions.
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/compiler/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ts from 'typescript';
import { createCallExpression } from './module';
import {
type AccessLevel,
createBlock,
createTypeNode,
type FunctionParameter,
type FunctionTypeParameter,
Expand Down Expand Up @@ -39,7 +40,7 @@ export const createConstructorDeclaration = ({
const node = ts.factory.createConstructorDeclaration(
toAccessLevelModifiers(accessLevel),
toParameterDeclarations(parameters),
ts.factory.createBlock(statements, multiLine),
createBlock({ multiLine, statements }),

Check warning on line 43 in packages/openapi-ts/src/compiler/classes.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/classes.ts#L43

Added line #L43 was not covered by tests
);

addLeadingComments({
Expand Down Expand Up @@ -100,7 +101,7 @@ export const createMethodDeclaration = ({
types ? toTypeParameters(types) : undefined,
toParameterDeclarations(parameters),
returnType ? createTypeNode(returnType) : undefined,
ts.factory.createBlock(statements, multiLine),
createBlock({ multiLine, statements }),
);

addLeadingComments({
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export const compiler = {
anonymousFunction: types.createAnonymousFunction,
arrayLiteralExpression: types.createArrayLiteralExpression,
arrowFunction: types.createArrowFunction,
assignment: types.createAssignment,
awaitExpression: types.createAwaitExpression,
binaryExpression: transform.createBinaryExpression,
block: types.createBlock,
callExpression: module.createCallExpression,
classDeclaration: classes.createClassDeclaration,
conditionalExpression: types.createConditionalExpression,
Expand All @@ -27,6 +29,8 @@ export const compiler = {
exportAllDeclaration: module.createExportAllDeclaration,
exportNamedDeclaration: module.createNamedExportDeclarations,
expressionToStatement: convert.expressionToStatement,
forOfStatement: types.createForOfStatement,
functionTypeNode: types.createFunctionTypeNode,
identifier: utils.createIdentifier,
ifStatement: transform.createIfStatement,
indexedAccessTypeNode: types.createIndexedAccessTypeNode,
Expand All @@ -36,13 +40,16 @@ export const compiler = {
methodDeclaration: classes.createMethodDeclaration,
namedImportDeclarations: module.createNamedImportDeclarations,
namespaceDeclaration: types.createNamespaceDeclaration,
newExpression: types.createNewExpression,
nodeToString: utils.tsNodeToString,
null: types.createNull,
objectExpression: types.createObjectType,
ots: utils.ots,
parameterDeclaration: types.createParameterDeclaration,
propertyAccessExpression: types.createPropertyAccessExpression,
propertyAccessExpressions: transform.createPropertyAccessExpressions,
returnFunctionCall: _return.createReturnFunctionCall,
returnStatement: _return.createReturnStatement,
returnVariable: _return.createReturnVariable,
safeAccessExpression: transform.createSafeAccessExpression,
stringLiteral: types.createStringLiteral,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/compiler/return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createCallExpression } from './module';
import { createTypeReferenceNode } from './types';
import { createIdentifier, isType } from './utils';

const createReturnStatement = ({
export const createReturnStatement = ({
expression,
}: {
expression?: ts.Expression;
Expand Down
55 changes: 28 additions & 27 deletions packages/openapi-ts/src/compiler/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { expressionToStatement } from './convert';
import { createCallExpression } from './module';
import {
createArrowFunction,
createBlock,
createNewExpression,
createPropertyAccessChain,
createPropertyAccessExpression,
} from './types';
Expand Down Expand Up @@ -99,19 +101,20 @@ export const createDateTransformMutation = ({
const safeAccessExpression = createSafeAccessExpression(path);
const accessExpression = createAccessExpression(path);

const thenStatement = ts.factory.createBlock([
expressionToStatement({
expression: ts.factory.createBinaryExpression(
accessExpression,
ts.SyntaxKind.EqualsToken,
ts.factory.createNewExpression(
createIdentifier({ text: 'Date' }),
undefined,
[accessExpression],
const thenStatement = createBlock({
statements: [
expressionToStatement({
expression: ts.factory.createBinaryExpression(
accessExpression,
ts.SyntaxKind.EqualsToken,
createNewExpression({
argumentsArray: [accessExpression],
expression: createIdentifier({ text: 'Date' }),
}),
),
),
}),
]);
}),
],
});

const statement = createIfStatement({
expression: safeAccessExpression,
Expand All @@ -131,17 +134,16 @@ export const createFunctionTransformMutation = ({
const safeAccessExpression = createSafeAccessExpression(path);
const accessExpression = createAccessExpression(path);

const thenStatement = ts.factory.createBlock(
[
const thenStatement = createBlock({
statements: [
expressionToStatement({
expression: createCallExpression({
functionName: transformerName,
parameters: [accessExpression],
}),
}),
],
true,
);
});

const statement = [
createIfStatement({
Expand Down Expand Up @@ -171,8 +173,8 @@ export const createArrayTransformMutation = ({
}),
parameters: [safeAccessExpression],
}),
thenStatement: ts.factory.createBlock(
[
thenStatement: createBlock({
statements: [
expressionToStatement({
expression: ts.factory.createCallChain(
createPropertyAccessExpression({
Expand All @@ -185,8 +187,7 @@ export const createArrayTransformMutation = ({
),
}),
],
true,
),
}),
});

return statement;
Expand All @@ -198,9 +199,10 @@ export const createDateTransformerExpression = ({
parameterName: string;
}) => {
const expression = createIdentifier({ text: 'Date' });
const newExpression = ts.factory.createNewExpression(expression, undefined, [
createIdentifier({ text: parameterName }),
]);
const newExpression = createNewExpression({
argumentsArray: [createIdentifier({ text: parameterName })],
expression,
});
return newExpression;
};

Expand All @@ -222,8 +224,8 @@ export const createArrayMapTransform = ({
}),
parameters: [safeAccessExpression],
}),
thenStatement: ts.factory.createBlock(
[
thenStatement: createBlock({
statements: [
expressionToStatement({
expression: ts.factory.createBinaryExpression(
accessExpression,
Expand All @@ -249,8 +251,7 @@ export const createArrayMapTransform = ({
),
}),
],
true,
),
}),
});

return statement;
Expand Down
15 changes: 5 additions & 10 deletions packages/openapi-ts/src/compiler/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ts from 'typescript';
import { validTypescriptIdentifierRegExp } from '../utils/regexp';
import {
createKeywordTypeNode,
createParameterDeclaration,
createStringLiteral,
createTypeNode,
createTypeReferenceNode,
Expand Down Expand Up @@ -109,16 +110,10 @@ export const createTypeInterfaceNode = ({
const indexSignature = ts.factory.createIndexSignature(
modifiers,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
createIdentifier({ text: indexProperty.name }),
undefined,
createKeywordTypeNode({
keyword: 'string',
}),
undefined,
),
createParameterDeclaration({
name: createIdentifier({ text: indexProperty.name }),
type: createKeywordTypeNode({ keyword: 'string' }),
}),
],
createTypeNode(indexProperty.type),
);
Expand Down
Loading

0 comments on commit 7a41471

Please sign in to comment.