@graphql-codegen/flutter-freezed 2.11.2-alpha-20221101110911-a036e7a25 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/cjs/config/config-value.js +132 -0
  2. package/cjs/config/pattern.js +731 -0
  3. package/cjs/config/plugin-config.js +140 -0
  4. package/cjs/freezed-declaration-blocks/class-block.js +87 -224
  5. package/cjs/freezed-declaration-blocks/enum-block.js +54 -0
  6. package/cjs/freezed-declaration-blocks/factory-block.js +81 -113
  7. package/cjs/freezed-declaration-blocks/index.js +91 -4
  8. package/cjs/freezed-declaration-blocks/node-repository.js +26 -0
  9. package/cjs/freezed-declaration-blocks/parameter-block.js +55 -143
  10. package/cjs/index.js +10 -14
  11. package/cjs/schema-visitor.js +8 -7
  12. package/cjs/utils.js +64 -219
  13. package/esm/config/config-value.js +128 -0
  14. package/esm/config/pattern.js +723 -0
  15. package/esm/config/plugin-config.js +137 -0
  16. package/esm/freezed-declaration-blocks/class-block.js +86 -223
  17. package/esm/freezed-declaration-blocks/enum-block.js +50 -0
  18. package/esm/freezed-declaration-blocks/factory-block.js +81 -113
  19. package/esm/freezed-declaration-blocks/index.js +89 -3
  20. package/esm/freezed-declaration-blocks/node-repository.js +22 -0
  21. package/esm/freezed-declaration-blocks/parameter-block.js +53 -141
  22. package/esm/index.js +11 -15
  23. package/esm/schema-visitor.js +8 -7
  24. package/esm/utils.js +56 -212
  25. package/package.json +6 -6
  26. package/typings/config/config-value.d.cts +24 -0
  27. package/typings/config/config-value.d.ts +24 -0
  28. package/typings/config/pattern.d.cts +252 -0
  29. package/typings/config/pattern.d.ts +252 -0
  30. package/typings/config/plugin-config.d.cts +683 -0
  31. package/typings/config/plugin-config.d.ts +683 -0
  32. package/typings/freezed-declaration-blocks/class-block.d.cts +11 -35
  33. package/typings/freezed-declaration-blocks/class-block.d.ts +11 -35
  34. package/typings/freezed-declaration-blocks/enum-block.d.cts +10 -0
  35. package/typings/freezed-declaration-blocks/enum-block.d.ts +10 -0
  36. package/typings/freezed-declaration-blocks/factory-block.d.cts +16 -37
  37. package/typings/freezed-declaration-blocks/factory-block.d.ts +16 -37
  38. package/typings/freezed-declaration-blocks/index.d.cts +27 -3
  39. package/typings/freezed-declaration-blocks/index.d.ts +27 -3
  40. package/typings/freezed-declaration-blocks/node-repository.d.cts +10 -0
  41. package/typings/freezed-declaration-blocks/node-repository.d.ts +10 -0
  42. package/typings/freezed-declaration-blocks/parameter-block.d.cts +11 -44
  43. package/typings/freezed-declaration-blocks/parameter-block.d.ts +11 -44
  44. package/typings/index.d.cts +1 -1
  45. package/typings/index.d.ts +1 -1
  46. package/typings/schema-visitor.d.cts +7 -7
  47. package/typings/schema-visitor.d.ts +7 -7
  48. package/typings/utils.d.cts +33 -72
  49. package/typings/utils.d.ts +33 -72
  50. package/cjs/config.js +0 -2
  51. package/esm/config.js +0 -1
  52. package/typings/config.d.cts +0 -479
  53. package/typings/config.d.ts +0 -479
