Skip to content
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
16 changes: 5 additions & 11 deletions web_generator/lib/src/ast/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import 'base.dart';
import 'builtin.dart';
import 'declarations.dart';

abstract interface class DeclarationAssociatedType {
String get declarationName;

Declaration get declaration;
}

class ReferredType<T extends Declaration> extends Type {
@override
String name;
Expand Down Expand Up @@ -59,8 +53,10 @@ class UnionType extends Type {
}

// TODO: Handle naming anonymous declarations
class HomogenousUnionType<T extends LiteralType, D extends Declaration>
extends UnionType implements DeclarationAssociatedType {
// TODO: Extract having a declaration associated with a type to its own type
// (e.g DeclarationAssociatedType)
class HomogenousEnumType<T extends LiteralType, D extends Declaration>
extends UnionType {
final List<T> _types;

@override
Expand All @@ -70,17 +66,15 @@ class HomogenousUnionType<T extends LiteralType, D extends Declaration>

final bool isNullable;

@override
String declarationName;

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

@override
EnumDeclaration get declaration => EnumDeclaration(
name: declarationName,
dartName: UniqueNamer.makeNonConflicting(declarationName),
Expand Down
47 changes: 23 additions & 24 deletions web_generator/lib/src/interop_gen/transform/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ class Transformer {

return variable.declarationList.declarations.toDart.map((d) {
namer.markUsed(d.name.text);
return VariableDeclaration(
final variableDeclaration = VariableDeclaration(
name: d.name.text,
type: d.type == null ? BuiltinType.anyType : _transformType(d.type!),
modifier: modifier,
exported: isExported);
return variableDeclaration;
}).toList();
}

Expand Down Expand Up @@ -292,38 +293,37 @@ class Transformer {
final types = unionType.types.toDart.map<Type>(_transformType).toList();

var isHomogenous = true;
final nonNullLiteralTypes = <LiteralType>[];
bool? isBooleanType;
var isNullable = false;
LiteralType? firstNonNullablePrimitiveType;

for (final type in types) {
if (type is LiteralType) {
if (type.kind == LiteralKind.$null) continue;
if (type.kind == LiteralKind.$null) {
isNullable = true;
continue;
}
firstNonNullablePrimitiveType ??= type;
isBooleanType ??= (type.kind == LiteralKind.$true) ||
(type.kind == LiteralKind.$false);
if (type.kind.primitive !=
(types.first as LiteralType).kind.primitive) {
firstNonNullablePrimitiveType.kind.primitive) {
isHomogenous = false;
}
if (isBooleanType) {
isBooleanType = (type.kind == LiteralKind.$true) ||
(type.kind == LiteralKind.$false);
}
nonNullLiteralTypes.add(type);
} else {
isHomogenous = false;
}
}

// check if it is a union of literals
if (isHomogenous) {
// get the literal types other than null
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;

if (isBooleanType) {
if (isBooleanType ?? false) {
return BuiltinType.primitiveType(PrimitiveType.boolean,
isNullable: isNullable);
}
Expand All @@ -332,10 +332,8 @@ class Transformer {
namer.makeUnique('AnonymousUnion', 'type');

// TODO: Handle similar types here...
final type = HomogenousUnionType(
return HomogenousEnumType(
types: nonNullLiteralTypes, isNullable: isNullable, name: name);

return type;
}

return UnionType(types: types);
Expand Down Expand Up @@ -452,7 +450,8 @@ class Transformer {
break;
case final EnumDeclaration _:
break;
case final HomogenousUnionType hu:
// TODO: We can make (DeclarationAssociatedType) and use that rather than individual type names
case final HomogenousEnumType hu:
filteredDeclarations.add(hu.declaration);
break;
case final UnionType u:
Expand Down
2 changes: 1 addition & 1 deletion web_generator/test/integration/interop_gen/enum_input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ export declare let buttonState: "default" | "hovered" | "pressed" | "disabled";
export declare let retriesLeft: 0 | 1 | 2 | 3;
export declare const direction: "N" | "S" | "E" | "W" | null;
export declare const someUnionEnum: 2 | 4 | 6 | 8 | 10;
export declare const myBooleanEnum: true | false;
export declare const myBooleanEnum: true | false;