Skip to content

Commit

Permalink
Use URI instead of library name (#191)
Browse files Browse the repository at this point in the history
When analyzing library elements, we should use their import
URIs instead of their library name. This is especially relevant
as https://dart-review.googlesource.com/c/sdk/+/352977 is
planning to remove the library name from dart:js_interop.

Also fixes an issue where we were returning instead of continuing
in the loop that iterates over dart:js_interop.
  • Loading branch information
srujzs committed Feb 29, 2024
1 parent 2f00226 commit 5e5adc8
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions tool/update_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,27 @@ Future<void> _runProc(
}
}

bool _isInJsTypesOrJsInterop(InterfaceElement element) =>
element.library.isInSdk &&
(element.library.name == '_js_types' ||
element is ExtensionTypeElement &&
element.library.name == 'dart.js_interop');

// Generates a map of the JS type hierarchy defined in `dart:js_interop` that is
// used by the translator to handle IDL types.
Future<void> _generateJsTypeSupertypes() async {
// Use a file that uses `dart:js_interop` for analysis.
final contextCollection = AnalysisContextCollection(includedPaths: [
p.fromUri(Platform.script.resolve('../lib/src/dom.dart'))
]);
final dartJsInterop = await contextCollection.contexts.single.currentSession
.getLibraryByUri('dart:js_interop') as LibraryElementResult;
final definedNames = dartJsInterop.element.exportNamespace.definedNames;
final dartJsInterop = (await contextCollection.contexts.single.currentSession
.getLibraryByUri('dart:js_interop') as LibraryElementResult)
.element;
final definedNames = dartJsInterop.exportNamespace.definedNames;
// `SplayTreeMap` to avoid moving types around in `dart:js_interop` affecting
// the code generation.
final jsTypeSupertypes = SplayTreeMap<String, String?>();
for (final name in definedNames.keys) {
final element = definedNames[name];
if (element is ExtensionTypeElement) {
if (!_isInJsTypesOrJsInterop(element)) return;
// Only extension types defined in `dart:js_interop` are JS types.
bool isJSType(InterfaceElement element) =>
element is ExtensionTypeElement && element.library == dartJsInterop;
if (!isJSType(element)) continue;

String? parentJsType;
final supertype = element.supertype;
Expand All @@ -190,7 +190,7 @@ Future<void> _generateJsTypeSupertypes() async {
// We should have at most one non-trivial supertype.
assert(immediateSupertypes.length <= 1);
for (final supertype in immediateSupertypes) {
if (_isInJsTypesOrJsInterop(supertype.element)) {
if (isJSType(supertype.element)) {
parentJsType = "'${supertype.element.name}'";
}
}
Expand Down

0 comments on commit 5e5adc8

Please sign in to comment.