From aba6eefabb8fe0ca3a9ef0005ef25f6dc7e8e8e4 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Tue, 24 Apr 2018 23:29:54 +0900 Subject: [PATCH] add Console types --- baselines/dom.generated.d.ts | 62 ++++++++-------------- baselines/webworker.generated.d.ts | 62 ++++++++-------------- inputfiles/idl/Console.widl | 25 +++++++++ inputfiles/idlSources.json | 4 ++ inputfiles/overridingTypes.json | 85 ++++++------------------------ inputfiles/removedTypes.json | 9 +++- src/emitter.ts | 25 +++++++-- src/helpers.ts | 23 ++++---- src/index.ts | 2 + src/types.d.ts | 4 ++ src/widlprocess.ts | 32 +++++++++++ 11 files changed, 169 insertions(+), 164 deletions(-) create mode 100644 inputfiles/idl/Console.widl diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index 1753a4fd4..c1c113cd0 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -3252,41 +3252,6 @@ interface ConcatParams extends Algorithm { publicInfo?: Uint8Array; } -interface Console { - memory: any; - assert(condition?: boolean, message?: string, ...data: any[]): void; - clear(): void; - count(label?: string): void; - debug(message?: any, ...optionalParams: any[]): void; - dir(value?: any, ...optionalParams: any[]): void; - dirxml(value: any): void; - error(message?: any, ...optionalParams: any[]): void; - exception(message?: string, ...optionalParams: any[]): void; - group(groupTitle?: string, ...optionalParams: any[]): void; - groupCollapsed(groupTitle?: string, ...optionalParams: any[]): void; - groupEnd(): void; - info(message?: any, ...optionalParams: any[]): void; - log(message?: any, ...optionalParams: any[]): void; - markTimeline(label?: string): void; - msIsIndependentlyComposed(element: Element): boolean; - profile(reportName?: string): void; - profileEnd(): void; - select(element: Element): void; - table(...tabularData: any[]): void; - time(label?: string): void; - timeEnd(label?: string): void; - timeStamp(label?: string): void; - timeline(label?: string): void; - timelineEnd(label?: string): void; - trace(message?: any, ...optionalParams: any[]): void; - warn(message?: any, ...optionalParams: any[]): void; -} - -declare var Console: { - prototype: Console; - new(): Console; -}; - interface ConvolverNode extends AudioNode { buffer: AudioBuffer | null; normalize: boolean; @@ -15431,7 +15396,7 @@ interface WindowEventMap extends GlobalEventHandlersEventMap { "waiting": Event; } -interface Window extends EventTarget, WindowTimers, WindowSessionStorage, WindowLocalStorage, WindowConsole, GlobalEventHandlers, IDBEnvironment, WindowBase64, GlobalFetch { +interface Window extends EventTarget, WindowTimers, WindowSessionStorage, WindowLocalStorage, GlobalEventHandlers, IDBEnvironment, WindowBase64, GlobalFetch { Blob: typeof Blob; URL: typeof URL; URLSearchParams: typeof URLSearchParams; @@ -15639,10 +15604,6 @@ interface WindowBase64 { btoa(rawString: string): string; } -interface WindowConsole { - readonly console: Console; -} - interface WindowEventHandlersEventMap { "afterprint": Event; "beforeprint": Event; @@ -15957,6 +15918,26 @@ declare var webkitRTCPeerConnection: { new(configuration: RTCConfiguration): webkitRTCPeerConnection; }; +declare namespace console { + function assert(condition?: boolean, ...data: any[]): void; + function clear(): void; + function count(label?: string): void; + function debug(...data: any[]): void; + function dir(item: any, options?: any): void; + function dirxml(...data: any[]): void; + function error(...data: any[]): void; + function group(...data: any[]): void; + function groupCollapsed(...data: any[]): void; + function groupEnd(): void; + function info(...data: any[]): void; + function log(...data: any[]): void; + function table(tabularData: any, properties?: string[]): void; + function time(label?: string): void; + function timeEnd(label?: string): void; + function trace(...data: any[]): void; + function warn(...data: any[]): void; +} + declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject; interface DecodeErrorCallback { @@ -16464,7 +16445,6 @@ declare function setImmediate(handler: (...args: any[]) => void): number; declare function setImmediate(handler: any, ...args: any[]): number; declare var sessionStorage: Storage; declare var localStorage: Storage; -declare var console: Console; declare var onpointercancel: ((this: Window, ev: PointerEvent) => any) | null; declare var onpointerdown: ((this: Window, ev: PointerEvent) => any) | null; declare var onpointerenter: ((this: Window, ev: PointerEvent) => any) | null; diff --git a/baselines/webworker.generated.d.ts b/baselines/webworker.generated.d.ts index df461f303..a5f02291f 100644 --- a/baselines/webworker.generated.d.ts +++ b/baselines/webworker.generated.d.ts @@ -408,41 +408,6 @@ interface ConcatParams extends Algorithm { publicInfo?: Uint8Array; } -interface Console { - memory: any; - assert(condition?: boolean, message?: string, ...data: any[]): void; - clear(): void; - count(label?: string): void; - debug(message?: any, ...optionalParams: any[]): void; - dir(value?: any, ...optionalParams: any[]): void; - dirxml(value: any): void; - error(message?: any, ...optionalParams: any[]): void; - exception(message?: string, ...optionalParams: any[]): void; - group(groupTitle?: string, ...optionalParams: any[]): void; - groupCollapsed(groupTitle?: string, ...optionalParams: any[]): void; - groupEnd(): void; - info(message?: any, ...optionalParams: any[]): void; - log(message?: any, ...optionalParams: any[]): void; - markTimeline(label?: string): void; - msIsIndependentlyComposed(element: object): boolean; - profile(reportName?: string): void; - profileEnd(): void; - select(element: object): void; - table(...tabularData: any[]): void; - time(label?: string): void; - timeEnd(label?: string): void; - timeStamp(label?: string): void; - timeline(label?: string): void; - timelineEnd(label?: string): void; - trace(message?: any, ...optionalParams: any[]): void; - warn(message?: any, ...optionalParams: any[]): void; -} - -declare var Console: { - prototype: Console; - new(): Console; -}; - interface CryptoKey { readonly algorithm: KeyAlgorithm; readonly extractable: boolean; @@ -1874,10 +1839,6 @@ declare var WindowClient: { new(): WindowClient; }; -interface WindowConsole { - readonly console: Console; -} - interface WorkerEventMap extends AbstractWorkerEventMap { "message": MessageEvent; } @@ -1902,7 +1863,7 @@ interface WorkerGlobalScopeEventMap { "error": ErrorEvent; } -interface WorkerGlobalScope extends EventTarget, WorkerUtils, WindowConsole, GlobalFetch { +interface WorkerGlobalScope extends EventTarget, WorkerUtils, GlobalFetch { readonly caches: CacheStorage; readonly isSecureContext: boolean; readonly location: WorkerLocation; @@ -2049,6 +2010,26 @@ declare var XMLHttpRequestUpload: { new(): XMLHttpRequestUpload; }; +declare namespace console { + function assert(condition?: boolean, ...data: any[]): void; + function clear(): void; + function count(label?: string): void; + function debug(...data: any[]): void; + function dir(item: any, options?: any): void; + function dirxml(...data: any[]): void; + function error(...data: any[]): void; + function group(...data: any[]): void; + function groupCollapsed(...data: any[]): void; + function groupEnd(): void; + function info(...data: any[]): void; + function log(...data: any[]): void; + function table(tabularData: any, properties?: string[]): void; + function time(label?: string): void; + function timeEnd(label?: string): void; + function trace(...data: any[]): void; + function warn(...data: any[]): void; +} + declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject; interface EventHandlerNonNull { @@ -2085,7 +2066,6 @@ declare function setInterval(handler: any, timeout?: any, ...args: any[]): numbe declare function setTimeout(handler: any, timeout?: any, ...args: any[]): number; declare function atob(encodedString: string): string; declare function btoa(rawString: string): string; -declare var console: Console; declare function fetch(input?: Request | string, init?: RequestInit): Promise; declare function addEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; diff --git a/inputfiles/idl/Console.widl b/inputfiles/idl/Console.widl new file mode 100644 index 000000000..7351bfcfd --- /dev/null +++ b/inputfiles/idl/Console.widl @@ -0,0 +1,25 @@ +[Exposed=(Window,Worker,Worklet)] +namespace console { // but see namespace object requirements below + // Logging + void assert(optional boolean condition = false, any... data); + void clear(); + void count(optional DOMString label = "default"); + void debug(any... data); + void error(any... data); + void info(any... data); + void log(any... data); + void table(any tabularData, optional sequence properties); + void trace(any... data); + void warn(any... data); + void dir(any item, optional object? options); + void dirxml(any... data); + + // Grouping + void group(any... data); + void groupCollapsed(any... data); + void groupEnd(); + + // Timing + void time(optional DOMString label = "default"); + void timeEnd(optional DOMString label = "default"); +}; diff --git a/inputfiles/idlSources.json b/inputfiles/idlSources.json index 93f9f2144..a420f4a24 100644 --- a/inputfiles/idlSources.json +++ b/inputfiles/idlSources.json @@ -1,4 +1,8 @@ [ + { + "url": "https://console.spec.whatwg.org/", + "title": "Console" + }, { "url": "https://encoding.spec.whatwg.org/", "title": "Encoding" diff --git a/inputfiles/overridingTypes.json b/inputfiles/overridingTypes.json index b2a46235a..19557b688 100644 --- a/inputfiles/overridingTypes.json +++ b/inputfiles/overridingTypes.json @@ -248,7 +248,23 @@ } } }, - "overide-index-signatures": [] + "overide-index-signatures": [], + "override-implements": [ + "WindowTimers", + "WindowSessionStorage", + "WindowLocalStorage", + "GlobalEventHandlers", + "IDBEnvironment", + "WindowBase64", + "GlobalFetch" + ] + }, + "WorkerGlobalScope": { + "name": "WorkerGlobalScope", + "override-implements": [ + "WorkerUtils", + "GlobalFetch" + ] }, "Document": { "name": "Document", @@ -878,73 +894,6 @@ } } }, - "Console": { - "name": "Console", - "methods": { - "method": { - "debug": { - "name": "debug", - "override-signatures": [ - "debug(message?: any, ...optionalParams: any[]): void" - ] - }, - "dir": { - "name": "dir", - "override-signatures": [ - "dir(value?: any, ...optionalParams: any[]): void" - ] - }, - "dirxml": { - "name": "dirxml", - "override-signatures": [ - "dirxml(value: any): void" - ] - }, - "error": { - "name": "error", - "override-signatures": [ - "error(message?: any, ...optionalParams: any[]): void" - ] - }, - "info": { - "name": "info", - "override-signatures": [ - "info(message?: any, ...optionalParams: any[]): void" - ] - }, - "log": { - "name": "log", - "override-signatures": [ - "log(message?: any, ...optionalParams: any[]): void" - ] - }, - "warn": { - "name": "warn", - "override-signatures": [ - "warn(message?: any, ...optionalParams: any[]): void" - ] - }, - "group": { - "name": "group", - "override-signatures": [ - "group(groupTitle?: string, ...optionalParams: any[]): void" - ] - }, - "groupCollapsed": { - "name": "groupCollapsed", - "override-signatures": [ - "groupCollapsed(groupTitle?: string, ...optionalParams: any[]): void" - ] - }, - "trace": { - "name": "trace", - "override-signatures": [ - "trace(message?: any, ...optionalParams: any[]): void" - ] - } - } - } - }, "FormData": { "name": "FormData", "methods": { diff --git a/inputfiles/removedTypes.json b/inputfiles/removedTypes.json index 060cc069d..9329bfc3f 100644 --- a/inputfiles/removedTypes.json +++ b/inputfiles/removedTypes.json @@ -6,7 +6,8 @@ }, "mixins": { "mixin": { - "XMLHttpRequestEventTarget": null + "XMLHttpRequestEventTarget": null, + "WindowConsole": null } }, "callback-interfaces": { @@ -20,6 +21,7 @@ }, "interfaces": { "interface": { + "Console": null, "FileReaderProgressEvent": null, "HTMLElement": { "methods": { @@ -51,6 +53,11 @@ } } }, + "namespaces": { + "namespace": { + + } + }, "dictionaries": { "dictionary": { "ObjectURLOptions": null, diff --git a/src/emitter.ts b/src/emitter.ts index adb902c18..81208246f 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -220,7 +220,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) { } const extendedParentWithEventHandler = allInterfacesMap[i.extends] && getParentEventHandler(allInterfacesMap[i.extends]) || []; - const implementedParentsWithEventHandler = i.implements ? flatMap(i.implements, i => getParentEventHandler(allInterfacesMap[i])) : []; + const implementedParentsWithEventHandler = flatMap(i["override-implements"] || i.implements, i => getParentEventHandler(allInterfacesMap[i])); return extendedParentWithEventHandler.concat(implementedParentsWithEventHandler); } @@ -684,7 +684,10 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) { function emitMembers(prefix: string, emitScope: EmitScope, i: Browser.Interface) { const conflictedMembers = extendConflictsBaseTypes[i.name] ? extendConflictsBaseTypes[i.name].memberNames : new Set(); emitProperties(prefix, emitScope, i, conflictedMembers); - const methodPrefix = prefix.startsWith("declare var") ? "declare function " : ""; + const methodPrefix = + prefix.startsWith("declare var") ? "declare function " : + prefix.startsWith("const") ? "function " : + ""; emitMethods(methodPrefix, emitScope, i, conflictedMembers); if (emitScope === EmitScope.InstanceOnly) { emitIteratorForEach(i); @@ -802,7 +805,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) { printer.printLine(`interface ${processInterfaceType(i, processedIName)}`); - const finalExtends = distinct([i.extends || "Object"].concat(i.implements || []) + const finalExtends = distinct([i.extends || "Object"].concat(i["override-implements"] || i.implements || []) .filter(i => i !== "Object") .map(processIName)); @@ -1003,6 +1006,21 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) { .forEach(emitDictionary); } + function emitNamespace(namespace: Browser.Interface) { + printer.printLine(`declare namespace ${namespace.name} {`); + printer.increaseIndent(); + emitMembers("const", EmitScope.All, namespace); + printer.decreaseIndent(); + printer.printLine("}"); + printer.printLine(""); + } + + function emitNamespaces() { + getElements(webidl.namespaces, "namespace") + .sort(compareName) + .forEach(emitNamespace); + } + function emitTypeDef(typeDef: Browser.TypeDef) { printer.printLine(`type ${typeDef["new-type"]} = ${convertDomTypeToTsType(typeDef)};`); } @@ -1035,6 +1053,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) { .sort(compareName) .forEach(i => emitCallBackInterface(i)); emitNonCallbackInterfaces(); + emitNamespaces(); // // Add missed interface definition from the spec // InputJson.getAddedItems InputJson.Interface flavor |> Array.iter EmitAddedInterface diff --git a/src/helpers.ts b/src/helpers.ts index ba33218cd..f4c9e4733 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -183,20 +183,23 @@ export function getEmptyWebIDL(): Browser.WebIdl { "callback-interfaces": { "interface": {} }, - "dictionaries": { - "dictionary": {} + dictionaries: { + dictionary: {} }, - "enums": { - "enum": {} + enums: { + enum: {} }, - "interfaces": { - "interface": {} + interfaces: { + interface: {} + }, + mixins: { + mixin: {} }, - "mixins": { - "mixin": {} + namespaces: { + namespace: {} }, - "typedefs": { - "typedef": [] + typedefs: { + typedef: [] } } } diff --git a/src/index.ts b/src/index.ts index ed9859c28..312764f34 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ function emitDomWorker(webidl: Browser.WebIdl, forceKnownWorkerTypes: Set exposesTo(o, "Worker")); if (webidl.typedefs) worker.typedefs!.typedef = webidl.typedefs.typedef.filter(t => knownWorkerTypes.has(t["new-type"])); const result = emitWebIDl(worker, Flavor.Worker); @@ -120,6 +121,7 @@ function emitDom() { if (obj.enums) result.enums!.enum = filterByNull(obj.enums.enum, template.enums!.enum); if (obj.mixins) result.mixins!.mixin = filterByNull(obj.mixins.mixin, template.mixins!.mixin); if (obj.interfaces) result.interfaces!.interface = filterByNull(obj.interfaces.interface, template.interfaces!.interface); + if (obj.namespaces) result.namespaces!.namespace = filterByNull(obj.namespaces.namespace, template.namespaces!.namespace); if (obj.typedefs) result.typedefs!.typedef = obj.typedefs.typedef.filter(t => !(template.typedefs && template.typedefs.typedef.find(o => o["new-type"] === t["new-type"]))); return result; diff --git a/src/types.d.ts b/src/types.d.ts index 7519e3e99..ad5371a22 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -152,6 +152,7 @@ export interface Interface { constructor?: Constructor; "secure-context"?: 1; implements?: string[]; + "override-implements"?: string[]; static?: 1; "anonymous-methods"?: { method: Method[]; @@ -222,6 +223,9 @@ export interface WebIdl { mixins?: { mixin: Record; } + namespaces?: { + namespace: Record; + } typedefs?: { typedef: TypeDef[]; } diff --git a/src/widlprocess.ts b/src/widlprocess.ts index cb51a4759..2184f4e2a 100644 --- a/src/widlprocess.ts +++ b/src/widlprocess.ts @@ -28,6 +28,9 @@ export function convert(text: string) { browser["callback-functions"]!["callback-function"][rootType.name] = convertCallbackFunctions(rootType); } + else if (rootType.type === "namespace") { + browser.namespaces!.namespace[rootType.name] = convertNamespace(rootType); + } else if (rootType.type === "dictionary") { const converted = convertDictionary(rootType); if (rootType.partial) { @@ -219,6 +222,35 @@ function convertConstantValue(value: webidl2.ValueDescription): string { } } +function convertNamespace(namespace: webidl2.NamespaceType) { + const result: Browser.Interface = { + name: namespace.name, + extends: "Object", + constructor: { signature: [] }, + exposed: getExtAttrConcatenated(namespace.extAttrs, "Exposed"), + methods: { method: {} }, + properties: { property: {} } + } + + for (const member of namespace.members) { + if (member.type === "attribute") { + result.properties!.property[member.name] = convertAttribute(member, result.exposed); + } + else if (member.type === "operation" && member.name) { + const operation = convertOperation(member, result.exposed); + const { method } = result.methods; + if (method[member.name]) { + method[member.name].signature.push(...operation.signature); + } + else { + method[member.name] = operation; + } + } + } + + return result; +} + function convertDictionary(dictionary: webidl2.DictionaryType) { const result: Browser.Dictionary = { name: dictionary.name,