@@ -0,0 +1,137 @@
1
+ export const APPLIES_ON_ENUM = ['enum'];
2
+ export const APPLIES_ON_ENUM_VALUE = ['enum_value'];
3
+ export const APPLIES_ON_CLASS = ['class'];
4
+ export const APPLIES_ON_DEFAULT_FACTORY = ['factory', 'default_factory'];
5
+ export const APPLIES_ON_UNION_FACTORY = ['factory', 'named_factory', 'union_factory'];
6
+ export const APPLIES_ON_MERGED_FACTORY = ['factory', 'named_factory', 'merged_factory'];
7
+ export const APPLIES_ON_FACTORY = ['factory', 'default_factory', 'named_factory', 'merged_factory', 'union_factory'];
8
+ export const APPLIES_ON_DEFAULT_FACTORY_PARAMETERS = ['parameter', 'default_factory_parameter'];
9
+ export const APPLIES_ON_UNION_FACTORY_PARAMETERS = [
10
+ 'parameter',
11
+ 'named_factory_parameter',
12
+ 'union_factory_parameter',
13
+ ];
14
+ export const APPLIES_ON_MERGED_FACTORY_PARAMETERS = [
15
+ 'parameter',
16
+ 'named_factory_parameter',
17
+ 'merged_factory_parameter',
18
+ ];
19
+ export const APPLIES_ON_PARAMETERS = [
20
+ 'parameter',
21
+ 'default_factory_parameter',
22
+ 'named_factory_parameter',
23
+ 'union_factory_parameter',
24
+ 'merged_factory_parameter',
25
+ ];
26
+ /**
27
+ * maps GraphQL scalar types to Dart's scalar types
28
+ */
29
+ export const DART_SCALARS = {
30
+ ID: 'String',
31
+ String: 'String',
32
+ Boolean: 'bool',
33
+ Int: 'int',
34
+ Float: 'double',
35
+ DateTime: 'DateTime',
36
+ };
37
+ export const DART_KEYWORDS = {
38
+ abstract: 'built-in',
39
+ else: 'reserved',
40
+ import: 'built-in',
41
+ show: 'context',
42
+ as: 'built-in',
43
+ enum: 'reserved',
44
+ in: 'reserved',
45
+ static: 'built-in',
46
+ assert: 'reserved',
47
+ export: 'built-in',
48
+ interface: 'built-in',
49
+ super: 'reserved',
50
+ async: 'context',
51
+ extends: 'reserved',
52
+ is: 'reserved',
53
+ switch: 'reserved',
54
+ await: 'async-reserved',
55
+ extension: 'built-in',
56
+ late: 'built-in',
57
+ sync: 'context',
58
+ break: 'reserved',
59
+ external: 'built-in',
60
+ library: 'built-in',
61
+ this: 'reserved',
62
+ case: 'reserved',
63
+ factory: 'built-in',
64
+ mixin: 'built-in',
65
+ throw: 'reserved',
66
+ catch: 'reserved',
67
+ false: 'reserved',
68
+ new: 'reserved',
69
+ true: 'reserved',
70
+ class: 'reserved',
71
+ final: 'reserved',
72
+ null: 'reserved',
73
+ try: 'reserved',
74
+ const: 'reserved',
75
+ finally: 'reserved',
76
+ on: 'context',
77
+ typedef: 'built-in',
78
+ continue: 'reserved',
79
+ for: 'reserved',
80
+ operator: 'built-in',
81
+ var: 'reserved',
82
+ covariant: 'built-in',
83
+ Function: 'built-in',
84
+ part: 'built-in',
85
+ void: 'reserved',
86
+ default: 'reserved',
87
+ get: 'built-in',
88
+ required: 'built-in',
89
+ while: 'reserved',
90
+ deferred: 'built-in',
91
+ hide: 'context',
92
+ rethrow: 'reserved',
93
+ with: 'reserved',
94
+ do: 'reserved',
95
+ if: 'reserved',
96
+ return: 'reserved',
97
+ yield: 'async-reserved',
98
+ dynamic: 'built-in',
99
+ implements: 'built-in',
100
+ set: 'built-in',
101
+ // built-in types
102
+ int: 'reserved',
103
+ double: 'reserved',
104
+ String: 'reserved',
105
+ bool: 'reserved',
106
+ List: 'reserved',
107
+ Set: 'reserved',
108
+ Map: 'reserved',
109
+ Runes: 'reserved',
110
+ Symbol: 'reserved',
111
+ Object: 'reserved',
112
+ Null: 'reserved',
113
+ Never: 'reserved',
114
+ Enum: 'reserved',
115
+ Future: 'reserved',
116
+ Iterable: 'reserved',
117
+ };
118
+ /** initializes a FreezedPluginConfig with the defaults values */
119
+ export const defaultFreezedPluginConfig = {
120
+ camelCasedEnums: true,
121
+ copyWith: undefined,
122
+ customScalars: {},
123
+ defaultValues: undefined,
124
+ deprecated: undefined,
125
+ equal: undefined,
126
+ escapeDartKeywords: true,
127
+ final: undefined,
128
+ // fromJsonToJson: true, // TODO: @next-version
129
+ ignoreTypes: undefined,
130
+ immutable: true,
131
+ makeCollectionsUnmodifiable: undefined,
132
+ mergeTypes: undefined,
133
+ mutableInputs: true,
134
+ privateEmptyConstructor: true,
135
+ unionClass: undefined,
136
+ };
137
+ //#endregion
@@ -1,228 +1,91 @@
1
1
  import { indent } from '@graphql-codegen/visitor-plugin-common';
