Skip to content

Commit

Permalink
Merge pull request #457 from saschanaz/fullautoexpose
Browse files Browse the repository at this point in the history
Autoexpose all types
  • Loading branch information
mhegazy authored Apr 27, 2018
2 parents c6f87f1 + 4bf3310 commit 8587c6d
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 213 deletions.
43 changes: 4 additions & 39 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2051,20 +2051,8 @@ declare var XMLHttpRequestUpload: {

declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject;

interface ErrorEventHandler {
(event: Event | string, source?: string, fileno?: number, columnNumber?: number, error?: Error): void;
}

interface ForEachCallback {
(keyId: Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | ArrayBuffer | null, status: MediaKeyStatus): void;
}

interface FunctionStringCallback {
(data: string): void;
}

interface NotificationPermissionCallback {
(permission: NotificationPermission): void;
interface EventHandlerNonNull {
(event: Event): any;
}

interface PerformanceObserverCallback {
Expand Down Expand Up @@ -2107,43 +2095,20 @@ type HeadersInit = Headers | string[][] | Record<string, string>;
type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream | string;
type RequestInfo = Request | string;
type BlobPart = BufferSource | Blob | string;
type DOMHighResTimeStamp = number;
type PerformanceEntryList = PerformanceEntry[];
type PushMessageDataInit = BufferSource | string;
type VibratePattern = number | number[];
type BufferSource = ArrayBufferView | ArrayBuffer;
type DOMTimeStamp = number;
type FormDataEntryValue = File | string;
type IDBValidKey = number | string | Date | BufferSource | IDBArrayKey;
type AlgorithmIdentifier = string | Algorithm;
type AAGUID = string;
type ByteString = string;
type CryptoOperationData = ArrayBufferView;
type GLbitfield = number;
type GLboolean = boolean;
type GLbyte = number;
type GLclampf = number;
type GLenum = number;
type GLfloat = number;
type GLint = number;
type GLintptr = number;
type GLshort = number;
type GLsizei = number;
type GLsizeiptr = number;
type GLubyte = number;
type GLuint = number;
type GLushort = number;
type IDBKeyPath = string;
type USVString = string;
type payloadtype = number;
type MessageEventSource = object | MessagePort | ServiceWorker;
type BinaryType = "blob" | "arraybuffer";
type ClientTypes = "window" | "worker" | "sharedworker" | "all";
type IDBCursorDirection = "next" | "nextunique" | "prev" | "prevunique";
type IDBRequestReadyState = "pending" | "done";
type IDBTransactionMode = "readonly" | "readwrite" | "versionchange";
type KeyFormat = "raw" | "spki" | "pkcs8" | "jwk";
type KeyType = "public" | "private" | "secret";
type KeyUsage = "encrypt" | "decrypt" | "sign" | "verify" | "deriveKey" | "deriveBits" | "wrapKey" | "unwrapKey";
type MediaKeyStatus = "usable" | "expired" | "output-downscaled" | "output-not-allowed" | "status-pending" | "internal-error";
type NotificationDirection = "auto" | "ltr" | "rtl";
type NotificationPermission = "default" | "denied" | "granted";
type PushEncryptionKeyName = "p256dh" | "auth";
Expand Down
8 changes: 4 additions & 4 deletions inputfiles/addedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@
"[index: number]: TNode"
],
"iterator": {
"type": "iterable",
"subtype": [{
"kind": "iterable",
"type": [{
"override-type": "TNode"
}]
}
Expand Down Expand Up @@ -546,8 +546,8 @@
"[index: number]: T"
],
"iterator": {
"type": "iterable",
"subtype": [{
"kind": "iterable",
"type": [{
"override-type": "T"
}]
}
Expand Down
134 changes: 3 additions & 131 deletions inputfiles/knownWorkerTypes.json
Original file line number Diff line number Diff line change
@@ -1,135 +1,7 @@
[
"ClientQueryOptions",
"ExtendableEventInit",
"ExtendableMessageEventInit",
"FetchEventInit",
"NotificationEventInit",
"PushEventInit",
"SyncEventInit",
"Algorithm",
"CacheQueryOptions",
"CloseEventInit",
"EventInit",
"GetNotificationOptions",
"IDBIndexParameters",
"IDBObjectStoreParameters",
"KeyAlgorithm",
"MessageEventInit",
"NotificationOptions",
"PushSubscriptionOptionsInit",
"RequestInit",
"ResponseInit",
"DOMMatrixInit",
"DOMMatrix2DInit",
"DOMPointInit",
"DOMQuadInit",
"DOMRectInit",
"BlobPart",
"IDBKeyRange",
"MessageEventSource",
"MessagePort",
"NavigationPreloadState",
"NotificationAction",
"PerformanceEntryList",
"PerformanceObserverCallback",
"PerformanceObserverInit",
"PromiseRejectionEventInit",
"PushSubscriptionJSON",
"RegistrationOptions",
"ServiceWorkerUpdateViaCache",
"VibratePattern",
"WorkerType",
"AbstractWorker",
"Body",
"GlobalFetch",
"NavigatorBeacon",
"NavigatorConcurrentHardware",
"NavigatorID",
"NavigatorOnLine",
"WindowBase64",
"WindowConsole",
"Client",
"Clients",
"DedicatedWorkerGlobalScope",
"ServiceWorkerGlobalScope",
"WindowClient",
"WorkerGlobalScope",
"WorkerLocation",
"WorkerNavigator",
"WorkerUtils",
"EventListener",
"ErrorEventHandler",
"ForEachCallback",
"FunctionStringCallback",
"NotificationPermissionCallback",
"PushMessageDataInit",
"AAGUID",
"AlgorithmIdentifier",
"BodyInit",
"ByteString",
"CryptoOperationData",
"GLbitfield",
"GLboolean",
"GLbyte",
"GLclampf",
"GLenum",
"GLfloat",
"GLint",
"GLintptr",
"GLshort",
"GLsizei",
"GLsizeiptr",
"GLubyte",
"GLuint",
"GLushort",
"HeadersInit",
"IDBKeyPath",
"JSON",
"KeyFormat",
"KeyType",
"KeyUsage",
"RequestInfo",
"USVString",
"payloadtype",
"IDBCursorDirection",
"IDBRequestReadyState",
"IDBTransactionMode",
"MediaKeyStatus",
"NotificationDirection",
"NotificationPermission",
"PushEncryptionKeyName",
"PushPermissionState",
"ReferrerPolicy",
"RequestCache",
"RequestCredentials",
"RequestDestination",
"RequestMode",
"RequestRedirect",
"RequestType",
"ResponseType",
"ServiceWorkerState",
"TextDecodeOptions",
"TextDecoderOptions",
"VisibilityState",
"XMLHttpRequestResponseType",
"ClientTypes",
"FrameType",
"BinaryType",
"ProgressEventInit",
"EventListenerOptions",
"AddEventListenerOptions",
"ErrorEventInit",
"PushSubscriptionChangeEvent",
"PushSubscriptionChangeInit",
"ImageBitmap",
"ImageBitmapOptions",
"FormDataEntryValue",
"EventListenerObject",
"URLSearchParams",
"BlobPropertyBag",
"FilePropertyBag",
"IDBValidKey",
"IDBArrayKey",
"IDBVersionChangeEventInit",
"BufferSource"
"IDBValidKey",
"MessageEventSource",
"PromiseRejectionEventInit"
]
44 changes: 12 additions & 32 deletions src/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Browser from "./types";
import { mapToArray, distinct, map, toNameMap, mapDefined, arrayToMap, flatMap } from "./helpers";
import { mapToArray, distinct, map, toNameMap, mapDefined, arrayToMap, flatMap, integerTypes, baseTypeConversionMap } from "./helpers";

export const enum Flavor {
Web,
Expand All @@ -20,9 +20,6 @@ enum EmitScope {
}

const defaultEventType = "Event";
// Extended types used but not defined in the spec
const extendedTypes = new Set(["ArrayBuffer", "ArrayBufferView", "DataView", "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Uint8ClampedArray", "Int32Array", "Uint32Array", "Float32Array", "Float64Array"]);
const integerTypes = new Set(["byte", "octet", "short", "unsigned short", "long", "unsigned long", "long long", "unsigned long long"]);
const tsKeywords = new Set(["default", "delete", "continue"]);
const extendConflictsBaseTypes: Record<string, { extendType: string[], memberNames: Set<string> }> = {
"AudioContext": { extendType: ["OfflineContext"], memberNames: new Set(["suspend"]) },
Expand Down Expand Up @@ -308,31 +305,14 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
}

function convertDomTypeToTsTypeSimple(objDomType: string): string {
if (baseTypeConversionMap.has(objDomType)) {
return baseTypeConversionMap.get(objDomType)!;
}
switch (objDomType) {
case "DOMHighResTimeStamp": return "number";
case "DOMTimeStamp": return "number";
case "EventListener": return "EventListenerOrEventListenerObject";
case "double":
case "unrestricted double": return "number";
case "float": return "number";
case "object": return "any";
case "ByteString":
case "DOMString":
case "USVString": return "string";
case "sequence": return "Array";
case "record": return "Record";
case "FrozenArray": return "ReadonlyArray";
case "WindowProxy": return "Window";
case "any":
case "boolean":
case "BufferSource":
case "Date":
case "Function":
case "Promise":
case "void": return objDomType;
default:
if (integerTypes.has(objDomType)) return "number";
if (extendedTypes.has(objDomType)) return objDomType;
if (flavor === Flavor.Worker && (objDomType === "Element" || objDomType === "Window" || objDomType === "Document" || objDomType === "AbortSignal" || objDomType === "HTMLFormElement")) return "object";
if (flavor === Flavor.Web && objDomType === "Client") return "object";
// Name of an interface / enum / dict. Just return itself
Expand Down Expand Up @@ -692,10 +672,10 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
if (!i.iterator) {
return;
}
const subtype = i.iterator.subtype.map(convertDomTypeToTsType);
const subtype = i.iterator.type.map(convertDomTypeToTsType);
const value = subtype[subtype.length - 1];
const key = subtype.length > 1 ? subtype[0] :
i.iterator.type === "iterable" ? "number" : value;
i.iterator.kind === "iterable" ? "number" : value;
const name = i.name.replace(/ extends \w+/, "");
printer.printLine(`forEach(callbackfn: (value: ${value}, key: ${key}, parent: ${name}) => void, thisArg?: any): void;`);
}
Expand Down Expand Up @@ -1102,10 +1082,10 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {

function getIteratorSubtypes() {
if (i.iterator) {
if (i.iterator.subtype.length === 1) {
return [convertDomTypeToTsType(i.iterator.subtype[0])];
if (i.iterator.type.length === 1) {
return [convertDomTypeToTsType(i.iterator.type[0])];
}
return i.iterator.subtype.map(convertDomTypeToTsType);
return i.iterator.type.map(convertDomTypeToTsType);
}
else if (i.name !== "Window" && i.properties) {
const iterableGetter = findIterableGetter();
Expand Down Expand Up @@ -1138,8 +1118,8 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
if (!iterator) {
return "";
}
const base = iterator.type === "maplike" ? `Map<${subtypes[0]}, ${subtypes[1]}>` :
iterator.type === "setlike" ? `Set<${subtypes[0]}>` : undefined;
const base = iterator.kind === "maplike" ? `Map<${subtypes[0]}, ${subtypes[1]}>` :
iterator.kind === "setlike" ? `Set<${subtypes[0]}>` : undefined;
if (!base) {
return "";
}
Expand All @@ -1156,7 +1136,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
if (!iteratorExtends) {
printer.printLine(`[Symbol.iterator](): IterableIterator<${stringifySingleOrTupleTypes(subtypes)}>`);
}
if (i.iterator && i.iterator.type === "iterable") {
if (i.iterator && i.iterator.kind === "iterable") {
emitIterableDeclarationMethods(subtypes);
}
printer.decreaseIndent();
Expand Down
Loading

0 comments on commit 8587c6d

Please sign in to comment.