Skip to content

Commit

Permalink
Use least common supertypes for unions
Browse files Browse the repository at this point in the history
It's beneficial to have a more restrictive type for some APIs to get
better typing.

1. Precomputes a type hierarchy of JS types
2. Modifies type calculation to handle union types
3. Desugars typedefs/interfaces/etc. in the case of union types to
get a better bound
4. Adds some missing type aliases
  • Loading branch information
srujzs committed Oct 18, 2023
1 parent d859db0 commit d468fa6
Show file tree
Hide file tree
Showing 44 changed files with 396 additions and 164 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.1-dev

- Updated types to account for union types.

## 0.3.0

- Updated source IDL to `v3.39.1`.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/dom/credential_management.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'fedcm.dart';
import 'web_otp.dart';
import 'webauthn.dart';

typedef PasswordCredentialInit = JSAny?;
typedef PasswordCredentialInit = JSObject;
typedef CredentialMediationRequirement = String;

@JS('Credential')
Expand Down Expand Up @@ -113,7 +113,7 @@ extension CredentialCreationOptionsExtension on CredentialCreationOptions {
@JS('PasswordCredential')
@staticInterop
class PasswordCredential implements Credential, CredentialUserData {
external factory PasswordCredential(JSAny? dataOrForm);
external factory PasswordCredential(JSObject dataOrForm);
}

extension PasswordCredentialExtension on PasswordCredential {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/css_animation_worklet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension WorkletAnimationEffectExtension on WorkletAnimationEffect {
class WorkletAnimation implements Animation {
external factory WorkletAnimation(
String animatorName, [
JSAny? effects,
JSObject? effects,
AnimationTimeline? timeline,
JSAny? options,
]);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/css_font_loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:js_interop';
import 'dom.dart';
import 'html.dart';

typedef BinaryData = JSAny?;
typedef BinaryData = JSObject;
typedef FontFaceLoadStatus = String;
typedef FontFaceSetLoadStatus = String;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/css_pseudo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ extension CSSPseudoElementExtension on CSSPseudoElement {
external CSSPseudoElement? pseudo(String type);
external String get type;
external Element get element;
external JSAny? get parent;
external JSObject get parent;
}
2 changes: 1 addition & 1 deletion lib/src/dom/css_typed_om.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class CSSImageValue implements CSSStyleValue {}
@JS('CSSColorValue')
@staticInterop
class CSSColorValue implements CSSStyleValue {
external static JSAny? parse(String cssText);
external static JSObject parse(String cssText);
}

@JS('CSSRGB')
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/cssom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class StyleSheet implements JSObject {}
extension StyleSheetExtension on StyleSheet {
external String get type;
external String? get href;
external JSAny? get ownerNode;
external JSObject? get ownerNode;
external CSSStyleSheet? get parentStyleSheet;
external String? get title;
external MediaList get media;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/cssom_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'geometry.dart';
import 'html.dart';
import 'screen_orientation.dart';

typedef GeometryNode = JSAny?;
typedef GeometryNode = JSObject;
typedef ScrollBehavior = String;
typedef ScrollLogicalPosition = String;
typedef CSSBoxType = String;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ extension DocumentExtension on Document {
);
external JSPromise exitFullscreen();
external NodeList getElementsByName(String elementName);
external JSAny? open([
external JSObject? open([
String unused1OrUrl,
String nameOrUnused2,
String features,
Expand Down
46 changes: 23 additions & 23 deletions lib/src/dom/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ import 'webxr.dart';
import 'window_controls_overlay.dart';
import 'xhr.dart';

typedef HTMLOrSVGScriptElement = JSAny?;
typedef MediaProvider = JSAny?;
typedef RenderingContext = JSAny?;
typedef HTMLOrSVGImageElement = JSAny?;
typedef CanvasImageSource = JSAny?;
typedef OffscreenRenderingContext = JSAny?;
typedef HTMLOrSVGScriptElement = JSObject;
typedef MediaProvider = JSObject;
typedef RenderingContext = JSObject;
typedef HTMLOrSVGImageElement = JSObject;
typedef CanvasImageSource = JSObject;
typedef OffscreenRenderingContext = JSObject;
typedef EventHandler = EventHandlerNonNull?;
typedef OnErrorEventHandler = OnErrorEventHandlerNonNull?;
typedef OnBeforeUnloadEventHandler = OnBeforeUnloadEventHandlerNonNull?;
typedef TimerHandler = JSAny?;
typedef ImageBitmapSource = JSAny?;
typedef MessageEventSource = JSAny?;
typedef ImageBitmapSource = JSObject;
typedef MessageEventSource = JSObject;
typedef BlobCallback = JSFunction;
typedef CustomElementConstructor = JSFunction;
typedef FunctionStringCallback = JSFunction;
Expand Down Expand Up @@ -138,8 +138,8 @@ typedef WorkerType = String;
class HTMLAllCollection implements JSObject {}

extension HTMLAllCollectionExtension on HTMLAllCollection {
external JSAny? namedItem(String name);
external JSAny? item([String nameOrIndex]);
external JSObject? namedItem(String name);
external JSObject? item([String nameOrIndex]);
external int get length;
}

Expand All @@ -148,7 +148,7 @@ extension HTMLAllCollectionExtension on HTMLAllCollection {
class HTMLFormControlsCollection implements HTMLCollection {}

extension HTMLFormControlsCollectionExtension on HTMLFormControlsCollection {
external JSAny? namedItem(String name);
external JSObject? namedItem(String name);
}

@JS('RadioNodeList')
Expand All @@ -166,7 +166,7 @@ class HTMLOptionsCollection implements HTMLCollection {}

extension HTMLOptionsCollectionExtension on HTMLOptionsCollection {
external JSVoid add(
JSAny? element, [
JSObject element, [
JSAny? before,
]);
external JSVoid remove(int index);
Expand Down Expand Up @@ -1182,19 +1182,19 @@ class TrackEvent implements Event {
}

extension TrackEventExtension on TrackEvent {
external JSAny? get track;
external JSObject? get track;
}

@JS()
@staticInterop
@anonymous
class TrackEventInit implements EventInit {
external factory TrackEventInit({JSAny? track});
external factory TrackEventInit({JSObject? track});
}

extension TrackEventInitExtension on TrackEventInit {
external set track(JSAny? value);
external JSAny? get track;
external set track(JSObject? value);
external JSObject? get track;
}

@JS('HTMLMapElement')
Expand Down Expand Up @@ -1606,7 +1606,7 @@ extension HTMLSelectElementExtension on HTMLSelectElement {
external HTMLOptionElement? item(int index);
external HTMLOptionElement? namedItem(String name);
external JSVoid add(
JSAny? element, [
JSObject element, [
JSAny? before,
]);
external JSVoid remove([int index]);
Expand Down Expand Up @@ -2002,7 +2002,7 @@ class HTMLSlotElement implements HTMLElement {
extension HTMLSlotElementExtension on HTMLSlotElement {
external JSArray assignedNodes([AssignedNodesOptions options]);
external JSArray assignedElements([AssignedNodesOptions options]);
external JSVoid assign(JSAny? nodes);
external JSVoid assign(JSObject nodes);
external set name(String value);
external String get name;
}
Expand Down Expand Up @@ -2281,7 +2281,7 @@ class CanvasUserInterface implements JSObject {}

extension CanvasUserInterfaceExtension on CanvasUserInterface {
external JSVoid drawFocusIfNeeded(
JSAny? elementOrPath, [
JSObject elementOrPath, [
Element element,
]);
external JSVoid scrollPathIntoView([Path2D path]);
Expand Down Expand Up @@ -2554,7 +2554,7 @@ class ImageBitmapRenderingContext implements JSObject {}

extension ImageBitmapRenderingContextExtension on ImageBitmapRenderingContext {
external JSVoid transferFromImageBitmap(ImageBitmap? bitmap);
external JSAny? get canvas;
external JSObject get canvas;
}

@JS()
Expand Down Expand Up @@ -4228,7 +4228,7 @@ class MessagePort implements EventTarget {}
extension MessagePortExtension on MessagePort {
external JSVoid postMessage(
JSAny? message, [
JSAny? optionsOrTransfer,
JSObject optionsOrTransfer,
]);
external JSVoid start();
external JSVoid close();
Expand Down Expand Up @@ -4298,7 +4298,7 @@ class DedicatedWorkerGlobalScope
extension DedicatedWorkerGlobalScopeExtension on DedicatedWorkerGlobalScope {
external JSVoid postMessage(
JSAny? message, [
JSAny? optionsOrTransfer,
JSObject optionsOrTransfer,
]);
external JSVoid close();
external String get name;
Expand Down Expand Up @@ -4343,7 +4343,7 @@ extension WorkerExtension on Worker {
external JSVoid terminate();
external JSVoid postMessage(
JSAny? message, [
JSAny? optionsOrTransfer,
JSObject optionsOrTransfer,
]);
external set onmessage(EventHandler value);
external EventHandler get onmessage;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/image_capture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:js_interop';

import 'mediacapture_streams.dart';

typedef ConstrainPoint2D = JSAny?;
typedef ConstrainPoint2D = JSObject;
typedef RedEyeReduction = String;
typedef FillLightMode = String;
typedef MeteringMode = String;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/dom/indexeddb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IDBRequest implements EventTarget {}
extension IDBRequestExtension on IDBRequest {
external JSAny? get result;
external DOMException? get error;
external JSAny? get source;
external JSObject? get source;
external IDBTransaction? get transaction;
external IDBRequestReadyState get readyState;
external set onsuccess(EventHandler value);
Expand Down Expand Up @@ -303,7 +303,7 @@ extension IDBCursorExtension on IDBCursor {
);
external IDBRequest update(JSAny? value);
external IDBRequest delete();
external JSAny? get source;
external JSObject get source;
external IDBCursorDirection get direction;
external JSAny? get key;
external JSAny? get primaryKey;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/dom/intersection_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension IntersectionObserverExtension on IntersectionObserver {
external JSVoid unobserve(Element target);
external JSVoid disconnect();
external JSArray takeRecords();
external JSAny? get root;
external JSObject? get root;
external String get rootMargin;
external String get scrollMargin;
external JSArray get thresholds;
Expand Down Expand Up @@ -85,16 +85,16 @@ extension IntersectionObserverEntryInitExtension
@anonymous
class IntersectionObserverInit implements JSObject {
external factory IntersectionObserverInit({
JSAny? root,
JSObject? root,
String rootMargin,
String scrollMargin,
JSAny? threshold,
});
}

extension IntersectionObserverInitExtension on IntersectionObserverInit {
external set root(JSAny? value);
external JSAny? get root;
external set root(JSObject? value);
external JSObject? get root;
external set rootMargin(String value);
external String get rootMargin;
external set scrollMargin(String value);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/mediacapture_streams.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef MediaDeviceKind = String;
@JS('MediaStream')
@staticInterop
class MediaStream implements EventTarget {
external factory MediaStream([JSAny? streamOrTracks]);
external factory MediaStream([JSObject streamOrTracks]);
}

extension MediaStreamExtension on MediaStream {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/orientation_sensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:js_interop';

import 'generic_sensor.dart';

typedef RotationMatrixType = JSAny?;
typedef RotationMatrixType = JSObject;
typedef OrientationSensorLocalCoordinateSystem = String;

@JS('OrientationSensor')
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/sanitizer_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Sanitizer implements JSObject {
}

extension SanitizerExtension on Sanitizer {
external DocumentFragment sanitize(JSAny? input);
external DocumentFragment sanitize(JSObject input);
external Element? sanitizeFor(
String element,
String input,
Expand Down
12 changes: 6 additions & 6 deletions lib/src/dom/service_workers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ServiceWorker implements EventTarget, AbstractWorker {}
extension ServiceWorkerExtension on ServiceWorker {
external JSVoid postMessage(
JSAny? message, [
JSAny? optionsOrTransfer,
JSObject optionsOrTransfer,
]);
external String get scriptURL;
external ServiceWorkerState get state;
Expand Down Expand Up @@ -193,7 +193,7 @@ class Client implements JSObject {}
extension ClientExtension on Client {
external JSVoid postMessage(
JSAny? message, [
JSAny? optionsOrTransfer,
JSObject optionsOrTransfer,
]);
external ClientLifecycleState get lifecycleState;
external String get url;
Expand Down Expand Up @@ -323,7 +323,7 @@ extension ExtendableMessageEventExtension on ExtendableMessageEvent {
external JSAny? get data;
external String get origin;
external String get lastEventId;
external JSAny? get source;
external JSObject? get source;
external JSArray get ports;
}

Expand All @@ -335,7 +335,7 @@ class ExtendableMessageEventInit implements ExtendableEventInit {
JSAny? data,
String origin,
String lastEventId,
JSAny? source,
JSObject? source,
JSArray ports,
});
}
Expand All @@ -347,8 +347,8 @@ extension ExtendableMessageEventInitExtension on ExtendableMessageEventInit {
external String get origin;
external set lastEventId(String value);
external String get lastEventId;
external set source(JSAny? value);
external JSAny? get source;
external set source(JSObject? value);
external JSObject? get source;
external set ports(JSArray value);
external JSArray get ports;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/dom/streams.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import 'dart:js_interop';
import 'dom.dart';
import 'webidl.dart';

typedef ReadableStreamReader = JSAny?;
typedef ReadableStreamController = JSAny?;
typedef ReadableStreamReader = JSObject;
typedef ReadableStreamController = JSObject;
typedef UnderlyingSourceStartCallback = JSFunction;
typedef UnderlyingSourcePullCallback = JSFunction;
typedef UnderlyingSourceCancelCallback = JSFunction;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ extension SVGElementInstanceExtension on SVGElementInstance {
class ShadowAnimation implements Animation {
external factory ShadowAnimation(
Animation source,
JSAny? newTarget,
JSObject newTarget,
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/trusted_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:js_interop';
typedef HTMLString = String;
typedef ScriptString = String;
typedef ScriptURLString = String;
typedef TrustedType = JSAny?;
typedef TrustedType = JSObject;
typedef CreateHTMLCallback = JSFunction;
typedef CreateScriptCallback = JSFunction;
typedef CreateScriptURLCallback = JSFunction;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dom/url.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class URL implements JSObject {
String base,
]);

external static String createObjectURL(JSAny? obj);
external static String createObjectURL(JSObject obj);
external static JSVoid revokeObjectURL(String url);
external static bool canParse(
String url, [
Expand Down
Loading

0 comments on commit d468fa6

Please sign in to comment.