diff --git a/types/webidl2js/index.d.ts b/types/webidl2js/index.d.ts new file mode 100644 index 0000000000..6751ba4896 --- /dev/null +++ b/types/webidl2js/index.d.ts @@ -0,0 +1,93 @@ +// Type definitions for webidl2js 15.1 +// Project: https://github.com/jsdom/webidl2js#readme +// Definitions by: ExE Boss +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { AttributeMemberType } from 'webidl2'; + +declare class Transformer { + constructor(opts?: Transformer.Options); + + /** + * @param idl The WebIDL file or directory of WebIDL files + * to generate wrappers for. + * @param impl The directory containing implementation files + * for the provided WebIDL file(s). + */ + addSource(idl: string, impl: string): this; + + /** + * Generates WebIDL2JS wrapper classes for the supplied WebIDL file(s) + * in the supplied output directory. + * + * @param outputDir The directory where WebIDL2JS wrappers will be generated in. + * @return A promise that will be resolved once all files have been written, + * or rejected if an error was encountered. + */ + generate(outputDir: string): Promise; +} + +declare namespace Transformer { + interface ProcessorContext { + /** + * @param specifier The import specifier. + * @param property The imported property, when undefined or empty, + * then the whole module namespace will be imported. + * @return The identifier referring to this imported value. + */ + addImport(specifier: string, property?: string): string; + } + + type CodeProcessor = (this: ProcessorContext, code: string) => string; + + type AttributeProcessor = ( + this: ProcessorContext, + idl: AttributeMemberType, + implName: string, + ) => { + get: string; + set: string; + }; + + interface Options { + /** + * The optional suffix present on implementation files. + * + * @default "" + */ + implSuffix?: string; + + /** + * The function used to modify attributes and operations + * with the `[CEReactions]` extended attribute. + * + * The default value is the identity function. + */ + processCEReactions?: CodeProcessor; + + /** + * The function used to modify attributes and operations + * with the `[HTMLConstructor]` extended attribute. + * + * The default value is the identity function. + */ + processHTMLConstructor?: CodeProcessor; + + /** + * The function used to generate attributes and operations + * with the `[Reflect]` extended attribute. + * + * @default null + */ + processReflect?: AttributeProcessor | null; + + /** + * Whether non-fatal errors should be ignored. + * + * @default false + */ + suppressErrors?: boolean; + } +} + +export = Transformer; diff --git a/types/webidl2js/test/convert-idl.ts b/types/webidl2js/test/convert-idl.ts new file mode 100644 index 0000000000..a18e76ab7e --- /dev/null +++ b/types/webidl2js/test/convert-idl.ts @@ -0,0 +1,52 @@ +/// + +import path = require('path'); +import Transformer = require('webidl2js'); + +const idlDir = path.resolve(__dirname, '../src'); +const libDir = path.resolve(__dirname, '../lib'); + +const transformer = new Transformer({ + implSuffix: '-impl', + + processCEReactions(code) { + this; // $ExpectType ProcessorContext + this.addImport; // $ExpectType (specifier: string, property?: string | undefined) => string + + code; // $ExpectType string + return code; + }, + + processHTMLConstructor(code) { + this; // $ExpectType ProcessorContext + this.addImport; // $ExpectType (specifier: string, property?: string | undefined) => string + + code; // $ExpectType string + return code; + }, + + processReflect(idl, implName) { + this; // $ExpectType ProcessorContext + this.addImport; // $ExpectType (specifier: string, property?: string | undefined) => string + + idl; // $ExpectType AttributeMemberType + implName; // $ExpectType string + + const reflectAttr = idl.extAttrs.find(attr => attr.name === 'Reflect'); + const attrName: string = + (reflectAttr && reflectAttr.rhs && JSON.parse(reflectAttr.rhs.value as string)) || idl.name.toLowerCase(); + + return { + get: `${implName}.getAttributeNS(null, "${attrName}")`, + set: `${implName}.setAttributeNS(null, "${attrName}", V)`, + }; + }, + + suppressErrors: false, +}); + +transformer.addSource(idlDir, libDir); +transformer.generate(libDir).catch(err => { + console.error(err); + process.exit(1); +}); diff --git a/types/webidl2js/tsconfig.json b/types/webidl2js/tsconfig.json new file mode 100644 index 0000000000..ecf0cc0dc3 --- /dev/null +++ b/types/webidl2js/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["ES2015"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "test/convert-idl.ts", + "index.d.ts" + ] +} diff --git a/types/webidl2js/tslint.json b/types/webidl2js/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/webidl2js/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }