@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
@@ -1,125 +1,93 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FreezedFactoryBlock = void 0;
3
+ exports.FactoryBlock = void 0;
4
4
  const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
5
- const graphql_1 = require("graphql");
6
- const change_case_all_1 = require("change-case-all");
5
+ const config_value_js_1 = require("../config/config-value.js");
6
+ const pattern_js_1 = require("../config/pattern.js");
7
+ const plugin_config_js_1 = require("../config/plugin-config.js");
8
+ const index_js_1 = require("./index.js");
7
9
  const parameter_block_js_1 = require("./parameter-block.js");
8
10
  const utils_js_1 = require("../utils.js");
9
- class FreezedFactoryBlock {
10
- constructor(_config, _node) {
11
- this._config = _config;
12
- this._node = _node;
13
- /** document the constructor */
14
- this._comment = '';
15
- /** a list of decorators to copy paste to the generator */
16
- this._decorators = [];
17
- /** a list of interfaces to implements */
18
- // _implements: string[] = [];
19
- /** a list of class to mixin with */
20
- // _mixins: string[] = [];
21
- /** the parameters of this factory constructor */
22
- // TODO: handle other parameter types like positional parameters later.
23
- // TODO: sticking to named parameters because GraphQL is a typed language
24
- this._parameters = [];
25
- this._config = _config;
26
- this._node = _node;
27
- this._freezedConfigValue = new utils_js_1.FreezedConfigValue(_config, _node.name.value);
28
- }
29
- init() {
30
- /*
31
- setDecorators(), setName() and setType() will be called
32
- when the factory is retrieved from the repository
33
- */
34
- this.setComment().setParameters().setShape().setBlock();
35
- return this;
36
- }
37
- setComment() {
38
- var _a;
39
- const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
40
- if (comment && comment !== null && comment !== '') {
41
- this._comment = (0, visitor_plugin_common_1.indent)(`/// ${comment} \n`);
42
- }
43
- return this;
44
- }
45
- setDecorators(appliesOn, nodeName) {
46
- this._decorators = [
47
- ...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, appliesOn.split(','), nodeName), this._node),
48
- ];
49
- return this;
50
- }
51
- setKey(key) {
52
- this._key = (0, change_case_all_1.pascalCase)(key);
53
- return this;
11
+ class FactoryBlock {
12
+ static build(config, node, blockAppliesOn, className, factoryName) {
13
+ let block = '';
14
+ block += index_js_1.Block.buildComment(node);
15
+ block += this.buildDecorators(config, blockAppliesOn, className, factoryName);
16
+ block += this.buildHeader(config, blockAppliesOn, className, factoryName);
17
+ block += this.buildBody(config, node, blockAppliesOn);
18
+ factoryName = blockAppliesOn.includes('default_factory') ? className : factoryName;
19
+ block += this.buildFooter(config, blockAppliesOn, factoryName);
20
+ return block;
54
21
  }
55
- setName(name) {
56
- this._name = (0, change_case_all_1.pascalCase)(name);
57
- return this;
22
+ }
23
+ exports.FactoryBlock = FactoryBlock;
24
+ FactoryBlock.buildDecorators = (config, blockAppliesOn, className, factoryName) => {
25
+ // TODO: @Assert
26
+ const typeName = factoryName ? pattern_js_1.TypeName.fromUnionOfTypeNames(className, factoryName) : className;
27
+ const deprecatedDecorator = config_value_js_1.Config.deprecated(config, blockAppliesOn, typeName);
28
+ const decorators = [deprecatedDecorator].join('');
29
+ return (0, utils_js_1.stringIsNotEmpty)(decorators) ? (0, visitor_plugin_common_1.indent)(decorators) : decorators;
30
+ };
31
+ FactoryBlock.buildHeader = (config, blockAppliesOn, className, factoryName) => {
32
+ const typeName = factoryName ? pattern_js_1.TypeName.fromUnionOfTypeNames(className, factoryName) : className;
33
+ const immutable = config_value_js_1.Config.immutable(config, typeName);
34
+ // const mutableInputs = Config.mutableInputs(config, factoryName);
35
+ // const mutable = immutable !== true || (node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && mutableInputs);
36
+ const constFactory = immutable ? (0, visitor_plugin_common_1.indent)('const factory') : (0, visitor_plugin_common_1.indent)('factory');
37
+ const _className = index_js_1.Block.buildBlockName(config, blockAppliesOn, className.value, className, undefined, 'PascalCase');
38
+ if (factoryName) {
39
+ const _factoryName = index_js_1.Block.buildBlockName(config, blockAppliesOn, factoryName.value, factoryName, undefined, 'camelCase');
40
+ return `${constFactory} ${_className}.${_factoryName}({\n`;
58
41
  }
59
- setNamedConstructor(namedConstructor) {
60
- if (namedConstructor) {
61
- this._namedConstructor = (0, change_case_all_1.camelCase)(namedConstructor);
62
- }
63
- return this;
42
+ return `${constFactory} ${_className}({\n`;
43
+ };
44
+ FactoryBlock.buildBody = (config, node, appliesOn) => {
45
+ var _a, _b;
46
+ let appliesOnParameters = [];
47
+ if (appliesOn.includes('default_factory')) {
48
+ appliesOnParameters = plugin_config_js_1.APPLIES_ON_DEFAULT_FACTORY_PARAMETERS;
64
49
  }
65
- setParameters() {
66
- var _a, _b, _c;
67
- // TODO: get this from config directly
68
- const mergeInputs = this._freezedConfigValue.get('mergeInputs');
69
- const appliesOn = this._namedConstructor
70
- ? ['union_factory_parameter']
71
- : (mergeInputs !== null && mergeInputs !== void 0 ? mergeInputs : mergeInputs !== [])
72
- ? ['merged_input_parameter']
73
- : ['class_factory_parameter'];
74
- if (this._node.kind !== graphql_1.Kind.UNION_TYPE_DEFINITION && this._node.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
75
- this._parameters =
76
- (_c = (_b = (_a = this._node) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.map((field) => new parameter_block_js_1.FreezedParameterBlock(this._config, appliesOn, this._node, field).init())) !== null && _c !== void 0 ? _c : [];
77
- }
78
- return this;
50
+ else if (appliesOn.includes('union_factory')) {
51
+ appliesOnParameters = plugin_config_js_1.APPLIES_ON_UNION_FACTORY_PARAMETERS;
79
52
  }
80
- setShape() {
81
- this._shape = this._parameters.map(p => p.toString()).join('');
82
- return this;
53
+ else if (appliesOn.includes('merged_factory')) {
54
+ appliesOnParameters = plugin_config_js_1.APPLIES_ON_MERGED_FACTORY_PARAMETERS;
83
55
  }
84
- setBlock() {
85
- let block = '';
86
- //append comment
87
- block += this._comment;
88
- // append the decorators
89
- block += this._decorators.map(d => (0, visitor_plugin_common_1.indent)(d)).join('');
90
- block += (0, visitor_plugin_common_1.indent)('');
91
- // decide if to use const or not
92
- if (this._freezedConfigValue.get('immutable')) {
93
- block += 'const ';
94
- }
95
- // append the factory keyword and the name
96
- block += `factory ${this._name}`;
97
- // append .namedConstructor is not null
98
- if (this._namedConstructor && this._namedConstructor !== '') {
99
- block += `.${this._namedConstructor}`;
100
- }
101
- // append the parenthesis for the constructor and braces for the named parameters
102
- block += '({\n';
103
- //append the shape
104
- block += this._shape;
105
- // close the constructor and assign the key
106
- block += (0, visitor_plugin_common_1.indent)(`}) = `);
107
- // but first decide whether prefix the key with an underscore
108
- if (!this._namedConstructor) {
109
- block += '_';
110
- }
111
- // finally, append the key
112
- block += `${this._key};\n`;
113
- // store it in the shape
114
- this._block = block;
115
- return this;
56
+ return ((_b = (_a = node.fields) === null || _a === void 0 ? void 0 : _a.map((field) => {
57
+ return parameter_block_js_1.ParameterBlock.build(config, node, field, appliesOnParameters);
58
+ }).join('')) !== null && _b !== void 0 ? _b : '');
59
+ };
60
+ FactoryBlock.buildFooter = (config, blockAppliesOn, factoryName) => {
61
+ const _ = blockAppliesOn.includes('default_factory') ? '_' : '';
62
+ const _factoryName = index_js_1.Block.buildBlockName(config, blockAppliesOn, factoryName.value, factoryName, undefined, 'PascalCase');
63
+ return (0, visitor_plugin_common_1.indent)(`}) = ${_}${_factoryName};\n\n`);
64
+ };
65
+ FactoryBlock.serializeDefaultFactory = (className) => {
66
+ return `${index_js_1.Block.tokens.defaultFactory}${className.value}==>${plugin_config_js_1.APPLIES_ON_DEFAULT_FACTORY.join(',')}\n`;
67
+ };
68
+ FactoryBlock.serializeUnionFactory = (className, factoryName) => {
69
+ return `${index_js_1.Block.tokens.unionFactory}${className.value}==>${factoryName.value}==>${plugin_config_js_1.APPLIES_ON_UNION_FACTORY.join(',')}\n`;
70
+ };
71
+ FactoryBlock.serializeMergedFactory = (className, factoryName) => {
72
+ return `${index_js_1.Block.tokens.mergedFactory}${className.value}==>${factoryName.value}==>${plugin_config_js_1.APPLIES_ON_MERGED_FACTORY.join(',')}\n`;
73
+ };
74
+ FactoryBlock.deserializeFactory = (config, nodeRepository, blockAppliesOn, className) => {
75
+ const node = nodeRepository.get(className.value);
76
+ if (node) {
77
+ return FactoryBlock.buildFromFactory(config, node, blockAppliesOn, className);
116
78
  }
117
- /** returns the block */
118
- toString() {
119
- if (!this._block) {
120
- throw new Error('FreezedFactoryBlock: setShape must be called before calling toString()');
121
- }
122
- return this._block;
79
+ return '';
80
+ };
81
+ FactoryBlock.deserializeNamedFactory = (config, nodeRepository, blockAppliesOn, className, factoryName) => {
82
+ const node = nodeRepository.get(factoryName.value);
83
+ if (node) {
84
+ return FactoryBlock.buildFromNamedFactory(config, node, blockAppliesOn, className, factoryName);
123
85
  }
124
- }
125
- exports.FreezedFactoryBlock = FreezedFactoryBlock;
86
+ return '';
87
+ };
88
+ FactoryBlock.buildFromFactory = (config, node, blockAppliesOn, className) => {
89
+ return FactoryBlock.build(config, node, blockAppliesOn, className);
90
+ };
91
+ FactoryBlock.buildFromNamedFactory = (config, node, blockAppliesOn, className, factoryName) => {
92
+ return FactoryBlock.build(config, node, blockAppliesOn, className, factoryName);
93
+ };
@@ -1,6 +1,93 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./parameter-block.js"), exports);
5
- tslib_1.__exportStar(require("./factory-block.js"), exports);
6
- tslib_1.__exportStar(require("./class-block.js"), exports);
3
+ exports.Block = void 0;
4
+ const change_case_all_1 = require("change-case-all");
5
+ const graphql_1 = require("graphql");
6
+ const pattern_js_1 = require("../config/pattern.js");
7
+ const config_value_js_1 = require("../config/config-value.js");
8
+ const utils_js_1 = require("../utils.js");
9
+ const class_block_js_1 = require("./class-block.js");
10
+ const enum_block_js_1 = require("./enum-block.js");
11
+ const factory_block_js_1 = require("./factory-block.js");
12
+ class Block {
13
+ }
14
+ exports.Block = Block;
15
+ Block.buildImportStatements = (fileName) => {
16
+ if (fileName.length < 1) {
17
+ throw new Error('fileName is required and must not be empty');
18
+ }
19
+ const segments = fileName.split('/');
20
+ const target = segments[segments.length - 1];
21
+ const expectedFileName = (0, change_case_all_1.snakeCase)(target.replace(/\.dart/g, ''));
22
+ return [
23
+ `import 'package:freezed_annotation/freezed_annotation.dart';\n`,
24
+ `import 'package:flutter/foundation.dart';\n\n`,
25
+ `part '${expectedFileName}.freezed.dart';\n`,
26
+ `part '${expectedFileName}.g.dart';\n\n`,
27
+ ].join('');
28
+ };
29
+ /**
30
+ * Transforms the AST nodes into Freezed classes/models
31
+ * @param config The plugin configuration object
32
+ * @param node the AST node passed by the schema visitor
33
+ * @param nodeRepository A map that stores the name of the Graphql Type as the key and it AST node as the value. Used to build FactoryBlocks from placeholders for mergedInputs and Union Types
34
+ * @returns a string output of a `FreezedDeclarationBlock` which represents a Freezed class/model in Dart
35
+ */
36
+ Block.build = (config, node, nodeRepository) => {
37
+ // ignore these...
38
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
39
+ if (['Query', 'Mutation', 'Subscription', ...config_value_js_1.Config.ignoreTypes(config, typeName)].includes(typeName.value)) {
40
+ return '';
41
+ }
42
+ // registers all the ObjectTypes
43
+ if ((0, utils_js_1.nodeIsObjectType)(node)) {
44
+ nodeRepository.register(node);
45
+ }
46
+ return node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION ? enum_block_js_1.EnumBlock.build(config, node) : class_block_js_1.ClassBlock.build(config, node);
47
+ };
48
+ Block.buildComment = (node) => {
49
+ var _a;
50
+ const comment = (_a = node === null || node === void 0 ? void 0 : node.description) === null || _a === void 0 ? void 0 : _a.value;
51
+ return comment && (comment === null || comment === void 0 ? void 0 : comment.length) > 0
52
+ ? `${comment
53
+ .trim()
54
+ .split(/\n/gm)
55
+ .map(c => `/// ${c.trim().replace(/^#/, '')}\n`)
56
+ .join('')}`
57
+ : '';
58
+ };
59
+ Block.buildBlockName = (config, blockAppliesOn, identifier, typeName, fieldName, blockCasing) => {
60
+ identifier = (0, utils_js_1.dartCasing)(identifier, blockCasing);
61
+ if ((0, utils_js_1.isDartKeyword)(identifier)) {
62
+ return (0, utils_js_1.escapeDartKeyword)(config, blockAppliesOn, identifier, typeName, fieldName);
63
+ }
64
+ return identifier;
65
+ };
66
+ Block.tokens = {
67
+ defaultFactory: '==>default_factory==>',
68
+ unionFactory: '==>union_factory==>',
69
+ mergedFactory: '==>merged_factory==>',
70
+ fromJsonToJson: '==>from_json_to_json==>',
71
+ };
72
+ Block.regexpForToken = (tokenName) => {
73
+ return RegExp(`${Block.tokens[tokenName]}.+\n`, 'gm');
74
+ };
75
+ Block.replaceTokens = (config, nodeRepository, generatedBlocks) => generatedBlocks
76
+ .map(block => {
77
+ block = Block.replaceDefaultFactoryToken(block, config, nodeRepository);
78
+ block = Block.replaceNamedFactoryToken(block, config, nodeRepository, 'unionFactory');
79
+ block = Block.replaceNamedFactoryToken(block, config, nodeRepository, 'mergedFactory');
80
+ // TODO: one more for parameter fromJson and toJson tokens inside @JsonKey
81
+ return block;
82
+ })
83
+ .join('');
84
+ Block.replaceDefaultFactoryToken = (block, config, nodeRepository) => block.replace(Block.regexpForToken('defaultFactory'), token => {
85
+ const pattern = token.replace(Block.tokens.defaultFactory, '').trim();
86
+ const [className, blockAppliesOn] = pattern.split('==>');
87
+ return factory_block_js_1.FactoryBlock.deserializeFactory(config, nodeRepository, blockAppliesOn.split(','), pattern_js_1.TypeName.fromString(className));
88
+ });
89
+ Block.replaceNamedFactoryToken = (block, config, nodeRepository, blockType) => block.replace(Block.regexpForToken(blockType), token => {
90
+ const pattern = token.replace(Block.tokens[blockType], '').trim();
91
+ const [className, factoryName, blockAppliesOn] = pattern.split('==>');
92
+ return factory_block_js_1.FactoryBlock.deserializeNamedFactory(config, nodeRepository, blockAppliesOn.split(','), pattern_js_1.TypeName.fromString(className), pattern_js_1.TypeName.fromString(factoryName));
93
+ });
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ //#region NodeRepository classes
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.NodeRepository = void 0;
5
+ const utils_js_1 = require("../utils.js");
6
+ /**
7
+ * stores an instance of `ObjectTypeDefinitionNode` or `InputObjectTypeDefinitionNode` using the node name as the key
8
+ * and returns that node when replacing placeholders
9
+ * */
10
+ class NodeRepository {
11
+ constructor() {
12
+ this._store = {};
13
+ }
14
+ get(key) {
15
+ return this._store[key];
16
+ }
17
+ register(node) {
18
+ if (!(0, utils_js_1.nodeIsObjectType)(node)) {
19
+ throw new Error('Node is not an ObjectTypeDefinitionNode or InputObjectTypeDefinitionNode');
20
+ }
21
+ this._store[node.name.value] = node;
22
+ return node;
23
+ }
24
+ }
25
+ exports.NodeRepository = NodeRepository;
26
+ //#endregion
@@ -1,150 +1,62 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FreezedParameterBlock = exports.DART_SCALARS = void 0;
4
- const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
5
- const change_case_all_1 = require("change-case-all");
4
+ exports.ParameterBlock = void 0;
6
5
  const utils_js_1 = require("../utils.js");
7
- /**
8
- * maps GraphQL scalar types to Dart's scalar types
9
- */
10
- exports.DART_SCALARS = {
11
- ID: 'String',
12
- String: 'String',
13
- Boolean: 'bool',
14
- Int: 'int',
15
- Float: 'double',
16
- DateTime: 'DateTime',
17
- };
18
- class FreezedParameterBlock {
19
- constructor(_config, _appliesOn, _node, _field) {
20
- this._config = _config;
21
- this._appliesOn = _appliesOn;
22
- this._node = _node;
23
- this._field = _field;
24
- /** document the property */
25
- this._comment = '';
26
- /** a list of decorators to copy paste to the generator */
27
- this._decorators = [];
28
- this.propertyType = (field, type, parentType) => {
29
- if (this.isNonNullType(type)) {
30
- return this.propertyType(field, type.type, type);
31
- }
32
- if (this.isListType(type)) {
33
- const T = this.propertyType(field, type.type, type);
34
- return `List<${T}>${this.isNonNullType(parentType) ? '' : '?'}`;
35
- }
36
- if (this.isNamedType(type)) {
37
- return `${this.scalar(type.name.value)}${this.isNonNullType(parentType) ? '' : '?'}`;
38
- }
39
- return '';
40
- };
41
- this.isListType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'ListType';
42
- this.isNonNullType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NonNullType';
43
- this.isNamedType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NamedType';
44
- this._config = _config;
45
- this._appliesOn = _appliesOn;
46
- this._node = _node;
47
- this._field = _field;
48
- this._freezedConfigValue = new utils_js_1.FreezedConfigValue(_config, _node.name.value);
49
- }
50
- init() {
51
- this.setComment().setDecorators().setRequired().setType().setName().setShape().setBlock();
52
- return this;
53
- }
54
- setComment() {
55
- var _a;
56
- const comment = (_a = this._field.description) === null || _a === void 0 ? void 0 : _a.value;
57
- if (comment && comment !== null && comment !== '') {
58
- this._comment = (0, visitor_plugin_common_1.indent)(`/// ${comment}\n`, 2);
59
- }
60
- return this;
61
- }
62
- setDecorators() {
63
- var _a, _b, _c, _d, _e, _f, _g, _h;
64
- const nodeName = this._node.name.value;
65
- const fieldName = this._field.name.value;
66
- // determine if should mark as deprecated
67
- const isDeprecated = (_d = (_c = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[nodeName]) === null || _b === void 0 ? void 0 : _b.fields) === null || _c === void 0 ? void 0 : _c[fieldName]) === null || _d === void 0 ? void 0 : _d.deprecated;
68
- const defaultValue = (_h = (_g = (_f = (_e = this._config.typeSpecificFreezedConfig) === null || _e === void 0 ? void 0 : _e[nodeName]) === null || _f === void 0 ? void 0 : _f.fields) === null || _g === void 0 ? void 0 : _g[fieldName]) === null || _h === void 0 ? void 0 : _h.defaultValue;
69
- if (this._freezedConfigValue.get('alwaysUseJsonKeyName') || fieldName !== (0, change_case_all_1.camelCase)(fieldName)) {
70
- this._decorators = [...this._decorators, `@JsonKey(name: '${fieldName}')\n`];
71
- }
72
- this._decorators = [
73
- ...this._decorators,
74
- ...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, this._appliesOn, this._node.name.value, fieldName), this._node, this._field),
75
- ];
76
- // @deprecated
77
- // if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
78
- if (!this._decorators.includes('@deprecated') && isDeprecated) {
79
- this._decorators = [...this._decorators, '@deprecated\n'];
80
- }
81
- // @Default
82
- if (defaultValue) {
83
- //overwrite the customDecorator's defaultValue
84
- this._decorators = this._decorators.filter(d => !d.startsWith('@Default'));
85
- this._decorators = [...this._decorators, `@Default(value: ${defaultValue})\n`];
86
- }
87
- return this;
88
- }
89
- setRequired() {
90
- this._required = this.isNonNullType(this._field.type);
91
- return this;
92
- }
93
- setType() {
94
- this._type = this.propertyType(this._field, this._field.type);
95
- return this;
96
- }
97
- setName() {
98
- this._name = (0, change_case_all_1.camelCase)(this._field.name.value);
99
- return this;
100
- }
101
- /** compose the freezed constructor property */
102
- setShape() {
103
- var _a, _b, _c, _d;
104
- let shape = '';
105
- const nodeName = this._node.name.value;
106
- const fieldName = this._field.name.value;
107
- // determine if should mark as final
108
- const isFinal = this._decorators.includes('final') ||
109
- ((_d = (_c = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[nodeName]) === null || _b === void 0 ? void 0 : _b.fields) === null || _c === void 0 ? void 0 : _c[fieldName]) === null || _d === void 0 ? void 0 : _d.final);
110
- //append comment
111
- shape += this._comment;
112
- // append the decorators
113
- shape += this._decorators
114
- .filter(d => d !== 'final')
115
- .map(d => (0, visitor_plugin_common_1.indent)(d, 2))
116
- .join('');
117
- // append required for non-nullable types
118
- shape += (0, visitor_plugin_common_1.indent)(this._required ? 'required ' : '', 2);
119
- // append isFinal
120
- shape += isFinal ? 'final ' : '';
121
- // append the Dart Type, name and trailing comma
122
- shape += `${this._type} ${this._name},\n`;
123
- // store it in the shape
124
- this._shape = shape;
125
- return this;
6
+ const config_value_js_1 = require("../config/config-value.js");
7
+ const pattern_js_1 = require("../config/pattern.js");
8
+ const index_js_1 = require("./index.js");
9
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
10
+ class ParameterBlock {
11
+ static build(config, node, field, blockAppliesOn) {
12
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
13
+ const fieldName = pattern_js_1.FieldName.fromString(field.name.value);
14
+ const parameterName = index_js_1.Block.buildBlockName(config, blockAppliesOn, fieldName.value, typeName, fieldName, 'camelCase');
15
+ let block = '';
16
+ block += index_js_1.Block.buildComment(field);
17
+ block += this.buildDecorators(config, typeName, fieldName, parameterName, blockAppliesOn);
18
+ block += this.buildBody(config, field, typeName, fieldName, parameterName, blockAppliesOn);
19
+ // return indentMultiline(block, 2);
20
+ return block;
126
21
  }
127
- /** composes the full block */
128
- setBlock() {
129
- this._block = this._shape;
130
- return this;
22
+ }
23
+ exports.ParameterBlock = ParameterBlock;
24
+ _a = ParameterBlock;
25
+ ParameterBlock.buildDecorators = (config, typeName, fieldName, parameterName, blockAppliesOn) => {
26
+ const deprecatedDecorator = config_value_js_1.Config.deprecated(config, blockAppliesOn, typeName, fieldName);
27
+ const defaultValueDecorator = config_value_js_1.Config.defaultValues(config, blockAppliesOn, typeName, fieldName);
28
+ const jsonKeyDecorator = (0, utils_js_1.atJsonKeyDecorator)({
29
+ name: fieldName.value !== parameterName ? fieldName.value : undefined,
30
+ });
31
+ return [
32
+ deprecatedDecorator,
33
+ defaultValueDecorator,
34
+ jsonKeyDecorator,
35
+ // TODO: add decorator for unionValueName
36
+ ]
37
+ .filter(decorator => (0, utils_js_1.stringIsNotEmpty)(decorator))
38
+ .map(decorator => (0, visitor_plugin_common_1.indent)(decorator, 2))
39
+ .join('');
40
+ };
41
+ ParameterBlock.buildBody = (config, field, typeName, fieldName, parameterName, blockAppliesOn) => {
42
+ const required = _a.isNonNullType(field.type) ? 'required ' : '';
43
+ const final = config_value_js_1.Config.final(config, blockAppliesOn, typeName, fieldName) ? 'final ' : '';
44
+ const dartType = _a.parameterType(config, field.type);
45
+ return (0, visitor_plugin_common_1.indent)(`${required}${final}${dartType} ${parameterName},\n`, 2);
46
+ };
47
+ ParameterBlock.parameterType = (config, type, parentType) => {
48
+ if (_a.isNonNullType(type)) {
49
+ return _a.parameterType(config, type.type, type);
131
50
  }
132
- scalar(_scalar) {
133
- var _a, _b;
134
- if ((_b = (_a = this._config) === null || _a === void 0 ? void 0 : _a.customScalars) === null || _b === void 0 ? void 0 : _b[_scalar]) {
135
- return this._config.customScalars[_scalar];
136
- }
137
- if (exports.DART_SCALARS[_scalar]) {
138
- return exports.DART_SCALARS[_scalar];
139
- }
140
- return _scalar;
51
+ if (_a.isListType(type)) {
52
+ const T = _a.parameterType(config, type.type, type);
53
+ return `List<${T}>${_a.isNonNullType(parentType) ? '' : '?'}`;
141
54
  }
142
- /** returns the block */
143
- toString() {
144
- if (!this._block) {
145
- throw new Error('FreezedParameterBlock: setShape must be called before calling toString()');
146
- }
147
- return this._block;
55
+ if (_a.isNamedType(type)) {
56
+ return `${config_value_js_1.Config.customScalars(config, type.name.value)}${_a.isNonNullType(parentType) ? '' : '?'}`;
148
57
  }
149
- }
150
- exports.FreezedParameterBlock = FreezedParameterBlock;
58
+ return '';
59
+ };
60
+ ParameterBlock.isListType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'ListType';
61
+ ParameterBlock.isNonNullType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NonNullType';
62
+ ParameterBlock.isNamedType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NamedType';
package/cjs/index.js CHANGED
@@ -3,24 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.plugin = void 0;
4
4
  const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
5
5
  const schema_ast_1 = require("@graphql-codegen/schema-ast");
6
+ const plugin_config_js_1 = require("./config/plugin-config.js");
6
7
  const index_js_1 = require("./freezed-declaration-blocks/index.js");
7
8
  const schema_visitor_js_1 = require("./schema-visitor.js");
8
- const utils_js_1 = require("./utils.js");
9
- const plugin = (schema, _documents, config) => {
9
+ const plugin = (schema, _documents, _config, info) => {
10
+ var _a;
10
11
  // sets the defaults for the config
11
- config = { ...new utils_js_1.DefaultFreezedPluginConfig(config) };
12
+ const config = { ...plugin_config_js_1.defaultFreezedPluginConfig, ..._config };
12
13
  const { schema: _schema, ast } = (0, schema_ast_1.transformSchemaAST)(schema, config);
13
- const { freezedFactoryBlockRepository, ...visitor } = (0, schema_visitor_js_1.schemaVisitor)(_schema, config);
14
+ const { nodeRepository, ...visitor } = (0, schema_visitor_js_1.schemaVisitor)(_schema, config);
14
15
  const visitorResult = (0, plugin_helpers_1.oldVisit)(ast, { leave: visitor });
15
- const generated = visitorResult.definitions.filter((def) => def instanceof index_js_1.FreezedDeclarationBlock);
16
- return ((0, utils_js_1.addFreezedImportStatements)(config.fileName) +
17
- generated
18
- .map(freezedDeclarationBlock => freezedDeclarationBlock.toString().replace(/==>factory==>.+\n/gm, s => {
19
- const pattern = s.replace('==>factory==>', '').trim();
20
- const [key, appliesOn, name, typeName] = pattern.split('==>');
21
- return freezedFactoryBlockRepository.retrieve(key, appliesOn, name, typeName !== null && typeName !== void 0 ? typeName : null);
22
- }))
23
- .join('')
24
- .trim());
16
+ const importStatements = index_js_1.Block.buildImportStatements((_a = info === null || info === void 0 ? void 0 : info.outputFile) !== null && _a !== void 0 ? _a : 'app_models');
17
+ const generatedBlocks = visitorResult.definitions.filter((def) => typeof def === 'string' && def.length > 0);
18
+ // return [importStatements, ...generatedBlocks].join('').trim();
19
+ const output = index_js_1.Block.replaceTokens(config, nodeRepository, generatedBlocks);
20
+ return [importStatements, output].join('').trim();
25
21
  };
26
22
  exports.plugin = plugin;
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.schemaVisitor = void 0;
4
- const utils_js_1 = require("./utils.js");
4
+ const index_js_1 = require("./freezed-declaration-blocks/index.js");
5
+ const node_repository_js_1 = require("./freezed-declaration-blocks/node-repository.js");
5
6
  const schemaVisitor = (_schema, config) => {
6
- const freezedFactoryBlockRepository = new utils_js_1.FreezedFactoryBlockRepository();
7
+ const nodeRepository = new node_repository_js_1.NodeRepository();
7
8
  return {
8
- freezedFactoryBlockRepository,
9
- EnumTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
10
- UnionTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
11
- ObjectTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
12
- InputObjectTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
9
+ nodeRepository,
10
+ EnumTypeDefinition: (node) => index_js_1.Block.build(config, node, nodeRepository),
11
+ UnionTypeDefinition: (node) => index_js_1.Block.build(config, node, nodeRepository),
12
+ ObjectTypeDefinition: (node) => index_js_1.Block.build(config, node, nodeRepository),
13
+ InputObjectTypeDefinition: (node) => index_js_1.Block.build(config, node, nodeRepository),
13
14
  };
14
15
  };
15
16
  exports.schemaVisitor = schemaVisitor;