Skip to content

Commit 5e5adc8

Browse files
authored
Use URI instead of library name (#191)
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.
1 parent 2f00226 commit 5e5adc8

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

tool/update_bindings.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,27 @@ Future<void> _runProc(
159159
}
160160
}
161161

162-
bool _isInJsTypesOrJsInterop(InterfaceElement element) =>
163-
element.library.isInSdk &&
164-
(element.library.name == '_js_types' ||
165-
element is ExtensionTypeElement &&
166-
element.library.name == 'dart.js_interop');
167-
162+
// Generates a map of the JS type hierarchy defined in `dart:js_interop` that is
163+
// used by the translator to handle IDL types.
168164
Future<void> _generateJsTypeSupertypes() async {
169165
// Use a file that uses `dart:js_interop` for analysis.
170166
final contextCollection = AnalysisContextCollection(includedPaths: [
171167
p.fromUri(Platform.script.resolve('../lib/src/dom.dart'))
172168
]);
173-
final dartJsInterop = await contextCollection.contexts.single.currentSession
174-
.getLibraryByUri('dart:js_interop') as LibraryElementResult;
175-
final definedNames = dartJsInterop.element.exportNamespace.definedNames;
169+
final dartJsInterop = (await contextCollection.contexts.single.currentSession
170+
.getLibraryByUri('dart:js_interop') as LibraryElementResult)
171+
.element;
172+
final definedNames = dartJsInterop.exportNamespace.definedNames;
176173
// `SplayTreeMap` to avoid moving types around in `dart:js_interop` affecting
177174
// the code generation.
178175
final jsTypeSupertypes = SplayTreeMap<String, String?>();
179176
for (final name in definedNames.keys) {
180177
final element = definedNames[name];
181178
if (element is ExtensionTypeElement) {
182-
if (!_isInJsTypesOrJsInterop(element)) return;
179+
// Only extension types defined in `dart:js_interop` are JS types.
180+
bool isJSType(InterfaceElement element) =>
181+
element is ExtensionTypeElement && element.library == dartJsInterop;
182+
if (!isJSType(element)) continue;
183183

184184
String? parentJsType;
185185
final supertype = element.supertype;
@@ -190,7 +190,7 @@ Future<void> _generateJsTypeSupertypes() async {
190190
// We should have at most one non-trivial supertype.
191191
assert(immediateSupertypes.length <= 1);
192192
for (final supertype in immediateSupertypes) {
193-
if (_isInJsTypesOrJsInterop(supertype.element)) {
193+
if (isJSType(supertype.element)) {
194194
parentJsType = "'${supertype.element.name}'";
195195
}
196196
}

0 commit comments

Comments
 (0)