Skip to content

[interop] Add Support for Enums #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions web_generator/lib/src/ast/declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,31 @@ class EnumDeclaration extends NamedDeclaration
@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
String? dartName;

@override
Spec emit([DeclarationOptions? options]) {
final baseTypeIsJSType = getJSTypeAlternative(baseType) == baseType;
final shouldUseJSRepType =
members.any((m) => m.value == null) || baseTypeIsJSType;
final externalMember = members.any((m) => m.isExternal);
final shouldUseJSRepType = externalMember || baseTypeIsJSType;

return ExtensionType((e) => e
..annotations.addAll([
if (dartName != null && dartName != name) generateJSAnnotation(name)
if (dartName != null && dartName != name && externalMember)
generateJSAnnotation(name)
])
..constant = !shouldUseJSRepType
..name = dartName ?? name
Expand All @@ -181,7 +183,8 @@ class EnumDeclaration extends NamedDeclaration
shouldUseJSRepType ? getJSTypeAlternative(baseType) : baseType)
.emit(options?.toTypeOptions())
..name = '_')
..fields.addAll(members.map((mem) => mem.emit(shouldUseJSRepType))));
..fields
.addAll(members.map((member) => member.emit(shouldUseJSRepType))));
}

@override
Expand All @@ -197,6 +200,8 @@ class EnumMember {

final String parent;

bool get isExternal => value == null;

EnumMember(this.name, this.value,
{this.type, required this.parent, this.dartName});

Expand All @@ -206,10 +211,10 @@ class EnumMember {
// 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 (value != null) {
if (!isExternal) {
f.modifier = (!jsRep ? FieldModifier.constant : FieldModifier.final$);
}
if (dartName != null && name != dartName) {
if (dartName != null && name != dartName && isExternal) {
f.annotations.add(generateJSAnnotation(name));
}
f
Expand Down
54 changes: 31 additions & 23 deletions web_generator/lib/src/ast/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,37 @@ class UnionType extends Type {
@override
ID get id => ID(type: 'type', name: types.map((t) => t.id.name).join('|'));

@override
String? get name => null;

@override
Reference emit([TypeOptions? options]) {
throw UnimplementedError('TODO: Implement UnionType.emit');
}

@override
String? get name => null;
}

// TODO: Handle naming anonymous declarations
class HomogenousUnionType<T extends LiteralType, D extends Declaration>
extends UnionType implements DeclarationAssociatedType {
final List<T> _types;

@override
List<T> get types => _types;

Type get baseType {
return types.first.baseType;
}
final Type baseType;

final bool isNullable;

@override
String declarationName;

HomogenousUnionType(
{required List<T> types, this.isNullable = false, required String name})
: declarationName = name,
_types = types,
baseType = types.first.baseType,
super(types: types);

// TODO: We need a better way of naming declarations
@override
String declarationName;

@override
EnumDeclaration get declaration => EnumDeclaration(
name: declarationName,
Expand All @@ -101,7 +100,7 @@ class HomogenousUnionType<T extends LiteralType, D extends Declaration>
Reference emit([TypeOptions? options]) {
return TypeReference((t) => t
..symbol = declarationName
..isNullable = options?.nullable);
..isNullable = options?.nullable ?? isNullable);
}
}

Expand All @@ -116,15 +115,15 @@ class GenericType extends Type {

GenericType({required this.name, this.constraint, this.parent});

@override
ID get id =>
ID(type: 'generic-type', name: '$name@${parent?.id ?? "(anonymous)"}');

@override
Reference emit([TypeOptions? options]) => TypeReference((t) => t
..symbol = name
..bound = constraint?.emit()
..isNullable = options?.nullable);

@override
ID get id =>
ID(type: 'generic-type', name: '$name@${parent?.id ?? "(anonymous)"}');
}

/// A type representing a bare literal, such as `null`, a string or number
Expand All @@ -143,13 +142,7 @@ class LiteralType extends Type {
};

BuiltinType get baseType {
final primitive = switch (kind) {
LiteralKind.$null => PrimitiveType.undefined,
LiteralKind.string => PrimitiveType.string,
LiteralKind.int => PrimitiveType.num,
LiteralKind.double => PrimitiveType.double,
LiteralKind.$true || LiteralKind.$false => PrimitiveType.boolean
};
final primitive = kind.primitive;

return BuiltinType.primitiveType(primitive);
}
Expand All @@ -165,4 +158,19 @@ class LiteralType extends Type {
ID get id => ID(type: 'type', name: name);
}

enum LiteralKind { $null, string, double, $true, $false, int }
enum LiteralKind {
$null,
string,
double,
$true,
$false,
int;

PrimitiveType get primitive => switch (this) {
LiteralKind.$null => PrimitiveType.undefined,
LiteralKind.string => PrimitiveType.string,
LiteralKind.int => PrimitiveType.num,
LiteralKind.double => PrimitiveType.double,
LiteralKind.$true || LiteralKind.$false => PrimitiveType.boolean
};
}
6 changes: 4 additions & 2 deletions web_generator/lib/src/interop_gen/transform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class TransformResult {
..ignoreForFile.addAll(
['constant_identifier_names', 'non_constant_identifier_names'])
..body.addAll(specs));
print('${lib.accept(emitter)}');
return MapEntry(file, formatter.format('${lib.accept(emitter)}'));
return MapEntry(
file,
formatter.format('${lib.accept(emitter)}'
.replaceAll('static external', 'external static')));
});
}
}
Expand Down
55 changes: 37 additions & 18 deletions web_generator/lib/src/interop_gen/transform/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ class Transformer {
final members = <EnumMember>[];
PrimitiveType? enumRepType;

for (final mem in enumMembers) {
final memName = mem.name.text;
for (final member in enumMembers) {
final memName = member.name.text;
final dartMemName = UniqueNamer.makeNonConflicting(memName);
final memInitializer = mem.initializer;
final memInitializer = member.initializer;

// check the type of the initializer
if (memInitializer != null) {
Expand All @@ -86,12 +86,15 @@ class Transformer {
// parse numeric literal
final value =
_parseNumericLiteral(memInitializer as TSNumericLiteral);
const primitiveType = PrimitiveType.num;
final primitiveType =
value is int ? PrimitiveType.int : PrimitiveType.double;
members.add(EnumMember(memName, value,
type: BuiltinType.primitiveType(primitiveType),
parent: name,
dartName: dartMemName));
if (enumRepType == null) {
if (enumRepType == null &&
!(primitiveType == PrimitiveType.int &&
enumRepType == PrimitiveType.double)) {
enumRepType = primitiveType;
} else if (enumRepType != primitiveType) {
enumRepType = PrimitiveType.any;
Expand Down Expand Up @@ -288,23 +291,39 @@ class Transformer {
// TODO: Unions
final types = unionType.types.toDart.map<Type>(_transformType).toList();

var isHomogenous = true;

for (final type in types) {
if (type is LiteralType) {
if (type.kind == LiteralKind.$null) continue;
if (type.kind.primitive !=
(types.first as LiteralType).kind.primitive) {
isHomogenous = false;
}
} else {
isHomogenous = false;
}
}

// check if it is a union of literals
if (types.every((t) => t is LiteralType) &&
types.every((t) =>
(t as LiteralType).kind == (types.first as LiteralType).kind ||
t.kind == LiteralKind.$null)) {
if (isHomogenous) {
// get the literal types other than null
final literalTypes = types.whereType<LiteralType>();
final nonNullLiteralTypes =
literalTypes.where((t) => t.kind != LiteralKind.$null).toList();
final literalTypes = <LiteralType>[];
final nonNullLiteralTypes = <LiteralType>[];
var isBooleanType = false;

for (final type in types) {
literalTypes.add(type as LiteralType);
if (type.kind != LiteralKind.$null) {
nonNullLiteralTypes.add(type);
isBooleanType = (type.kind == LiteralKind.$true) ||
(type.kind == LiteralKind.$false);
}
}

final isNullable = nonNullLiteralTypes.length == literalTypes.length;
final isNullable = nonNullLiteralTypes.length != literalTypes.length;

if (nonNullLiteralTypes.map((t) => t.kind)
case [
LiteralKind.$true || LiteralKind.$false,
LiteralKind.$true || LiteralKind.$false
]) {
if (isBooleanType) {
return BuiltinType.primitiveType(PrimitiveType.boolean,
isNullable: isNullable);
}
Expand Down
Loading