Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
10 changes: 5 additions & 5 deletions web_generator/lib/src/ast/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../interop_gen/namer.dart';
import 'types.dart';

class GlobalOptions {
static int variardicArgsCount = 4;
static int variadicArgsCount = 4;
static bool shouldEmitJsTypes = false;
static bool redeclareOverrides = true;
}
Expand All @@ -33,11 +33,11 @@ class TypeOptions extends Options {
class ASTOptions {
bool parameter;
bool emitJSTypes;
int variardicArgsCount;
int variadicArgsCount;

ASTOptions(
{this.parameter = false,
this.variardicArgsCount = 4,
this.variadicArgsCount = 4,
this.emitJSTypes = false});
}

Expand Down Expand Up @@ -98,13 +98,13 @@ class ParameterDeclaration {

final Type type;

final bool variardic;
final bool variadic;

ParameterDeclaration(
{required this.name,
this.optional = false,
required this.type,
this.variardic = false});
this.variadic = false});

Parameter emit([DeclarationOptions? options]) {
return Parameter((p) => p
Expand Down
41 changes: 17 additions & 24 deletions web_generator/lib/src/ast/declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class FunctionDeclaration extends CallableDeclaration
final requiredParams = <Parameter>[];
final optionalParams = <Parameter>[];
for (final p in parameters) {
if (p.variardic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variardicArgsCount));
if (p.variadic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variadicArgsCount));
requiredParams.add(p.emit(options));
} else {
if (p.optional) {
Expand Down Expand Up @@ -301,10 +301,8 @@ class ClassDeclaration extends TypeDeclaration {
@override
final List<PropertyDeclaration> properties;

final List<OperatorDeclaration> indexAccessors;

@override
List<OperatorDeclaration> get operators => indexAccessors;
final List<OperatorDeclaration> operators;

ClassDeclaration(
{required this.name,
Expand All @@ -317,7 +315,7 @@ class ClassDeclaration extends TypeDeclaration {
this.constructors = const [],
required this.methods,
required this.properties,
this.indexAccessors = const []});
this.operators = const []});

@override
ExtensionType emit([covariant DeclarationOptions? options]) {
Expand All @@ -342,10 +340,8 @@ class ClassDeclaration extends TypeDeclaration {
}
methodDecs.addAll(methods.where((p) => p.scope == DeclScope.public).map(
(m) => m.emit(options!..override = isOverride(m.dartName ?? m.name))));
methodDecs.addAll(indexAccessors
.where((p) => p.scope == DeclScope.public)
.map((m) =>
m.emit(options!..override = isOverride(m.dartName ?? m.name))));
methodDecs.addAll(operators.where((p) => p.scope == DeclScope.public).map(
(m) => m.emit(options!..override = isOverride(m.dartName ?? m.name))));
return ExtensionType((e) => e
..name = dartName ?? name
..annotations.addAll([
Expand Down Expand Up @@ -594,8 +590,8 @@ class MethodDeclaration extends CallableDeclaration
final requiredParams = <Parameter>[];
final optionalParams = <Parameter>[];
for (final p in parameters) {
if (p.variardic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variardicArgsCount));
if (p.variadic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variadicArgsCount));
requiredParams.add(p.emit(options));
} else {
if (p.optional) {
Expand Down Expand Up @@ -698,8 +694,8 @@ class ConstructorDeclaration implements MemberDeclaration {
final isFactory = dartName != null && dartName != name;

for (final p in parameters) {
if (p.variardic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variardicArgsCount));
if (p.variadic) {
optionalParams.addAll(spreadParam(p, GlobalOptions.variadicArgsCount));
requiredParams.add(p.emit(options));
} else {
if (p.optional) {
Expand Down Expand Up @@ -767,15 +763,13 @@ class OperatorDeclaration extends CallableDeclaration
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));
if (p.variadic) {
throw UnsupportedError('Variadic parameters are not supported for '
'operators.');
} else if (p.optional) {
optionalParams.add(p.emit(options));
} else {
if (p.optional) {
optionalParams.add(p.emit(options));
} else {
requiredParams.add(p.emit(options));
}
requiredParams.add(p.emit(options));
}
}

Expand All @@ -795,8 +789,7 @@ class OperatorDeclaration extends CallableDeclaration

enum OperatorKind {
squareBracket('[]'),
squareBracketSet('[]='),
bracket('()');
squareBracketSet('[]=');

const OperatorKind(this.expression);
final String expression;
Expand Down
55 changes: 18 additions & 37 deletions web_generator/lib/src/ast/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,21 @@ List<Parameter> spreadParam(ParameterDeclaration p, int count) {
});
}

Set<String> getMemberHierarchy(TypeDeclaration type) {
final members = <String>{};
final Map<String, Set<String>> _memberHierarchyCache = {};

switch (type) {
case ClassDeclaration(
extendedType: final extendee,
implementedTypes: final implementees
):
if (extendee case ReferredType<Declaration>(declaration: final d)
when d is TypeDeclaration) {
members.addAll(_getMemberHierarchy(d));
}
for (final implementedType in implementees) {
if (implementedType case ReferredType<Declaration>(declaration: final d)
when d is TypeDeclaration) {
members.addAll(_getMemberHierarchy(d));
}
}
case InterfaceDeclaration(extendedTypes: final extendees):
for (final extendedType in extendees) {
if (extendedType case ReferredType<Declaration>(declaration: final d)
when d is TypeDeclaration) {
members.addAll(_getMemberHierarchy(d));
}
}
}

return members;
}

Set<String> _getMemberHierarchy(TypeDeclaration type) {
Set<String> getMemberHierarchy(TypeDeclaration type,
[bool addDirectMembers = false]) {
final members = <String>{};

// add direct members
members.addAll(type.methods.map((m) => m.name));
members.addAll(type.properties.map((m) => m.name));
members.addAll(type.operators.map((m) => m.name));
if (addDirectMembers) {
if (_memberHierarchyCache.containsKey(type.name)) {
return _memberHierarchyCache[type.name]!;
}
// add direct members
members.addAll(type.methods.map((m) => m.name));
members.addAll(type.properties.map((m) => m.name));
members.addAll(type.operators.map((m) => m.name));
}

switch (type) {
case ClassDeclaration(
Expand All @@ -96,19 +74,22 @@ Set<String> _getMemberHierarchy(TypeDeclaration type) {
):
if (extendee case ReferredType<Declaration>(declaration: final d)
when d is TypeDeclaration) {
members.addAll(_getMemberHierarchy(d));
members.addAll(
_memberHierarchyCache[d.name] ??= getMemberHierarchy(d, true));
}
for (final implementedType in implementees) {
if (implementedType case ReferredType<Declaration>(declaration: final d)
when d is TypeDeclaration) {
members.addAll(_getMemberHierarchy(d));
members.addAll(
_memberHierarchyCache[d.name] ??= getMemberHierarchy(d, true));
}
}
case InterfaceDeclaration(extendedTypes: final extendees):
for (final extendedType in extendees) {
if (extendedType case ReferredType<Declaration>(declaration: final d)
when d is TypeDeclaration) {
members.addAll(_getMemberHierarchy(d));
members.addAll(
_memberHierarchyCache[d.name] ??= getMemberHierarchy(d, true));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion web_generator/lib/src/interop_gen/transform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'parser.dart';
import 'transform/transformer.dart';

void _setGlobalOptions(Config config) {
GlobalOptions.variardicArgsCount = config.functions?.varArgs ?? 4;
GlobalOptions.variadicArgsCount = config.functions?.varArgs ?? 4;
}

class TransformResult {
Expand Down
21 changes: 10 additions & 11 deletions web_generator/lib/src/interop_gen/transform/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class Transformer {
}) ??
false;

// get heritage clauses
// get heritage clauses
final heritageClauses = interface.heritageClauses?.toDart ?? [];

Expand Down Expand Up @@ -118,10 +117,10 @@ class Transformer {
typeParameters:
typeParams?.map(_transformTypeParamDeclaration).toList() ?? [],
extendedTypes: extendees,
methods: List.empty(growable: true),
properties: List.empty(growable: true),
operators: List.empty(growable: true),
constructors: List.empty(growable: true));
methods: [],
properties: [],
operators: [],
constructors: []);

final interfaceNamer = ScopedUniqueNamer({'get', 'set'});

Expand Down Expand Up @@ -228,7 +227,7 @@ class Transformer {
constructors: List.empty(growable: true),
methods: List.empty(growable: true),
properties: List.empty(growable: true),
indexAccessors: List.empty(growable: true));
operators: List.empty(growable: true));

final classNamer = ScopedUniqueNamer({'get', 'set'});

Expand All @@ -248,9 +247,9 @@ class Transformer {
final (opGet, opSetOrNull) = _transformIndexer(
member as TSIndexSignatureDeclaration,
parent: outputClass);
outputClass.indexAccessors.add(opGet);
outputClass.operators.add(opGet);
if (opSetOrNull case final opSet?) {
outputClass.indexAccessors.add(opSet);
outputClass.operators.add(opSet);
}
break;
case TSSyntaxKind.Constructor:
Expand Down Expand Up @@ -721,15 +720,15 @@ class Transformer {
? _transformType(parameter.type!, parameter: true)
: BuiltinType.anyType;
final isOptional = parameter.questionToken != null;
final isVariardic = parameter.dotDotDotToken != null;
final isvariadic = parameter.dotDotDotToken != null;

// what kind of parameter is this
switch (parameter.name.kind) {
case TSSyntaxKind.Identifier:
return ParameterDeclaration(
name: (parameter.name as TSIdentifier).text,
type: type,
variardic: isVariardic,
variadic: isvariadic,
optional: isOptional);
default:
// TODO: Support Destructured Object Parameters
Expand Down Expand Up @@ -1040,7 +1039,7 @@ class Transformer {
for (final methods in cl.methods) {
filteredDeclarations.addAll(getCallableDependencies(methods));
}
for (final indexAccessors in cl.indexAccessors) {
for (final indexAccessors in cl.operators) {
filteredDeclarations.addAll(getCallableDependencies(indexAccessors));
}
filteredDeclarations.addAll({
Expand Down
2 changes: 1 addition & 1 deletion web_generator/test/assets/test_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ external User get user1;
external User get user2;
@_i1.JS()
external User get adminUser;
typedef User = String;
extension type const UserRole._(int _) {
static const UserRole Guest = UserRole._(0);

Expand All @@ -64,3 +63,4 @@ extension type const UserRole._(int _) {

static const UserRole Administrator = UserRole._(3);
}
typedef User = String;
2 changes: 1 addition & 1 deletion web_generator/test/assets/test_no_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ external User get user1;
external User get user2;
@_i1.JS()
external User get adminUser;
typedef User = String;
extension type const UserRole._(int _) {
static const UserRole Guest = UserRole._(0);

Expand All @@ -59,6 +58,7 @@ extension type const UserRole._(int _) {

static const UserRole Administrator = UserRole._(3);
}
typedef User = String;
extension type const HttpStatusCode._(String _) {
static const HttpStatusCode OK = HttpStatusCode._('200 OK');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ export declare class EpahsImpl<TMeta = any> implements Epahs<TMeta> {
area(unit: 'cm2' | 'in2'): string;
static getById(id: string): EpahsImpl;
toString(): string;
}
}