-
Notifications
You must be signed in to change notification settings - Fork 36
[interop] Resolved Namespace typedef
and exception improvements
#459
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
Open
nikeokoronkwo
wants to merge
2
commits into
dart-lang:main
Choose a base branch
from
nikeokoronkwo:fix/namespace-def-res
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import '../../ast/documentation.dart'; | |
import '../../ast/helpers.dart'; | ||
import '../../ast/types.dart'; | ||
import '../../js/annotations.dart'; | ||
import '../../js/filesystem_api.dart'; | ||
import '../../js/helpers.dart'; | ||
import '../../js/typescript.dart' as ts; | ||
import '../../js/typescript.types.dart'; | ||
|
@@ -101,14 +102,14 @@ class Transformer { | |
void transform(TSNode node) { | ||
if (nodes.contains(node)) return; | ||
|
||
final decls = _transform(node); | ||
final decls = transformAndReturn(node); | ||
|
||
nodeMap.addAll({for (final d in decls) d.id.toString(): d}); | ||
|
||
nodes.add(node); | ||
} | ||
|
||
List<Declaration> _transform(TSNode node, | ||
List<Declaration> transformAndReturn(TSNode node, | ||
{Set<ExportReference>? exportSet, | ||
UniqueNamer? namer, | ||
NamespaceDeclaration? parent}) { | ||
|
@@ -287,6 +288,33 @@ class Transformer { | |
} | ||
} | ||
|
||
void transformDeclAndAppendParent( | ||
NamespaceDeclaration outputNamespace, TSNode decl) { | ||
if (outputNamespace.nodes.contains(decl)) return; | ||
final outputDecls = | ||
transformAndReturn(decl, namer: scopedNamer, parent: outputNamespace); | ||
switch (decl.kind) { | ||
case TSSyntaxKind.ClassDeclaration || TSSyntaxKind.InterfaceDeclaration: | ||
final outputDecl = outputDecls.single as TypeDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
case TSSyntaxKind.EnumDeclaration: | ||
final outputDecl = outputDecls.single as EnumDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
case TSSyntaxKind.TypeAliasDeclaration: | ||
final outputDecl = outputDecls.single as TypeAliasDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
default: | ||
outputNamespace.topLevelDeclarations.addAll(outputDecls); | ||
} | ||
outputNamespace.nodes.add(decl); | ||
|
||
// update namespace state | ||
updateNSInParent(); | ||
} | ||
|
||
// preload nodemap | ||
updateNSInParent(); | ||
|
||
|
@@ -306,26 +334,7 @@ class Transformer { | |
for (final decl in decls) { | ||
// TODO: We could also ignore namespace decls with the same name, as | ||
// a single instance should consider such non-necessary | ||
if (outputNamespace.nodes.contains(decl)) continue; | ||
final outputDecls = | ||
_transform(decl, namer: scopedNamer, parent: outputNamespace); | ||
switch (decl.kind) { | ||
case TSSyntaxKind.ClassDeclaration || | ||
TSSyntaxKind.InterfaceDeclaration: | ||
final outputDecl = outputDecls.first as TypeDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
case TSSyntaxKind.EnumDeclaration: | ||
final outputDecl = outputDecls.first as EnumDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
default: | ||
outputNamespace.topLevelDeclarations.addAll(outputDecls); | ||
} | ||
outputNamespace.nodes.add(decl); | ||
|
||
// update namespace state | ||
updateNSInParent(); | ||
transformDeclAndAppendParent(outputNamespace, decl); | ||
} | ||
} | ||
// fallback | ||
|
@@ -334,24 +343,7 @@ class Transformer { | |
when namespaceBody.kind == TSSyntaxKind.ModuleBlock) { | ||
for (final statement | ||
in (namespaceBody as TSModuleBlock).statements.toDart) { | ||
final outputDecls = _transform(statement, | ||
namer: scopedNamer, parent: outputNamespace); | ||
switch (statement.kind) { | ||
case TSSyntaxKind.ClassDeclaration || | ||
TSSyntaxKind.InterfaceDeclaration: | ||
final outputDecl = outputDecls.first as TypeDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
case TSSyntaxKind.EnumDeclaration: | ||
final outputDecl = outputDecls.first as EnumDeclaration; | ||
outputDecl.parent = outputNamespace; | ||
outputNamespace.nestableDeclarations.add(outputDecl); | ||
default: | ||
outputNamespace.topLevelDeclarations.addAll(outputDecls); | ||
} | ||
|
||
// update namespace state | ||
updateNSInParent(); | ||
transformDeclAndAppendParent(outputNamespace, statement); | ||
} | ||
} else if (namespace.body case final namespaceBody?) { | ||
// namespace import | ||
|
@@ -1041,7 +1033,8 @@ class Transformer { | |
default: | ||
// TODO: Support Destructured Object Parameters | ||
// and Destructured Array Parameters | ||
throw Exception('Unsupported Parameter Name kind ${parameter.kind}'); | ||
throw Exception( | ||
'Unsupported Parameter Name kind ${parameter.name.kind}'); | ||
} | ||
} | ||
|
||
|
@@ -1527,6 +1520,17 @@ class Transformer { | |
bool isNotTypableDeclaration = false, | ||
bool typeArg = false, | ||
bool isNullable = false}) { | ||
print(( | ||
name.map((d) => d.part).join('.'), | ||
parentName: parent?.qualifiedName, | ||
symbol.getDeclarations()?.toDart.map((d) => d.kind), | ||
isNullable: isNullable, | ||
typeArg: typeArg, | ||
parentDecls: { | ||
...?parent?.namespaceDeclarations, | ||
...?parent?.nestableDeclarations | ||
}.map((d) => d.name) | ||
)); | ||
// get name and map | ||
final firstName = name.first.part; | ||
|
||
|
@@ -1577,9 +1581,10 @@ class Transformer { | |
...parent.topLevelDeclarations | ||
].map((d) => d.id.toString())) | ||
: null; | ||
|
||
// TODO: multi-decls | ||
final transformedDecls = | ||
_transform(firstDecl, namer: namer, parent: parent); | ||
transformAndReturn(firstDecl, namer: namer, parent: parent); | ||
|
||
if (parent != null) { | ||
switch (firstDecl.kind) { | ||
|
@@ -1613,6 +1618,12 @@ class Transformer { | |
} | ||
|
||
// get node finally | ||
print(( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: stray print |
||
name: name.map((n) => n.part).join('.'), | ||
declarationsMatching.map((d) => (d.id, d is NamedDeclaration)), | ||
symbol.getDeclarations()?.toDart.map((d) => d.kind), | ||
name.first | ||
)); | ||
final decl = declarationsMatching.whereType<NamedDeclaration>().first; | ||
|
||
// are we done? | ||
|
@@ -1868,8 +1879,15 @@ class Transformer { | |
isNullable: isNullable); | ||
} else { | ||
// if import there and not this file, imported from specified file | ||
final importUrl = | ||
!nameImport.endsWith('.d.ts') ? '$nameImport.d.ts' : nameImport; | ||
final importUrl = !nameImport.endsWith('.d.ts') && | ||
fs | ||
.existsSync((p.isAbsolute('$nameImport.d.ts') | ||
? '$nameImport.d.ts' | ||
: p.join(p.dirname(file), '$nameImport.d.ts')) | ||
.toJS) | ||
.toDart | ||
? '$nameImport.d.ts' | ||
: nameImport; | ||
final relativePath = programMap.files.contains(importUrl) | ||
? p.relative(importUrl, from: p.dirname(file)) | ||
: null; | ||
|
@@ -1915,7 +1933,9 @@ class Transformer { | |
} | ||
} | ||
} | ||
throw Exception('Could not resolve type for node'); | ||
throw Exception( | ||
'Could not resolve type for node ${fullyQualifiedName.asName}' | ||
'${nameImport == null ? '' : ' from $nameImport'}'); | ||
} | ||
|
||
/// Get the type of a type node named [typeName] by referencing its | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: stray print