Skip to content

Commit

Permalink
feat: disable formatting with prettier by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Apr 21, 2024
1 parent 2faa567 commit 6e1244f
Show file tree
Hide file tree
Showing 120 changed files with 18,034 additions and 19,536 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-bugs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

change: disable formatting with prettier by default
14 changes: 7 additions & 7 deletions docs/openapi-ts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,29 @@ You might not need a `node` client. Fetch API is [experimental](https://nodejs.o

## Formatting

By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `format` to false.
By default, `openapi-ts` will not automatically format your client. To enable this feature, set `format` to a valid formatter.

::: code-group

```js{2} [prettier]
```js{2} [disabled]
export default {
format: 'prettier',
format: false,
input: 'path/to/openapi.json',
output: 'src/client',
}
```

```js{2} [biome]
```js{2} [prettier]
export default {
format: 'biome',
format: 'prettier',
input: 'path/to/openapi.json',
output: 'src/client',
}
```

```js{2} [disabled]
```js{2} [biome]
export default {
format: false,
format: 'biome',
input: 'path/to/openapi.json',
output: 'src/client',
}
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi-ts/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ This config option is deprecated and will be removed.

### Changed `format`

This config option has changed. You now need to specify a value (`biome` or `prettier`) to format the output (default: `prettier`).
This config option has changed. You now need to specify a value (`biome` or `prettier`) to format the output (default: `false`).

```js{2}
export default {
Expand Down
12 changes: 7 additions & 5 deletions packages/openapi-ts/src/compiler/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ts from 'typescript';

import { createTypeNode } from './typedef';
import { toExpression } from './types';
import { addLeadingComment, Comments, isType } from './utils';
import { addLeadingJSDocComment, Comments, isType } from './utils';

type AccessLevel = 'public' | 'protected' | 'private';

Expand Down Expand Up @@ -87,7 +87,7 @@ export const createConstructorDeclaration = ({
ts.factory.createBlock(statements, multiLine),
);
if (comment?.length) {
addLeadingComment(node, comment);
addLeadingJSDocComment(node, comment);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L90 was not covered by tests
}
return node;
};
Expand Down Expand Up @@ -138,7 +138,7 @@ export const createMethodDeclaration = ({
ts.factory.createBlock(statements, multiLine),
);
if (comment?.length) {
addLeadingComment(node, comment);
addLeadingJSDocComment(node, comment);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L141 was not covered by tests
}
return node;
};
Expand Down Expand Up @@ -214,7 +214,9 @@ export const createReturnFunctionCall = ({
ts.factory.createIdentifier(name),
undefined,
args
.map((arg) => ts.factory.createIdentifier(arg))
.filter(isType<ts.Identifier>),
.map((arg) =>
ts.isExpression(arg) ? arg : ts.factory.createIdentifier(arg),
)
.filter(isType<ts.Identifier | ts.Expression>),

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

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/classes.ts#L217-L220

Added lines #L217 - L220 were not covered by tests
),
);
9 changes: 5 additions & 4 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as classes from './classes';
import * as module from './module';
import * as typedef from './typedef';
import * as types from './types';
import { addLeadingComment, stringToTsNodes, tsNodeToString } from './utils';
import { stringToTsNodes, tsNodeToString } from './utils';

export type { FunctionParameter } from './classes';
export type { Property } from './typedef';
Expand Down Expand Up @@ -44,9 +44,10 @@ export class TypeScriptFile {
this._path = path.resolve(dir, this.getName());

if (header) {
const text = 'This file is auto-generated by @hey-api/openapi-ts';
const comment = addLeadingComment(undefined, [text], true, false);
this._headers = [...this._headers, comment];
this._headers = [
...this._headers,
'// This file is auto-generated by @hey-api/openapi-ts',
];
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/compiler/typedef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from 'typescript';

import { addLeadingComment, type Comments, tsNodeToString } from './utils';
import { addLeadingJSDocComment, type Comments, tsNodeToString } from './utils';

export const createTypeNode = (
base: any | ts.TypeNode,
Expand Down Expand Up @@ -41,7 +41,7 @@ export const createTypeAliasDeclaration = (
createTypeNode(type),
);
if (comments?.length) {
addLeadingComment(node, comments);
addLeadingJSDocComment(node, comments);
}
return node;
};
Expand Down Expand Up @@ -79,7 +79,7 @@ export const createTypeInterfaceNode = (
);
const comment = property.comment;
if (comment) {
addLeadingComment(signature, comment);
addLeadingJSDocComment(signature, comment);

Check warning on line 82 in packages/openapi-ts/src/compiler/typedef.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/typedef.ts#L82

Added line #L82 was not covered by tests
}
return signature;
}),
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from 'typescript';

import { addLeadingComment, type Comments, isType, ots } from './utils';
import { addLeadingJSDocComment, type Comments, isType, ots } from './utils';

/**
* Convert an unknown value to an expression.
Expand Down Expand Up @@ -120,7 +120,7 @@ export const createObjectType = <T extends object>({
: ts.factory.createPropertyAssignment(key, initializer);
const c = comments?.[key];
if (c?.length) {
addLeadingComment(assignment, c);
addLeadingJSDocComment(assignment, c);

Check warning on line 123 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L123

Added line #L123 was not covered by tests
}
return assignment;
})
Expand Down Expand Up @@ -158,13 +158,13 @@ export const createEnumDeclaration = <T extends object>({
const assignment = ts.factory.createEnumMember(key, initializer);
const c = comments?.[key];
if (c) {
addLeadingComment(assignment, c);
addLeadingJSDocComment(assignment, c);

Check warning on line 161 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L161

Added line #L161 was not covered by tests
}
return assignment;
}),
);
if (leadingComment.length) {
addLeadingComment(declaration, leadingComment);
addLeadingJSDocComment(declaration, leadingComment);

Check warning on line 167 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L167

Added line #L167 was not covered by tests
}
return declaration;
};
38 changes: 13 additions & 25 deletions packages/openapi-ts/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,38 +116,26 @@ export const isType = <T>(value: T | undefined): value is T =>

export type Comments = Array<string | null | false | undefined>;

export const addLeadingComment = (
node: ts.Node | undefined,
text: Comments,
hasTrailingNewLine: boolean = true,
useJSDocStyle = true,
): string => {
export const addLeadingJSDocComment = (node: ts.Node, text: Comments) => {
const comments = text.filter(Boolean);

if (!comments.length) {
return '';
return;
}

// if node is falsy, assume string mode
if (!node) {
if (useJSDocStyle) {
const result = ['/**', ...comments.map((row) => ` * ${row}`), ' */'].join(
'\n',
);
return hasTrailingNewLine ? `${result}\n` : result;
}

const result = comments.map((row) => `// ${row}`).join('\n');
return hasTrailingNewLine ? `${result}\n` : result;
}
const jsdocTexts = comments.map((c, l) =>
ts.factory.createJSDocText(`${c}${l !== comments.length ? '\n' : ''}`),
);
const jsdoc = ts.factory.createJSDocComment(
ts.factory.createNodeArray(jsdocTexts),
);
const cleanedJsdoc = tsNodeToString(jsdoc)
.replace('/*', '')
.replace('* */', '');

Check warning on line 133 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L125-L133

Added lines #L125 - L133 were not covered by tests

ts.addSyntheticLeadingComment(
node,
ts.SyntaxKind.MultiLineCommentTrivia,
encodeURIComponent(
['*', ...comments.map((row) => ` * ${row}`), ' '].join('\n'),
),
hasTrailingNewLine,
cleanedJsdoc,
true,

Check warning on line 139 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L138-L139

Added lines #L138 - L139 were not covered by tests
);
return '';
};
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const initConfig = async (
dryRun = false,
enums = false,
exportCore = true,
format = 'prettier',
format = false,
input,
lint = false,
name,
Expand Down
42 changes: 19 additions & 23 deletions packages/openapi-ts/src/templates/core/OpenAPI.hbs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{{~#equals @root.$config.client 'angular'~}}
{{#equals @root.$config.client 'angular'}}
import type { HttpResponse } from '@angular/common/http';
{{~/equals~}}
{{~#equals @root.$config.client 'axios'~}}
{{/equals}}
{{#equals @root.$config.client 'axios'}}
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
{{~/equals~}}
{{~#equals @root.$config.client 'node'~}}
{{/equals}}
{{#equals @root.$config.client 'node'}}
import type { RequestInit, Response } from 'node-fetch';
{{~/equals~}}

{{/equals}}
import type { ApiRequestOptions } from './ApiRequestOptions';

type Headers = Record<string, string>;
Expand All @@ -24,10 +23,7 @@ export class Interceptors<T> {
eject(fn: Middleware<T>) {
const index = this._fns.indexOf(fn);
if (index !== -1) {
this._fns = [
...this._fns.slice(0, index),
...this._fns.slice(index + 1),
];
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)];
}
}

Expand All @@ -47,25 +43,25 @@ export type OpenAPIConfig = {
VERSION: string;
WITH_CREDENTIALS: boolean;
interceptors: {
{{~#equals @root.$config.client 'angular'~}}
{{#equals @root.$config.client 'angular'}}
response: Interceptors<HttpResponse<any>>;
{{~/equals~}}
{{~#equals @root.$config.client 'axios'~}}
{{/equals}}
{{#equals @root.$config.client 'axios'}}
request: Interceptors<AxiosRequestConfig>;
response: Interceptors<AxiosResponse>;
{{~/equals~}}
{{~#equals @root.$config.client 'fetch'~}}
{{/equals}}
{{#equals @root.$config.client 'fetch'}}
request: Interceptors<RequestInit>;
response: Interceptors<Response>;
{{~/equals~}}
{{~#equals @root.$config.client 'node'~}}
{{/equals}}
{{#equals @root.$config.client 'node'}}
request: Interceptors<RequestInit>;
response: Interceptors<Response>;
{{~/equals~}}
{{~#equals @root.$config.client 'xhr'~}}
{{/equals}}
{{#equals @root.$config.client 'xhr'}}
request: Interceptors<XMLHttpRequest>;
response: Interceptors<XMLHttpRequest>;
{{~/equals~}}
{{/equals}}
};
};

Expand All @@ -80,9 +76,9 @@ export const OpenAPI: OpenAPIConfig = {
VERSION: '{{{version}}}',
WITH_CREDENTIALS: false,
interceptors: {
{{~#notEquals @root.$config.client 'angular'~}}
{{#notEquals @root.$config.client 'angular'}}
request: new Interceptors(),
{{~/notEquals~}}
{{/notEquals}}
response: new Interceptors(),
},
};
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface UserConfig {
exportCore?: boolean;
/**
* Process output folder with formatter?
* @default 'prettier'
* @default false
*/
format?: 'biome' | 'prettier' | false;
/**
Expand Down
9 changes: 6 additions & 3 deletions packages/openapi-ts/src/utils/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ export const enumName = (client: Client, name?: string) => {

export const enumUnionType = (enums: Enum[]) =>
enums
.map((enumerator) => enumValue(enumerator.value))
.map((enumerator) => enumValue(enumerator.value, true))

Check warning on line 62 in packages/openapi-ts/src/utils/enum.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/enum.ts#L62

Added line #L62 was not covered by tests
.filter(unique)
.join(' | ');

export const enumValue = (value?: string | number) => {
export const enumValue = (value?: string | number, union: boolean = false) => {
if (typeof value === 'string') {
return `'${value.replace(/'/g, "\\'")}'`;
if (value.includes("'") && union) {
return `"${value}"`;
}
return `'${value}'`;

Check warning on line 71 in packages/openapi-ts/src/utils/enum.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/enum.ts#L68-L71

Added lines #L68 - L71 were not covered by tests
}
return value;
};
9 changes: 2 additions & 7 deletions packages/openapi-ts/src/utils/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ export const unescapeName = (value: string): string => {
return value;
};

export const escapeComment = (value: string, insertAsterisk = true) =>
export const escapeComment = (value: string) =>
value
.replace(/\*\//g, '*')
.replace(/\/\*/g, '*')
.replace(/\r?\n(.*)/g, (_, w) => {
if (insertAsterisk) {
return `${EOL} * ${w.trim()}`;
}
return EOL + w.trim();
});
.replace(/\r?\n(.*)/g, (_l, w) => EOL + w.trim());

Check warning on line 31 in packages/openapi-ts/src/utils/escape.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/escape.ts#L31

Added line #L31 was not covered by tests

export const escapeDescription = (value: string) =>
value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/utils/write/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type Node,
TypeScriptFile,
} from '../../compiler';
import { addLeadingComment } from '../../compiler/utils';
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 @@ -100,7 +100,7 @@ const processEnum = (
unescape: true,
});
const node = compiler.export.asConst(name, expression);
addLeadingComment(node, comment);
addLeadingJSDocComment(node, comment);

Check warning on line 103 in packages/openapi-ts/src/utils/write/models.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/models.ts#L103

Added line #L103 was not covered by tests
onNode(node, 'enum');
}
};
Expand Down
Loading

0 comments on commit 6e1244f

Please sign in to comment.