-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathdeclarations.dart
More file actions
234 lines (189 loc) · 6.06 KB
/
declarations.dart
File metadata and controls
234 lines (189 loc) · 6.06 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:code_builder/code_builder.dart';
import '../interop_gen/namer.dart';
import 'base.dart';
import 'helpers.dart';
import 'types.dart';
class VariableDeclaration extends NamedDeclaration
implements ExportableDeclaration {
/// The variable modifier, as represented in TypeScript
VariableModifier modifier;
@override
String name;
Type type;
@override
bool exported;
VariableDeclaration(
{required this.name,
required this.type,
required this.modifier,
required this.exported});
@override
ID get id => ID(type: 'var', name: name);
@override
Spec emit([DeclarationOptions? options]) {
if (modifier == VariableModifier.$const) {
return Method((m) => m
..name = name
..type = MethodType.getter
..annotations.add(generateJSAnnotation())
..external = true
..returns = type.emit());
} else {
// getter and setter -> single variable
return Field((f) => f
..external = true
..name = name
..type = type.emit()
..annotations.add(generateJSAnnotation()));
}
}
@override
String? get dartName => null;
}
enum VariableModifier { let, $const, $var }
class FunctionDeclaration extends NamedDeclaration
implements ExportableDeclaration {
@override
final String name;
@override
final String? dartName;
final List<ParameterDeclaration> parameters;
final List<GenericType> typeParameters;
final Type returnType;
@override
bool exported;
@override
ID id;
FunctionDeclaration(
{required this.name,
required this.id,
this.dartName,
this.parameters = const [],
this.typeParameters = const [],
required this.exported,
required this.returnType});
@override
Spec emit([DeclarationOptions? options]) {
options ??= DeclarationOptions();
final requiredParams = <Parameter>[];
final optionalParams = <Parameter>[];
for (final p in parameters) {
if (p.variardic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variardicArgsCount));
requiredParams.add(p.emit(options));
} else {
if (p.optional) {
optionalParams.add(p.emit(options));
} else {
requiredParams.add(p.emit(options));
}
}
}
return Method((m) => m
..external = true
..name = dartName ?? name
..annotations.add(generateJSAnnotation(
dartName == null || dartName == name ? null : name))
..types
.addAll(typeParameters.map((t) => t.emit(options?.toTypeOptions())))
..returns = returnType.emit()
..requiredParameters.addAll(requiredParams)
..optionalParameters.addAll(optionalParams));
}
}
class ParameterDeclaration {
final String name;
final bool optional;
final Type type;
final bool variardic;
ParameterDeclaration(
{required this.name,
this.optional = false,
required this.type,
this.variardic = false});
Parameter emit([DeclarationOptions? options]) {
return Parameter((p) => p
..name = name
..type = type.emit(TypeOptions(nullable: optional)));
}
}
class EnumDeclaration extends NamedDeclaration
implements ExportableDeclaration {
@override
final String name;
@override
final bool exported;
/// The underlying type of the enum (usually a number)
Type baseType;
final List<EnumMember> members;
@override
String? dartName;
EnumDeclaration(
{required this.name,
required this.baseType,
required this.members,
required this.exported,
this.dartName});
@override
Spec emit([DeclarationOptions? options]) {
final baseTypeIsJSType = getJSTypeAlternative(baseType) == baseType;
final externalMember = members.any((m) => m.isExternal);
final shouldUseJSRepType = externalMember || baseTypeIsJSType;
return ExtensionType((e) => e
..annotations.addAll([
if (dartName != null && dartName != name && externalMember)
generateJSAnnotation(name)
])
..constant = !shouldUseJSRepType
..name = dartName ?? name
..primaryConstructorName = '_'
..representationDeclaration = RepresentationDeclaration((r) => r
..declaredRepresentationType = (
// if any member doesn't have a value, we have to use external
// so such type should be the JS rep type
shouldUseJSRepType ? getJSTypeAlternative(baseType) : baseType)
.emit(options?.toTypeOptions())
..name = '_')
..fields
.addAll(members.map((member) => member.emit(shouldUseJSRepType))));
}
@override
ID get id => ID(type: 'enum', name: name);
}
class EnumMember {
final String name;
final Type? type;
final Object? value;
final String parent;
bool get isExternal => value == null;
EnumMember(this.name, this.value,
{this.type, required this.parent, this.dartName});
Field emit([bool? shouldUseJSRepType]) {
final jsRep = shouldUseJSRepType ?? (value == null);
return Field((f) {
// TODO(nikeokoronkwo): This does not render correctly on `code_builder`.
// Until the update is made, we will omit examples concerning this
// Luckily, not many real-world instances of enums use this anyways, https://github.com/dart-lang/tools/issues/2118
if (!isExternal) {
f.modifier = (!jsRep ? FieldModifier.constant : FieldModifier.final$);
}
if (dartName != null && name != dartName && isExternal) {
f.annotations.add(generateJSAnnotation(name));
}
f
..name = dartName ?? name
..type = refer(parent)
..external = value == null
..static = true
..assignment = value == null
? null
: refer(parent).property('_').call([
jsRep ? literal(value).property('toJS') : literal(value)
]).code;
});
}
String? dartName;
}