-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathparameter-block.ts
More file actions
129 lines (108 loc) · 4.11 KB
/
parameter-block.ts
File metadata and controls
129 lines (108 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// import { indent } from '@graphql-codegen/visitor-plugin-common';
import { ListTypeNode, NamedTypeNode, NonNullTypeNode, TypeNode } from 'graphql';
import { indent } from '@graphql-codegen/visitor-plugin-common';
import { Config } from '../config/config-value.js';
import { FieldName, TypeName } from '../config/pattern.js';
import {
AppliesOnParameters,
FieldType,
FlutterFreezedPluginConfig,
NodeType,
} from '../config/plugin-config.js';
import { atJsonKeyDecorator, stringIsNotEmpty } from '../utils.js';
import { Block } from './index.js';
function toPascalCase(str: string): string {
if(!str.includes('-')) return str;
return str
.split('-') // Split the string by hyphens
.map((word, index) => // Capitalize each word, except the first one
index === 0
? word // Keep the first word in lowercase
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() // Capitalize subsequent words
)
.join(''); // Join the words together without spaces
}
export class ParameterBlock {
public static build(
config: FlutterFreezedPluginConfig,
node: NodeType,
field: FieldType,
blockAppliesOn: readonly AppliesOnParameters[],
): string {
const typeName = TypeName.fromString(node.name.value);
const fieldName = FieldName.fromString(field.name.value);
const parameterName = Block.buildBlockName(
config,
blockAppliesOn,
fieldName.value,
typeName,
fieldName,
'camelCase',
);
let block = '';
block += Block.buildComment(field);
block += this.buildDecorators(config, typeName, fieldName, parameterName, blockAppliesOn);
block += this.buildBody(config, field, typeName, fieldName, parameterName, blockAppliesOn);
// return indentMultiline(block, 2);
return block;
}
public static buildDecorators = (
config: FlutterFreezedPluginConfig,
typeName: TypeName,
fieldName: FieldName,
parameterName: string,
blockAppliesOn: readonly AppliesOnParameters[],
): string => {
const deprecatedDecorator = Config.deprecated(config, blockAppliesOn, typeName, fieldName);
const defaultValueDecorator = Config.defaultValues(config, blockAppliesOn, typeName, fieldName);
const jsonKeyDecorator = atJsonKeyDecorator({
name: fieldName.value !== parameterName ? fieldName.value : undefined,
});
return [
deprecatedDecorator,
defaultValueDecorator,
jsonKeyDecorator,
// TODO: add decorator for unionValueName
]
.filter(decorator => stringIsNotEmpty(decorator))
.map(decorator => indent(decorator, 2))
.join('');
};
public static buildBody = (
config: FlutterFreezedPluginConfig,
field: FieldType,
typeName: TypeName,
fieldName: FieldName,
parameterName: string,
blockAppliesOn: readonly AppliesOnParameters[],
): string => {
const required = this.isNonNullType(field.type) ? 'required ' : '';
const final = Config.final(config, blockAppliesOn, typeName, fieldName) ? 'final ' : '';
const dartType = this.parameterType(config, field.type);
return indent(`${required}${final}${dartType} ${parameterName},\n`, 2);
};
public static parameterType = (
config: FlutterFreezedPluginConfig,
type: TypeNode,
parentType?: TypeNode,
): string => {
if (this.isNonNullType(type)) {
return this.parameterType(config, type.type, type);
}
if (this.isListType(type)) {
const T = this.parameterType(config, type.type, type);
return `List<${toPascalCase(T)}>${this.isNonNullType(parentType) ? '' : '?'}`;
}
if (this.isNamedType(type)) {
return `${toPascalCase(Config.customScalars(config, type.name.value))}${
this.isNonNullType(parentType) ? '' : '?'
}`;
}
return '';
};
public static isListType = (type?: TypeNode): type is ListTypeNode => type?.kind === 'ListType';
public static isNonNullType = (type?: TypeNode): type is NonNullTypeNode =>
type?.kind === 'NonNullType';
public static isNamedType = (type?: TypeNode): type is NamedTypeNode =>
type?.kind === 'NamedType';
}