Best JavaScript code snippet using wpt
webidl2ts.js
Source:webidl2ts.js
1// adapted from https://github.com/markandrus/webidl2.js/blob/3d489b89f191f6ea7bda4388419c51b3d82bd169/scripts/typescript.js2import { parse } from "webidl2";3/**4 * @param {string} filepath5 * @returns {string}6 * @throws {Error}7 */8export function webidl2ts(unparsed /* : string */) /* : void */ {9 const idlDefinitions = parse(unparsed);10 const typescript = `${printIDLDefinitions(idlDefinitions)}`;11 return typescript;12}13/**14 * @param {IDLArgument} arg15 * @returns {string}16 */17function printIDLArgument(arg /* : IDLArgument */) /* : string */ {18 return `${arg.name}${arg.optional ? "?" : ""}: ${printIDLType(arg.idlType)}`;19}20/**21 * @param {Array<IDLArgument>} args22 * @returns {string}23 */24function printIDLArguments(args /* : Array<IDLArgument> */) /* : string */ {25 return args.map(printIDLArgument).join(", ");26}27/**28 * @param {IDLAttributeMember} idlAttributeMember29 * @returns {string}30 */31function printIDLAttributeMember(32 idlAttributeMember /* : IDLAttributeMember */33) /* : string */ {34 return ` ${idlAttributeMember.name}: ${printIDLType(35 idlAttributeMember.idlType36 )};`;37}38function printIDLOperationMember(idlMember) {39 let prefix =40 idlMember.special && idlMember.special.value41 ? { getter: "get", setter: "set" }[idlMember.special.value] || ""42 : "";43 return ` ${prefix} ${44 idlMember.body.name ? idlMember.body.name.value : ""45 }(${printIDLArguments(idlMember.body.arguments)}): ${printIDLType(46 idlMember.body.idlType47 )}`;48}49/**50 * @param {IDLDefinition} idlDefinition51 * @returns {?string}52 * @throws {Error}53 */54function printIDLDefinition(55 idlDefinition /* : IDLDefinition */56) /* : ?string */ {57 switch (idlDefinition.type) {58 case "dictionary":59 return printIDLDictionary(idlDefinition);60 case "enum":61 return printIDLEnum(idlDefinition);62 case "interface":63 return printIDLInterface(idlDefinition);64 case "typedef":65 // NOTE(mroberts): WebIDL cannot represent a type which is an empty Array,66 // nor can it represent "pairs" (e.g., an Array of length two); so we67 // special case these here.68 if (69 idlDefinition.name === "EmptyArray" ||70 idlDefinition.name === "PairOfIDLTypes"71 ) {72 return null;73 }74 return printIDLTypedef(idlDefinition);75 case "eof":76 return "";77 default:78 throw new Error(`I don't know how to print ${idlDefinition.type}s`);79 }80}81/**82 * @param {Array<IDLDefinition>} idlDefinitions83 * @returns {string}84 * @throws {Error}85 */86function printIDLDefinitions(87 idlDefinitions /* : Array<IDLDefinition> */88) /* : string */ {89 const typeScriptDefinitions = [];90 for (let idlDefinition of idlDefinitions) {91 const typeScriptDefinition = printIDLDefinition(idlDefinition);92 if (typeScriptDefinition) {93 typeScriptDefinitions.push(typeScriptDefinition);94 }95 }96 return typeScriptDefinitions.join("\n");97}98/**99 * @param {IDLDictionary} idlDictionary100 * @returns {string}101 */102function printIDLDictionary(103 idlDictionary /* : IDLDictionary */104) /* : string */ {105 const n = idlDictionary.members.length;106 return `export type ${idlDictionary.name} = {107${idlDictionary.members108 .map((member, i) => {109 return ` ${member.name}${member.required ? "" : "?"}: ${printIDLType(110 member.idlType111 )}${i === n - 1 ? "" : ","}`;112 })113 .join("\n")}114};115`;116}117/**118 * @param {IDLEnum} idlEnum119 * @returns {string}120 */121function printIDLEnum(idlEnum /* : IDLEnum */) /* : string */ {122 const n = idlEnum.values.length;123 return `export type ${idlEnum.name}124${idlEnum.values125 .map((value, i) => {126 return ` ${i ? "|" : "="} ${JSON.stringify(value)}${127 i === n - 1 ? ";" : ""128 }`;129 })130 .join("\n")}131`;132}133/**134 * @param {IDLInterface} idlInterface135 * @returns {string}136 * @throws {Error}137 */138function printIDLInterface(idlInterface /* : IDLInterface */) /* : string */ {139 const constructor = idlInterface.extAttrs.items.find(extAttr => {140 return extAttr.name === "Constructor";141 });142 let out = `export ${constructor ? "class" : "interface"} ${143 idlInterface.name144 }${145 idlInterface.inheritance ? ` extends ${idlInterface.inheritance}` : ""146 } {\n`;147 if (constructor) {148 out += ` constructor(${printIDLArguments(149 constructor.arguments || []150 )});\n`;151 }152 if (idlInterface.members.length) {153 out += printIDLMembers(idlInterface.members);154 }155 return out + "\n}\n";156}157/**158 * @param {IDLMember} idlMember159 * @returns {string}160 * @throws {Error}161 */162function printIDLMember(idlMember /* : IDLMember */) /* : string */ {163 switch (idlMember.type) {164 case "attribute":165 return printIDLAttributeMember(idlMember);166 case "operation":167 return printIDLOperationMember(idlMember);168 default:169 throw new Error(`I don't know how to print member type: ${idlMember}`);170 }171}172/**173 * @param {IDLMember} idlMembers174 * @returns {string}175 * @throws {Error}176 */177function printIDLMembers(idlMembers /* : Array<IDLMember> */) /* : string */ {178 return idlMembers.map(printIDLMember).join("\n");179}180/**181 * @param {IDLType} idlType182 * @returns {string}183 */184function printIDLType(idlType /* : IDLType */) /* : string */ {185 let before = "";186 let after = "";187 if (idlType.generic) {188 before = `${idlType.generic.value}<` + before;189 after += ">";190 }191 if (idlType.nullable) {192 after += "|null";193 }194 if (typeof idlType.idlType === "string") {195 let type = nativeTypes[idlType.idlType] || idlType.idlType;196 return `${before}${type}${after}`;197 } else if (Array.isArray(idlType.idlType)) {198 return `${before}${idlType.idlType.map(printIDLType).join("|")}${after}`;199 }200 return `${before}${printIDLType(idlType.idlType)}${after}`;201}202const nativeTypes = {203 // NOTE(mroberts): WebIDL cannot represent a type which is an empty Array,204 // nor can it represent "pairs" (e.g., an Array of length two); so we205 // special case these here.206 EmptyArray: "[]",207 PairOfIDLTypes: "[IDLType, IDLType]",208 sequence: "Array"209};210/**211 * @param {IDLTypedef} idlTypedef212 * @returns {string}213 * @throws {Error}214 */215function printIDLTypedef(idlTypedef /* : IDLTypedef */) /* : string */ {216 if (Array.isArray(idlTypedef.idlType.idlType)) {217 const n = idlTypedef.idlType.idlType.length;218 return `export type ${idlTypedef.name}219${idlTypedef.idlType.idlType220 .map((idlType, i) => {221 return ` ${i ? "|" : "="} ${printIDLType(idlType)}${222 i === n - 1 ? ";" : ""223 }`;224 })225 .join("\n")}226`;227 } else if (typeof idlTypedef.idlType.idlType === "string") {228 return `export type ${idlTypedef.name} = ${idlTypedef.idlType.idlType}229`;230 }231 throw new Error(`I only know how to print typedefs for unions`);...
dictionary_types.ts
Source:dictionary_types.ts
1/**2 * Copyright (c) 2017 The Bacardi Authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16import IDLDefinition from './idl_definition';17import IDLDictionary from './idl_dictionary';18export default class DictionaryTypes {19 readonly dictionaries: IDLDictionary[];20 constructor(definitions: IDLDefinition[]) {21 this.dictionaries = [];22 definitions.forEach((definition) => {23 if (definition.isIDLDictionary()) {24 this.dictionaries.push(definition as IDLDictionary);25 }26 });27 }28 public isDictionaryType(source: string): IDLDictionary {29 for (const item of this.dictionaries) {30 if (item.name == source) {31 return item;32 }33 }34 return null;35 }...
Using AI Code Generation
1function run_test() {2 var server = new HttpServer();3 server.registerDirectory("/data/", do_get_file("data"));4 server.registerDirectory("/common/", do_get_file("common"));5 server.start(-1);6 var channel = setupChannel("/data/test_idl_dictionary.html");7 channel.asyncOpen(new ChannelListener(finish_test, channel, CL_EXPECT_LATE_FAILURE), null);8 do_test_pending();9}10function finish_test(request, buffer) {11 Assert.equal(request.status, Cr.NS_ERROR_DOM_SYNTAX_ERR);12 do_test_finished();13}
Using AI Code Generation
1var idl_dict = new IdlDictionary();2idl_dict.set("key", "value");3var idl_dict2 = new IdlDictionary();4idl_dict2.set("key2", "value2");5idl_dict.set("key3", idl_dict2);6idl_dict.set("key4", "value4");7idl_dict.set("key5", [1,2,3]);8var idl_array = new IdlArray();9idl_array.add_objects({10});11idl_array.add_untested_idls("interface Unimplemented {};");12var idl_interface = new IdlInterface("interface Implemented {};");13idl_array.add_objects({14});15idl_array.add_untested_idls("interface Unimplemented {};");16var idl_object = new IdlObject();17idl_object.add_objects({18});19idl_object.add_untested_idls("interface Unimplemented {};");20var idl_object2 = new IdlObject();21idl_object2.add_objects({22});23idl_object2.add_untested_idls("interface Unimplemented {};");24var idl_object3 = new IdlObject();25idl_object3.add_objects({26});27idl_object3.add_untested_idls("interface Unimplemented {};");28var idl_object4 = new IdlObject();29idl_object4.add_objects({30});31idl_object4.add_untested_idls("interface Unimplemented {};");32var idl_object5 = new IdlObject();
Using AI Code Generation
1function run_test() {2 var httpserver = new HttpServer();3 httpserver.registerPathHandler("/test", testHandler);4 httpserver.start(-1);5 var serverPort = httpserver.identity.primaryPort;6 var channel = makeChan(testUrl);7 channel.asyncOpen(new ChannelListener(testComplete, null, CL_EXPECT_FAILURE), null);8 do_test_pending();9}10function testHandler(metadata, response)11{12 response.setHeader("Content-Type", "application/json", false);13 response.write("{'key1': 'value1', 'key2': 'value2'}");14}15function testComplete(request, data, ctx)16{17 do_check_eq(request.status, Components.results.NS_ERROR_FAILURE);18 do_test_finished();19}
Using AI Code Generation
1var test = new IdlDictionary("Test");2test.set("foo", "bar");3test.set("baz", "qux");4test.set("quux", "corge");5test.set("grault", "garply");6test.set("waldo", "fred");7test.set("plugh", "xyzzy");8test.set("thud", "thud");9var test2 = new IdlArray();10test2.add_objects({11});12var test3 = new IdlArray();13test3.add_objects({14});15test3.add_untested_idls("interface Test2 {};");16var test4 = new IdlArray();17test4.add_objects({18});19test4.add_untested_idls("interface Test2 {};");20test4.add_objects({21});22var test5 = new IdlArray();23test5.add_objects({24});25test5.add_untested_idls("interface Test2 {};");26test5.add_objects({27});28test5.add_untested_idls("interface Test3 {};");29test5.add_objects({30});31var test6 = new IdlArray();32test6.add_objects({33});34test6.add_untested_idls("interface Test2 {};");35test6.add_objects({36});37test6.add_untested_idls("interface Test3 {};");38test6.add_objects({39});40test6.add_untested_idls("interface Test4 {};");41test6.add_objects({42});43var test7 = new IdlArray();44test7.add_objects({45});
Using AI Code Generation
1var wptserve = require('wptserve');2var IdlDictionary = wptserve.IdlDictionary;3var response = IdlDictionary.create(4 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });5var response = IdlDictionary.create(6 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });7var response = IdlDictionary.create(8 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });9var response = IdlDictionary.create(10 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });11var response = IdlDictionary.create(12 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });13var response = IdlDictionary.create(14 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });15var response = IdlDictionary.create(16 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });17var response = IdlDictionary.create(18 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });19var response = IdlDictionary.create(20 "Response", { "status": 200, "statusText": "OK", "headers": { "Content-Type": "text/plain" } });
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!