2
2
  import { Kind } from 'graphql';
3
- import { camelCase, pascalCase } from 'change-case-all';
4
- import { getCustomDecorators, transformCustomDecorators, FreezedConfigValue, } from '../utils.js';
5
- import { FreezedFactoryBlock } from './factory-block.js';
6
- export class FreezedDeclarationBlock {
7
- constructor(_config, _freezedFactoryBlockRepository, _node) {
8
- this._config = _config;
9
- this._freezedFactoryBlockRepository = _freezedFactoryBlockRepository;
10
- this._node = _node;
11
- /** document the class */
12
- this._comment = '';
13
- /** a list of decorators to copy paste to the generator */
14
- this._decorators = [];
15
- /** a list of default constructor and named Constructors used create a Freezed union/sealed class */
16
- this._factoryBlocks = [];
17
- this._config = _config;
18
- this._freezedFactoryBlockRepository = _freezedFactoryBlockRepository;
19
- this._node = _node;
20
- this._freezedConfigValue = new FreezedConfigValue(this._config, this._node.name.value);
21
- }
22
- init() {
23
- if (this._node.kind === Kind.ENUM_TYPE_DEFINITION) {
24
- this.setDecorators().setName().setShape().setBlock();
25
- return this;
26
- }
27
- this.setComment().setDecorators().setName().setFactoryBlocks().setShape().setBlock();
28
- return this;
29
- }
30
- setComment() {
31
- var _a;
32
- const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
33
- if (comment && comment !== null && comment !== '') {
34
- this._comment = `/// ${comment} \n`;
35
- }
36
- return this;
37
- }
38
- getEnumComment(value) {
39
- var _a;
40
- const comment = (_a = value.description) === null || _a === void 0 ? void 0 : _a.value;
41
- if (comment && comment !== null && comment !== '') {
42
- return `/// ${comment} \n`;
43
- }
44
- return '';
45
- }
46
- setDecorators() {
47
- var _a, _b;
48
- const name = this._node.name.value;
49
- // determine if should mark as deprecated
50
- const isDeprecated = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b.deprecated;
51
- this._decorators =
52
- this._node.kind === Kind.ENUM_TYPE_DEFINITION
53
- ? [...transformCustomDecorators(getCustomDecorators(this._config, ['enum'], name), this._node)]
54
- : [
55
- this.getFreezedDecorator(),
56
- ...transformCustomDecorators(getCustomDecorators(this._config, ['class'], name), this._node),
57
- ];
58
- // @deprecated
59
- // if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
60
- if (!this._decorators.includes('@deprecated') && isDeprecated) {
61
- this._decorators = [...this._decorators, '@deprecated\n'];
62
- }
63
- return this;
64
- }
65
- getFreezedDecorator() {
66
- const use_unfreezed = () => {
67
- if (!this._freezedConfigValue.get('immutable') ||
68
- (this._node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && this._freezedConfigValue.get('mutableInputs'))) {
69
- return '@unfreezed\n';
70
- }
71
- return use_Freezed_or_freezed();
72
- };
73
- const use_Freezed_or_freezed = () => {
74
- if (isCustomizedFreezed()) {
75
- const copyWith = this._freezedConfigValue.get('copyWith');
76
- const equal = this._freezedConfigValue.get('equal');
77
- const makeCollectionsUnmodifiable = this._freezedConfigValue.get('makeCollectionsUnmodifiable');
78
- const unionKey = this._freezedConfigValue.get('unionKey');
79
- const unionValueCase = this._freezedConfigValue.get('unionValueCase');
80
- let atFreezed = '@Freezed(\n';
81
- if (copyWith !== undefined) {
82
- atFreezed += indent(`copyWith: ${copyWith},\n`);
83
- }
84
- if (equal !== undefined) {
85
- atFreezed += indent(`equal: ${equal},\n`);
86
- }
87
- if (makeCollectionsUnmodifiable !== undefined) {
88
- atFreezed += indent(`makeCollectionsUnmodifiable: ${makeCollectionsUnmodifiable},\n`);
89
- }
90
- if (unionKey !== undefined) {
91
- atFreezed += indent(`unionKey: ${unionKey},\n`);
92
- }
93
- if (unionValueCase !== undefined) {
94
- atFreezed += indent(`unionValueCase: '${unionValueCase}',\n`);
95
- }
96
- atFreezed += ')\n';
97
- return atFreezed;
98
- }
99
- // else fallback to the normal @freezed decorator
100
- return '@freezed\n';
101
- };
102
- const isCustomizedFreezed = () => {
103
- return (this._freezedConfigValue.get('copyWith') !== undefined ||
104
- this._freezedConfigValue.get('equal') !== undefined ||
105
- this._freezedConfigValue.get('makeCollectionsUnmodifiable') !== undefined ||
106
- this._freezedConfigValue.get('unionKey') !== undefined ||
107
- this._freezedConfigValue.get('unionValueCase') !==
108
- undefined);
109
- };
110
- // this is the start of the pipeline of decisions to determine which Freezed decorator to use
111
- return use_unfreezed();
112
- }
113
- setName() {
114
- this._name = pascalCase(this._node.name.value);
115
- return this;
116
- }
117
- setFactoryBlocks() {
118
- var _a, _b, _c, _d;
119
- if (this._node.kind === Kind.UNION_TYPE_DEFINITION) {
120
- this._factoryBlocks =
121
- (_b = (_a = this._node.types) === null || _a === void 0 ? void 0 : _a.map((_type) => new FreezedFactoryBlock(this._config, this._node).init())) !== null && _b !== void 0 ? _b : [];
122
- }
123
- else if (this._node.kind !== Kind.ENUM_TYPE_DEFINITION) {
124
- /*
125
- for `ObjectTypeDefinitionNode` and `InputObjectTypeDefinitionNode` nodes,
126
- we use the `ShapeRepository`
127
- to register the `FreezedFactoryBlock` so that we can use it later
128
- when we are merging inputs or generating freezed union/sealed classes
129
- for GraphQL union types
130
- */
131
- this._factoryBlocks =
132
- (_d = (_c = this._node.fields) === null || _c === void 0 ? void 0 : _c.map((_field) => this._freezedFactoryBlockRepository.register(this._node.name.value, new FreezedFactoryBlock(this._config, this._node).init()))) !== null && _d !== void 0 ? _d : [];
133
- }
134
- return this;
135
- }
136
- setShape() {
137
- var _a, _b, _c;
138
- let shape = '';
139
- // some helper variables
140
- const name = this._node.name.value;
141
- let namedConstructor;
142
- let factoryBlockKey;
143
- // handle enums differently
144
- if (this._node.kind === Kind.ENUM_TYPE_DEFINITION) {
145
- this._shape = (_a = this._node.values) === null || _a === void 0 ? void 0 : _a.map((value) => {
146
- var _a;
147
- shape = indent(this.getEnumComment(value));
148
- if ((_a = this._config.camelCasedEnums) !== null && _a !== void 0 ? _a : true) {
149
- shape += `@JsonKey(name: ${value.name.value}) ${value.name.value.toLowerCase()}`;
150
- }
151
- else {
152
- shape += value.name.value;
153
- }
154
- return `${shape}\n`;
155
- }).join('');
156
- return this;
157
- }
158
- // append private empty constructor
159
- if (this._freezedConfigValue.get('privateEmptyConstructor')) {
160
- shape += indent(`const ${this._name}._();\n\n`);
161
- }
162
- // decide whether to append an empty Union constructor
163
- if (this._freezedConfigValue.get('defaultUnionConstructor') && this._node.kind === Kind.UNION_TYPE_DEFINITION) {
164
- shape += indent(`const factory ${this._name}() = _${this._name};\n\n`);
165
- }
166
- // append tokens which will be used to retrieve the factory blocks
167
- // from the FreezedFactoryBlockRepository
168
- if (this._node.kind === Kind.UNION_TYPE_DEFINITION) {
169
- (_c = (_b = this._node) === null || _b === void 0 ? void 0 : _b.types) === null || _c === void 0 ? void 0 : _c.forEach(type => {
170
- namedConstructor = type.name.value;
171
- factoryBlockKey = namedConstructor;
172
- shape += `==>factory==>${factoryBlockKey}==>${'union_factory'}==>${name}==>${namedConstructor}\n`;
173
- });
174
- }
175
- else {
176
- factoryBlockKey = name;
177
- // replace token for the ObjectType & InputType to be replaced with the default Freezed constructor
178
- shape += `==>factory==>${factoryBlockKey}==>${'class_factory'}==>${name}\n`;
179
- const mergeInputs = this._freezedConfigValue.get('mergeInputs');
180
- if (this._node.kind === Kind.OBJECT_TYPE_DEFINITION && mergeInputs) {
181
- // replace token for the InputTypes(a.k.a namedConstructors) as a union/sealed class
182
- mergeInputs.forEach(input => {
183
- const separator = input.includes('$') ? '$' : input.includes(name) ? name : '*';
184
- namedConstructor = camelCase(input.split(separator).join('_'));
185
- factoryBlockKey = input.replace('$', name);
186
- shape += `==>factory==>${factoryBlockKey}==>${'merged_input_factory'}==>${name}==>${namedConstructor}\n`;
187
- });
188
- }
189
- }
190
- this._shape = shape;
191
- return this;
192
- }
193
- /**
194
- * returns the string output of the block
195
- */
196
- setBlock() {
3
+ import { Config } from '../config/config-value.js';
4
+ import { APPLIES_ON_CLASS } from '../config/plugin-config.js';
5
+ import { FactoryBlock } from './factory-block.js';
6
+ import { TypeName } from '../config/pattern.js';
7
+ import { Block } from './index.js';
8
+ import { nodeIsObjectType, stringIsNotEmpty } from '../utils.js';
9
+ export class ClassBlock {
10
+ static build(config, node) {
11
+ const typeName = TypeName.fromString(node.name.value);
12
+ const _className = Block.buildBlockName(config, APPLIES_ON_CLASS, typeName.value, typeName, undefined, 'PascalCase');
197
13
  let block = '';
198
- //append comment
199
- block += this._comment;
200
- // append the decorators
201
- block += this._decorators.join('');
202
- // handle enums differently
203
- if (this._node.kind === Kind.ENUM_TYPE_DEFINITION) {
204
- block += `enum ${this._name}{\n${this._shape}}\n\n`;
205
- this._block = block;
206
- return this;
207
- }
208
- // append start of class definition
209
- block += `class ${this._name} with _$${this._name} {\n`;
210
- // append the shape
211
- block += this._shape;
212
- // append fromJson
213
- if (this._freezedConfigValue.get('fromJsonToJson')) {
214
- block += indent(`factory ${this._name}.fromJson(Map<String, Object?> json) => _${this._name}FromJson(json);\n`);
215
- }
216
- //append end of class definition
217
- block += '}\n\n';
218
- this._block = block;
219
- return this;
220
- }
221
- /** returns the block */
222
- toString() {
223
- if (!this._block) {
224
- throw new Error('setShape must be called before calling toString()');
225
- }
226
- return this._block;
14
+ // the comments should be on the factory block instead
15
+ // block += Block.buildComment(node);
16
+ block += this.buildDecorators(config, node);
17
+ block += this.buildHeader(config, typeName, _className);
18
+ block += this.buildBody(config, node);
19
+ block += this.buildFooter(config, typeName, _className);
20
+ return block;
227
21
  }
228
22
  }
23
+ ClassBlock.buildDecorators = (config, node) => {
24
+ const freezedDecorator = ClassBlock.buildFreezedDecorator(config, node);
25
+ // TODO: consider implementing custom decorators
26
+ return [freezedDecorator].join('');
27
+ };
28
+ ClassBlock.buildFreezedDecorator = (config, node) => {
29
+ // this is the start of the pipeline of decisions to determine which Freezed decorator to use
30
+ return ClassBlock.decorateAsFreezed(config, node);
31
+ };
32
+ ClassBlock.decorateAsFreezed = (config, node) => {
33
+ const typeName = TypeName.fromString(node.name.value);
34
+ const copyWith = Config.copyWith(config, typeName);
35
+ const equal = Config.equal(config, typeName);
36
+ const makeCollectionsUnmodifiable = Config.makeCollectionsUnmodifiable(config, typeName);
37
+ const unionKey = Config.unionKey();
38
+ const unionValueCase = Config.unionValueCase();
39
+ const body = [
40
+ copyWith !== undefined ? `copyWith: ${copyWith},\n` : undefined,
41
+ equal !== undefined ? `equal: ${equal},\n` : undefined,
42
+ makeCollectionsUnmodifiable !== undefined
43
+ ? `makeCollectionsUnmodifiable: ${makeCollectionsUnmodifiable},\n`
44
+ : undefined,
45
+ unionKey !== undefined ? `unionKey: ${unionKey},\n` : undefined,
46
+ unionValueCase !== undefined ? `unionValueCase: '${unionValueCase}',\n` : undefined,
47
+ ]
48
+ .filter(value => value !== undefined)
49
+ .map(value => indent(value))
50
+ .join('');
51
+ return stringIsNotEmpty(body) ? `@Freezed(\n${body})\n` : ClassBlock.decorateAsUnfreezed(config, node);
52
+ };
53
+ ClassBlock.decorateAsUnfreezed = (config, node) => {
54
+ const typeName = TypeName.fromString(node.name.value);
55
+ const immutable = Config.immutable(config, typeName);
56
+ const mutableInputs = Config.mutableInputs(config, typeName);
57
+ const mutable = immutable !== true || (node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && mutableInputs);
58
+ return mutable ? '@unfreezed\n' : '@freezed\n';
59
+ };
60
+ ClassBlock.buildHeader = (config, typeName, _className) => {
61
+ const privateEmptyConstructor = Config.privateEmptyConstructor(config, typeName)
62
+ ? indent(`const ${_className}._();\n\n`)
63
+ : '';
64
+ return `class ${_className} with _$${_className} {\n${privateEmptyConstructor}`;
65
+ };
66
+ ClassBlock.buildBody = (config, node) => {
67
+ var _a;
68
+ const className = TypeName.fromString(node.name.value);
69
+ let body = '';
70
+ if (nodeIsObjectType(node)) {
71
+ body += FactoryBlock.serializeDefaultFactory(className);
72
+ }
73
+ else if (node.kind === Kind.UNION_TYPE_DEFINITION) {
74
+ body += ((_a = node.types) !== null && _a !== void 0 ? _a : [])
75
+ .map(value => {
76
+ const factoryName = TypeName.fromString(value.name.value);
77
+ return FactoryBlock.serializeUnionFactory(className, factoryName);
78
+ })
79
+ .join('');
80
+ }
81
+ body += Config.mergeTypes(config, className)
82
+ .map(value => {
83
+ const factoryName = TypeName.fromString(value);
84
+ return FactoryBlock.serializeMergedFactory(className, factoryName);
85
+ })
86
+ .join('');
87
+ return body;
88
+ };
89
+ ClassBlock.buildFooter = (config, typeName, _className) => {
90
+ return indent(`factory ${_className}.fromJson(Map<String, dynamic> json) => _$${_className}FromJson(json);\n}\n\n`);
91
+ };
@@ -0,0 +1,50 @@
1
+ import { TypeName, FieldName } from '../config/pattern.js';
2
+ import { APPLIES_ON_ENUM, APPLIES_ON_ENUM_VALUE } from '../config/plugin-config.js';
3
+ import { indent } from '@graphql-codegen/visitor-plugin-common';
4
+ import { Block } from './index.js';
5
+ import { Config } from '../config/config-value.js';
6
+ import { atJsonKeyDecorator, stringIsNotEmpty } from '../utils.js';
7
+ export class EnumBlock {
8
+ // TODO: @next-version: Implement enhanced enums
9
+ static build(config, node) {
10
+ const typeName = TypeName.fromString(node.name.value);
11
+ let block = '';
12
+ block += Block.buildComment(node);
13
+ block += this.buildDecorators();
14
+ block += this.buildHeader(config, typeName);
15
+ block += this.buildBody(config, node);
16
+ block += this.buildFooter();
17
+ return block;
18
+ }
19
+ }
20
+ EnumBlock.buildDecorators = () => {
21
+ // TODO: @next-version: @JsonEnum(valueField: 'code', fieldRename: 'new-name')
22
+ return '';
23
+ };
24
+ EnumBlock.buildHeader = (config, typeName) => {
25
+ const enumTypeName = Block.buildBlockName(config, APPLIES_ON_ENUM, typeName.value, typeName, undefined, 'PascalCase');
26
+ return `enum ${enumTypeName} {\n`;
27
+ };
28
+ EnumBlock.buildBody = (config, node) => {
29
+ var _a, _b;
30
+ const typeName = TypeName.fromString(node.name.value);
31
+ return (_b = ((_a = node.values) !== null && _a !== void 0 ? _a : [])) === null || _b === void 0 ? void 0 : _b.map((enumValue) => {
32
+ const fieldName = FieldName.fromString(enumValue.name.value);
33
+ const enumValueName = Block.buildBlockName(config, APPLIES_ON_ENUM_VALUE, fieldName.value, typeName, fieldName, Config.camelCasedEnums(config));
34
+ const comment = Block.buildComment(enumValue);
35
+ const jsonKey = atJsonKeyDecorator({
36
+ name: fieldName.value !== enumValueName ? fieldName.value : undefined,
37
+ });
38
+ //TODO: @next-version: const jsonValue = @JsonValue(String|int)
39
+ const decorators = [
40
+ jsonKey,
41
+ // jsonValue
42
+ ].join('');
43
+ return [comment, decorators, `${enumValueName}\n`]
44
+ .map(block => (stringIsNotEmpty(block) ? indent(block) : block))
45
+ .join('');
46
+ }).join('');
47
+ };
48
+ EnumBlock.buildFooter = () => {
49
+ return '}\n\n';
50
+ };