Skip to content

Commit 1d5771b

Browse files
[interop] Add support for importing and exporting declarations, as well as multi-file output (#418)
* added support for exporting declarations * wip: transform manager * added support for multi-projects - added import support - added gen of code inside file in the case of non multi-import - added support for multiple file generation - using `TransformManager` for managing transformation of code * formatting and analysis fix * added TODO and comment * refactoring transformation and docs * minor changes * fixed symbol code * fixed tests * updated test suite and completed `typeof` integration * completed integration test setup * minor comment change * Implemented new, faster transformation process * completed config update * removed comment * fallback to each child due to @types/web bug * formatting and fixes * resolved some comments and nits
1 parent da1dd5d commit 1d5771b

28 files changed

+2046
-256
lines changed

web_generator/lib/src/ast/base.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class DeclarationOptions extends Options {
2626

2727
class TypeOptions extends Options {
2828
bool nullable;
29+
String? url;
2930

30-
TypeOptions({this.nullable = false});
31+
TypeOptions({this.nullable = false, this.url});
3132
}
3233

3334
class ASTOptions {
@@ -42,7 +43,7 @@ class ASTOptions {
4243
}
4344

4445
sealed class Node {
45-
abstract final String? name;
46+
String? get name;
4647
abstract final ID id;
4748
String? get dartName;
4849

@@ -53,20 +54,30 @@ sealed class Node {
5354

5455
abstract class Declaration extends Node {
5556
@override
56-
abstract final String name;
57+
String get name;
5758

5859
@override
5960
Spec emit([covariant DeclarationOptions? options]);
6061
}
6162

6263
abstract class NamedDeclaration extends Declaration {
63-
ReferredType asReferredType([List<Type>? typeArgs]) =>
64-
ReferredType(name: name, declaration: this, typeParams: typeArgs ?? []);
64+
@override
65+
abstract String name;
66+
67+
ReferredType asReferredType([List<Type>? typeArgs, String? url]) =>
68+
ReferredType(
69+
name: name, declaration: this, typeParams: typeArgs ?? [], url: url);
6570
}
6671

6772
abstract interface class ExportableDeclaration extends Declaration {
6873
/// Whether this declaration is exported.
6974
bool get exported;
75+
76+
@override
77+
abstract String? dartName;
78+
79+
@override
80+
abstract String name;
7081
}
7182

7283
abstract class Type extends Node {

web_generator/lib/src/ast/declarations.dart

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import 'types.dart';
1717
sealed class TypeDeclaration extends NamedDeclaration
1818
implements ExportableDeclaration {
1919
@override
20-
final String name;
20+
String name;
2121

2222
@override
23-
final String? dartName;
23+
String? dartName;
2424

2525
@override
2626
final bool exported;
@@ -139,6 +139,9 @@ class VariableDeclaration extends FieldDeclaration
139139
@override
140140
ID get id => ID(type: 'var', name: name);
141141

142+
@override
143+
String? dartName;
144+
142145
@override
143146
Spec emit([DeclarationOptions? options]) {
144147
if (modifier == VariableModifier.$const) {
@@ -159,12 +162,10 @@ class VariableDeclaration extends FieldDeclaration
159162
}
160163

161164
@override
162-
String? get dartName => null;
163-
164-
@override
165-
ReferredType<VariableDeclaration> asReferredType([List<Type>? typeArgs]) {
165+
ReferredType<VariableDeclaration> asReferredType(
166+
[List<Type>? typeArgs, String? url]) {
166167
return ReferredType<VariableDeclaration>.fromType(type, this,
167-
typeParams: typeArgs ?? []);
168+
typeParams: typeArgs ?? [], url: url);
168169
}
169170
}
170171

@@ -173,10 +174,10 @@ enum VariableModifier { let, $const, $var }
173174
class FunctionDeclaration extends CallableDeclaration
174175
implements ExportableDeclaration {
175176
@override
176-
final String name;
177+
String name;
177178

178179
@override
179-
final String? dartName;
180+
String? dartName;
180181

181182
@override
182183
final List<ParameterDeclaration> parameters;
@@ -234,18 +235,19 @@ class FunctionDeclaration extends CallableDeclaration
234235
}
235236

236237
@override
237-
ReferredType<FunctionDeclaration> asReferredType([List<Type>? typeArgs]) {
238+
ReferredType<FunctionDeclaration> asReferredType(
239+
[List<Type>? typeArgs, String? url]) {
238240
// TODO: We could do better here and make the function type typed
239241
return ReferredType<FunctionDeclaration>.fromType(
240242
BuiltinType.referred('Function', typeParams: typeArgs ?? [])!, this,
241-
typeParams: typeArgs ?? []);
243+
typeParams: typeArgs ?? [], url: url);
242244
}
243245
}
244246

245247
class EnumDeclaration extends NamedDeclaration
246248
implements ExportableDeclaration {
247249
@override
248-
final String name;
250+
String name;
249251

250252
@override
251253
final bool exported;
@@ -339,14 +341,14 @@ class EnumMember {
339341
class TypeAliasDeclaration extends NamedDeclaration
340342
implements ExportableDeclaration {
341343
@override
342-
final String name;
344+
String name;
343345

344346
final List<GenericType> typeParameters;
345347

346348
final Type type;
347349

348350
@override
349-
final String? dartName;
351+
String? dartName;
350352

351353
@override
352354
bool exported;
@@ -447,13 +449,13 @@ class InterfaceDeclaration extends TypeDeclaration {
447449
class PropertyDeclaration extends FieldDeclaration
448450
implements MemberDeclaration {
449451
@override
450-
final String name;
452+
String name;
451453

452454
@override
453455
final ID id;
454456

455457
@override
456-
final String? dartName;
458+
String? dartName;
457459

458460
@override
459461
late final TypeDeclaration parent;
@@ -696,6 +698,9 @@ class OperatorDeclaration extends CallableDeclaration
696698
@override
697699
String get name => kind.expression;
698700

701+
@override
702+
set name(String? name) {}
703+
699704
OperatorKind kind;
700705

701706
@override

web_generator/lib/src/ast/types.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,24 @@ class ReferredType<T extends Declaration> extends Type {
2020

2121
List<Type> typeParams;
2222

23+
String? url;
24+
2325
ReferredType(
2426
{required this.name,
2527
required this.declaration,
26-
this.typeParams = const []});
28+
this.typeParams = const [],
29+
this.url});
2730

2831
factory ReferredType.fromType(Type type, T declaration,
29-
{List<Type> typeParams}) = ReferredDeclarationType;
32+
{List<Type> typeParams, String? url}) = ReferredDeclarationType;
3033

3134
@override
3235
Reference emit([TypeOptions? options]) {
33-
// TODO: Support referred types imported from URL
3436
return TypeReference((t) => t
35-
..symbol = declaration.name
37+
..symbol = declaration.dartName ?? declaration.name
3638
..types.addAll(typeParams.map((t) => t.emit(options)))
37-
..isNullable = options?.nullable);
39+
..isNullable = options?.nullable
40+
..url = options?.url ?? url);
3841
}
3942
}
4043

@@ -44,11 +47,15 @@ class ReferredDeclarationType<T extends Declaration> extends ReferredType<T> {
4447
@override
4548
String get name => type.name ?? declaration.name;
4649

47-
ReferredDeclarationType(this.type, T declaration, {super.typeParams})
50+
ReferredDeclarationType(this.type, T declaration,
51+
{super.typeParams, super.url})
4852
: super(name: declaration.name, declaration: declaration);
4953

5054
@override
5155
Reference emit([covariant TypeOptions? options]) {
56+
options ??= TypeOptions();
57+
options.url = super.url;
58+
5259
return type.emit(options);
5360
}
5461
}

web_generator/lib/src/dart_main.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ Future<void> generateJSInteropBindings(Config config) async {
7070
final jsDeclarations = parseDeclarationFiles(config.input);
7171

7272
// transform declarations
73-
final dartDeclarations = transform(jsDeclarations, config: config);
73+
final manager =
74+
TransformerManager.fromParsedResults(jsDeclarations, config: config);
75+
76+
final dartDeclarations = manager.transform();
7477

7578
// generate
7679
final generatedCodeMap = dartDeclarations.generate(config);

0 commit comments

Comments
 (0)