Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 7 additions & 3 deletions web_generator/lib/src/ast/declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ class EnumMember {
String? dartName;
}

class TypeAliasDeclaration extends NamedDeclaration
class TypeAliasDeclaration extends NestableDeclaration
implements ExportableDeclaration, DocumentedDeclaration {
@override
String name;
Expand All @@ -507,7 +507,8 @@ class TypeAliasDeclaration extends NamedDeclaration
this.typeParameters = const [],
required this.type,
required this.exported,
this.documentation})
this.documentation,
this.parent})
: dartName = null;

@override
Expand All @@ -518,11 +519,14 @@ class TypeAliasDeclaration extends NamedDeclaration
return TypeDef((t) => t
..docs.addAll([...doc])
..annotations.addAll([...annotations])
..name = name
..name = completedDartName
..types
.addAll(typeParameters.map((t) => t.emit(options?.toTypeOptions())))
..definition = type.emit(options?.toTypeOptions()));
}

@override
NestableDeclaration? parent;
}

/// The declaration node for a TypeScript Namespace
Expand Down
12 changes: 8 additions & 4 deletions web_generator/lib/src/ast/merger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import 'types.dart';
ClassDeclaration? classDecl; // there can be only 1 class
TypeAliasDeclaration? typealiasDecl; // there can be only 1 typedef
final namespaces = <NamespaceDeclaration>[];
final otherVariableDeclarations = <VariableDeclaration>[];
final varDeclarations = <VariableDeclaration>[];
final varDeclarationsWithBuiltinTypes = <VariableDeclaration>[];
final otherDeclarations = <Declaration>[];
Expand Down Expand Up @@ -97,6 +98,8 @@ import 'types.dart';
)
when modifier == VariableModifier.$var:
varDeclarationsWithBuiltinTypes.add(decl);
case final VariableDeclaration v:
otherVariableDeclarations.add(v);
default:
otherDeclarations.add(decl);
break;
Expand All @@ -115,7 +118,7 @@ import 'types.dart';
assert(interfaces.isEmpty && namespaces.isEmpty,
'Typedefs in TS do not allow other decls');

for (final varDecl in varDeclarations) {
for (final varDecl in [...varDeclarations, ...otherVariableDeclarations]) {
final VariableDeclaration(
type: varDeclType,
name: varDeclName,
Expand All @@ -126,10 +129,11 @@ import 'types.dart';
referredDecl.name == typealiasDecl.name) {
// change type of var decl
varDecl.type = referredDecl.type;
output.add(varDecl);
}
}

output.addAll([...functions, ...varDeclarations]);
output.addAll([...functions]);
} else if (mergedNamespace != null) {
var mergedComposite = mergedNamespace.asComposite
..mergeType(mergedInterface);
Expand Down Expand Up @@ -158,7 +162,7 @@ import 'types.dart';

additionals.addAll([if (enumDecl != null) enumDecl]);

output.add(mergedComposite);
output.addAll([mergedComposite, ...otherVariableDeclarations]);
} else if (mergedInterface != null) {
// merge em and vars
mergedInterface = _mergeInterfaceWithVars(mergedInterface, varDeclarations)
Expand All @@ -182,7 +186,7 @@ import 'types.dart';
}

// that's it
output.addAll(functions);
output.addAll([...functions, ...otherVariableDeclarations]);
} else {
return (declarations, additionals: []);
}
Expand Down
62 changes: 45 additions & 17 deletions web_generator/lib/src/interop_gen/transform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,16 @@ class ProgramMap {
/// The files in the given project
final p.PathSet files;

/// A list of declarations to include
final List<String> filterDeclSet;

/// The declarations as globs
List<RegExp> get filterDeclSetPatterns => filterDeclSet.map((decl) {
final escapedDecl = RegExp.escape(decl);
if (escapedDecl == decl) return RegExp('^$decl\$');
return RegExp(decl);
}).toList();

final bool generateAll;

final bool strictUnsupported;
Expand All @@ -201,28 +209,42 @@ class ProgramMap {
} else {
final src = program.getSourceFile(file);

final transformer =
_activeTransformers.putIfAbsent(file, () => Transformer(this, src));
if (src == null && !strictUnsupported) {
// print warning
print('WARN: Could not find file $file');
// try to transform by yourself
final anonymousTransformer = _activeTransformers.putIfAbsent(
file, () => Transformer(this, null, file: file));

// TODO: Replace with .transformAndReturn once #388 lands
return anonymousTransformer.transformAndReturn(node);
} else {
final transformer =
_activeTransformers.putIfAbsent(file, () => Transformer(this, src));

if (!transformer.nodes.contains(node)) {
if (declName case final d?
when transformer.nodeMap.findByName(d).isEmpty) {
// find the source file decl
if (src == null) return null;
if (!transformer.nodes.contains(node)) {
if (declName case final d?
when transformer.nodeMap.findByName(d).isEmpty) {
// find the source file decl
if (src == null) return null;

final symbol = typeChecker.getSymbolAtLocation(src)!;
final exports = symbol.exports?.toDart ?? {};
final symbol = typeChecker.getSymbolAtLocation(src)!;
final exports = symbol.exports?.toDart ?? {};

final targetSymbol = exports[d.toJS]!;
final targetSymbol = exports[d.toJS]!;

transformer.transform(targetSymbol.getDeclarations()!.toDart.first);
} else {
transformer.transform(node);
for (final decl in targetSymbol.getDeclarations()?.toDart ??
<TSDeclaration>[]) {
transformer.transform(decl);
}
} else {
transformer.transform(node);
}
}
}

nodeMap = transformer.processAndReturn();
_activeTransformers[file] = transformer;
nodeMap = transformer.processAndReturn();
_activeTransformers[file] = transformer;
}
}

final name = declName ?? (node as TSNamedDeclaration).name?.text;
Expand Down Expand Up @@ -287,8 +309,14 @@ class ProgramMap {
} else {
final exportedSymbols = sourceSymbol.exports?.toDart;

for (final MapEntry(value: symbol)
for (final MapEntry(key: symbolName, value: symbol)
in exportedSymbols?.entries ?? <MapEntry<JSString, TSSymbol>>[]) {
// if there are decls to filter by and it does not match any, skip
if (!filterDeclSetPatterns
.any((f) => f.hasMatch(symbolName.toDart)) &&
filterDeclSet.isNotEmpty) {
continue;
}
final decls = symbol.getDeclarations()?.toDart ?? [];
try {
final aliasedSymbol = typeChecker.getAliasedSymbol(symbol);
Expand Down
Loading