feat: Add webidl2js (#43606)

This commit is contained in:
ExE Boss
2020-04-03 16:54:17 -07:00
committed by GitHub
parent 3e31321ffc
commit 811bdb6362
4 changed files with 165 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
// Type definitions for webidl2js 15.1
// Project: https://github.com/jsdom/webidl2js#readme
// Definitions by: ExE Boss <https://github.com/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<void>;
}
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;
+52
View File
@@ -0,0 +1,52 @@
/// <reference types="node"/>
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);
});
+19
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }