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: comment position in JavaScript enums #544

Merged
merged 1 commit into from
May 7, 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/smart-buttons-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: comment position in JavaScript enums
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/utils/write/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@
if (config.types.enums === 'javascript') {
const expression = compiler.types.object({
comments,
leadingComment: comment,
multiLine: true,
obj: properties,
unescape: true,
});
const node = compiler.export.const({
comment,

Check warning on line 155 in packages/openapi-ts/src/utils/write/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/types.ts#L155

Added line #L155 was not covered by tests
constAssertion: true,
expression,
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,44 @@ export type SimpleStringWithPattern = string;
*/
export type EnumWithStrings = 'Success' | 'Warning' | 'Error' | "'Single Quote'" | '"Double Quotes"' | 'Non-ascii: øæåôöØÆÅÔÖ字符串';

/**
* This is a simple enum with strings
*/
export const EnumWithStrings = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

/**
* This is a simple enum with numbers
*/
export type EnumWithNumbers = 1 | 2 | 3 | 1.1 | 1.2 | 1.3 | 100 | 200 | 300 | -100 | -200 | -300 | -1.1 | -1.2 | -1.3;

/**
* This is a simple enum with numbers
*/
export const EnumWithNumbers = {
'_1': 1,
'_2': 2,
'_3': 3,
'_1.1': 1.1,
'_1.2': 1.2,
'_1.3': 1.3,
'_100': 100,
'_200': 200,
'_300': 300,
'_-100': -100,
'_-200': -200,
'_-300': -300,
'_-1.1': -1.1,
'_-1.2': -1.2,
'_-1.3': -1.3
} as const;

/**
* Success=1,Warning=2,Error=3
*/
Expand All @@ -93,6 +126,24 @@ export type EnumFromDescription = number;
*/
export type EnumWithExtensions = 200 | 400 | 500;

/**
* This is a simple enum with numbers
*/
export const EnumWithExtensions = {
/**
* Used when the status of something is successful
*/
CUSTOM_SUCCESS: 200,
/**
* Used when the status of something has a warning
*/
CUSTOM_WARNING: 400,
/**
* Used when the status of something has an error
*/
CUSTOM_ERROR: 500
} as const;

/**
* This is a simple array with numbers
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,54 @@ export type SimpleStringWithPattern = string | null;
*/
export type EnumWithStrings = 'Success' | 'Warning' | 'Error' | "'Single Quote'" | '"Double Quotes"' | 'Non-ascii: øæåôöØÆÅÔÖ字符串';

/**
* This is a simple enum with strings
*/
export const EnumWithStrings = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
'NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串': 'Non-ascii: øæåôöØÆÅÔÖ字符串'
} as const;

export type EnumWithReplacedCharacters = "'Single Quote'" | '"Double Quotes"' | 'øæåôöØÆÅÔÖ字符串' | 3.1 | '';

export const EnumWithReplacedCharacters = {
_SINGLE_QUOTE_: "'Single Quote'",
_DOUBLE_QUOTES_: '"Double Quotes"',
'ØÆÅÔÖ_ØÆÅÔÖ字符串': 'øæåôöØÆÅÔÖ字符串',
'_3.1': 3.1,
EMPTY_STRING: ''
} as const;

/**
* This is a simple enum with numbers
*/
export type EnumWithNumbers = 1 | 2 | 3 | 1.1 | 1.2 | 1.3 | 100 | 200 | 300 | -100 | -200 | -300 | -1.1 | -1.2 | -1.3;

/**
* This is a simple enum with numbers
*/
export const EnumWithNumbers = {
'_1': 1,
'_2': 2,
'_3': 3,
'_1.1': 1.1,
'_1.2': 1.2,
'_1.3': 1.3,
'_100': 100,
'_200': 200,
'_300': 300,
'_-100': -100,
'_-200': -200,
'_-300': -300,
'_-1.1': -1.1,
'_-1.2': -1.2,
'_-1.3': -1.3
} as const;

/**
* Success=1,Warning=2,Error=3
*/
Expand All @@ -103,8 +144,32 @@ export type EnumFromDescription = number;
*/
export type EnumWithExtensions = 200 | 400 | 500;

/**
* This is a simple enum with numbers
*/
export const EnumWithExtensions = {
/**
* Used when the status of something is successful
*/
CUSTOM_SUCCESS: 200,
/**
* Used when the status of something has a warning
*/
CUSTOM_WARNING: 400,
/**
* Used when the status of something has an error
*/
CUSTOM_ERROR: 500
} as const;

export type EnumWithXEnumNames = 0 | 1 | 2;

export const EnumWithXEnumNames = {
zero: 0,
one: 1,
two: 2
} as const;

/**
* This is a simple array with numbers
*/
Expand Down Expand Up @@ -447,6 +512,11 @@ export type CompositionWithNestedAnyAndTypeNull = {

export type e_num_1Период = 'Bird' | 'Dog';

export const e_num_1Период = {
BIRD: 'Bird',
DOG: 'Dog'
} as const;

export type ConstValue = "ConstValue";

/**
Expand Down Expand Up @@ -718,8 +788,18 @@ export type ModelWithOneOfEnum = {

export type ModelWithNestedArrayEnumsDataFoo = 'foo' | 'bar';

export const ModelWithNestedArrayEnumsDataFoo = {
FOO: 'foo',
BAR: 'bar'
} as const;

export type ModelWithNestedArrayEnumsDataBar = 'baz' | 'qux';

export const ModelWithNestedArrayEnumsDataBar = {
BAZ: 'baz',
QUX: 'qux'
} as const;

export type ModelWithNestedArrayEnumsData = {
foo?: Array<ModelWithNestedArrayEnumsDataFoo>;
bar?: Array<ModelWithNestedArrayEnumsDataBar>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,43 @@ export type SimpleStringWithPattern = string | null;
/**
* This is a simple enum with strings
*/
export type EnumWithStrings = 'Success' | 'Warning' | 'Error' | "'Single Quote'" | '"Double Quotes"' | 'Non-ascii: øæåôöØÆÅÔÖ字符串';

export type EnumWithReplacedCharacters = "'Single Quote'" | '"Double Quotes"' | 'øæåôöØÆÅÔÖ字符串' | 3.1 | '';
export enum EnumWithStrings {
SUCCESS = 'Success',
WARNING = 'Warning',
ERROR = 'Error',
_SINGLE_QUOTE_ = "'Single Quote'",
_DOUBLE_QUOTES_ = '"Double Quotes"',
NON_ASCII__ØÆÅÔÖ_ØÆÅÔÖ字符串 = 'Non-ascii: øæåôöØÆÅÔÖ字符串'
}

export enum EnumWithReplacedCharacters {
_SINGLE_QUOTE_ = "'Single Quote'",
_DOUBLE_QUOTES_ = '"Double Quotes"',
ØÆÅÔÖ_ØÆÅÔÖ字符串 = 'øæåôöØÆÅÔÖ字符串',
'_3.1' = 3.1,
EMPTY_STRING = ''
}

/**
* This is a simple enum with numbers
*/
export type EnumWithNumbers = 1 | 2 | 3 | 1.1 | 1.2 | 1.3 | 100 | 200 | 300 | -100 | -200 | -300 | -1.1 | -1.2 | -1.3;
export enum EnumWithNumbers {
'_1' = 1,
'_2' = 2,
'_3' = 3,
'_1.1' = 1.1,
'_1.2' = 1.2,
'_1.3' = 1.3,
'_100' = 100,
'_200' = 200,
'_300' = 300,
'_-100' = -100,
'_-200' = -200,
'_-300' = -300,
'_-1.1' = -1.1,
'_-1.2' = -1.2,
'_-1.3' = -1.3
}

/**
* Success=1,Warning=2,Error=3
Expand All @@ -101,9 +130,26 @@ export type EnumFromDescription = number;
/**
* This is a simple enum with numbers
*/
export type EnumWithExtensions = 200 | 400 | 500;
export enum EnumWithExtensions {
/**
* Used when the status of something is successful
*/
CUSTOM_SUCCESS = 200,
/**
* Used when the status of something has a warning
*/
CUSTOM_WARNING = 400,
/**
* Used when the status of something has an error
*/
CUSTOM_ERROR = 500
}

export type EnumWithXEnumNames = 0 | 1 | 2;
export enum EnumWithXEnumNames {
zero = 0,
one = 1,
two = 2
}

/**
* This is a simple array with numbers
Expand Down Expand Up @@ -445,7 +491,10 @@ export type CompositionWithNestedAnyAndTypeNull = {
propA?: Array<(ModelWithDictionary | null)> | Array<(ModelWithArray | null)>;
};

export type e_num_1Период = 'Bird' | 'Dog';
export enum e_num_1Период {
BIRD = 'Bird',
DOG = 'Dog'
}

export type ConstValue = "ConstValue";

Expand Down Expand Up @@ -716,9 +765,15 @@ export type ModelWithOneOfEnum = {
foo: 'Corge';
};

export type ModelWithNestedArrayEnumsDataFoo = 'foo' | 'bar';
export enum ModelWithNestedArrayEnumsDataFoo {
FOO = 'foo',
BAR = 'bar'
}

export type ModelWithNestedArrayEnumsDataBar = 'baz' | 'qux';
export enum ModelWithNestedArrayEnumsDataBar {
BAZ = 'baz',
QUX = 'qux'
}

export type ModelWithNestedArrayEnumsData = {
foo?: Array<ModelWithNestedArrayEnumsDataFoo>;
Expand Down
Loading