Best JavaScript code snippet using wpt
type.js
Source:type.js
1import { Base } from "./base.js";2import {3 unescape,4 type_with_extended_attributes,5 return_type,6 primitive_type,7 autoParenter,8} from "./helpers.js";9import { stringTypes, typeNameKeywords } from "../tokeniser.js";10import { validationError } from "../error.js";11import { idlTypeIncludesDictionary } from "../validators/helpers.js";12import { ExtendedAttributes } from "./extended-attributes.js";13/**14 * @param {import("../tokeniser.js").Tokeniser} tokeniser15 * @param {string} typeName16 */17function generic_type(tokeniser, typeName) {18 const base = tokeniser.consume(19 "FrozenArray",20 "ObservableArray",21 "Promise",22 "sequence",23 "record"24 );25 if (!base) {26 return;27 }28 const ret = autoParenter(29 new Type({ source: tokeniser.source, tokens: { base } })30 );31 ret.tokens.open =32 tokeniser.consume("<") ||33 tokeniser.error(`No opening bracket after ${base.value}`);34 switch (base.value) {35 case "Promise": {36 if (tokeniser.probe("["))37 tokeniser.error("Promise type cannot have extended attribute");38 const subtype =39 return_type(tokeniser, typeName) ||40 tokeniser.error("Missing Promise subtype");41 ret.subtype.push(subtype);42 break;43 }44 case "sequence":45 case "FrozenArray":46 case "ObservableArray": {47 const subtype =48 type_with_extended_attributes(tokeniser, typeName) ||49 tokeniser.error(`Missing ${base.value} subtype`);50 ret.subtype.push(subtype);51 break;52 }53 case "record": {54 if (tokeniser.probe("["))55 tokeniser.error("Record key cannot have extended attribute");56 const keyType =57 tokeniser.consume(...stringTypes) ||58 tokeniser.error(`Record key must be one of: ${stringTypes.join(", ")}`);59 const keyIdlType = new Type({60 source: tokeniser.source,61 tokens: { base: keyType },62 });63 keyIdlType.tokens.separator =64 tokeniser.consume(",") ||65 tokeniser.error("Missing comma after record key type");66 keyIdlType.type = typeName;67 const valueType =68 type_with_extended_attributes(tokeniser, typeName) ||69 tokeniser.error("Error parsing generic type record");70 ret.subtype.push(keyIdlType, valueType);71 break;72 }73 }74 if (!ret.idlType) tokeniser.error(`Error parsing generic type ${base.value}`);75 ret.tokens.close =76 tokeniser.consume(">") ||77 tokeniser.error(`Missing closing bracket after ${base.value}`);78 return ret.this;79}80/**81 * @param {import("../tokeniser.js").Tokeniser} tokeniser82 */83function type_suffix(tokeniser, obj) {84 const nullable = tokeniser.consume("?");85 if (nullable) {86 obj.tokens.nullable = nullable;87 }88 if (tokeniser.probe("?")) tokeniser.error("Can't nullable more than once");89}90/**91 * @param {import("../tokeniser.js").Tokeniser} tokeniser92 * @param {string} typeName93 */94function single_type(tokeniser, typeName) {95 let ret = generic_type(tokeniser, typeName) || primitive_type(tokeniser);96 if (!ret) {97 const base =98 tokeniser.consumeKind("identifier") ||99 tokeniser.consume(...stringTypes, ...typeNameKeywords);100 if (!base) {101 return;102 }103 ret = new Type({ source: tokeniser.source, tokens: { base } });104 if (tokeniser.probe("<"))105 tokeniser.error(`Unsupported generic type ${base.value}`);106 }107 if (ret.generic === "Promise" && tokeniser.probe("?")) {108 tokeniser.error("Promise type cannot be nullable");109 }110 ret.type = typeName || null;111 type_suffix(tokeniser, ret);112 if (ret.nullable && ret.idlType === "any")113 tokeniser.error("Type `any` cannot be made nullable");114 return ret;115}116/**117 * @param {import("../tokeniser.js").Tokeniser} tokeniser118 * @param {string} type119 */120function union_type(tokeniser, type) {121 const tokens = {};122 tokens.open = tokeniser.consume("(");123 if (!tokens.open) return;124 const ret = autoParenter(new Type({ source: tokeniser.source, tokens }));125 ret.type = type || null;126 while (true) {127 const typ =128 type_with_extended_attributes(tokeniser) ||129 tokeniser.error("No type after open parenthesis or 'or' in union type");130 if (typ.idlType === "any")131 tokeniser.error("Type `any` cannot be included in a union type");132 if (typ.generic === "Promise")133 tokeniser.error("Type `Promise` cannot be included in a union type");134 ret.subtype.push(typ);135 const or = tokeniser.consume("or");136 if (or) {137 typ.tokens.separator = or;138 } else break;139 }140 if (ret.idlType.length < 2) {141 tokeniser.error(142 "At least two types are expected in a union type but found less"143 );144 }145 tokens.close =146 tokeniser.consume(")") || tokeniser.error("Unterminated union type");147 type_suffix(tokeniser, ret);148 return ret.this;149}150export class Type extends Base {151 /**152 * @param {import("../tokeniser.js").Tokeniser} tokeniser153 * @param {string} typeName154 */155 static parse(tokeniser, typeName) {156 return single_type(tokeniser, typeName) || union_type(tokeniser, typeName);157 }158 constructor({ source, tokens }) {159 super({ source, tokens });160 Object.defineProperty(this, "subtype", { value: [], writable: true });161 this.extAttrs = new ExtendedAttributes({ source, tokens: {} });162 }163 get generic() {164 if (this.subtype.length && this.tokens.base) {165 return this.tokens.base.value;166 }167 return "";168 }169 get nullable() {170 return Boolean(this.tokens.nullable);171 }172 get union() {173 return Boolean(this.subtype.length) && !this.tokens.base;174 }175 get idlType() {176 if (this.subtype.length) {177 return this.subtype;178 }179 // Adding prefixes/postfixes for "unrestricted float", etc.180 const name = [this.tokens.prefix, this.tokens.base, this.tokens.postfix]181 .filter((t) => t)182 .map((t) => t.value)183 .join(" ");184 return unescape(name);185 }186 *validate(defs) {187 yield* this.extAttrs.validate(defs);188 if (this.idlType === "void") {189 const message = `\`void\` is now replaced by \`undefined\`. Refer to the \190[relevant GitHub issue](https://github.com/whatwg/webidl/issues/60) \191for more information.`;192 yield validationError(this.tokens.base, this, "replace-void", message, {193 autofix: replaceVoid(this),194 });195 }196 /*197 * If a union is nullable, its subunions cannot include a dictionary198 * If not, subunions may include dictionaries if each union is not nullable199 */200 const typedef = !this.union && defs.unique.get(this.idlType);201 const target = this.union202 ? this203 : typedef && typedef.type === "typedef"204 ? typedef.idlType205 : undefined;206 if (target && this.nullable) {207 // do not allow any dictionary208 const { reference } = idlTypeIncludesDictionary(target, defs) || {};209 if (reference) {210 const targetToken = (this.union ? reference : this).tokens.base;211 const message = "Nullable union cannot include a dictionary type.";212 yield validationError(213 targetToken,214 this,215 "no-nullable-union-dict",216 message217 );218 }219 } else {220 // allow some dictionary221 for (const subtype of this.subtype) {222 yield* subtype.validate(defs);223 }224 }225 }226 /** @param {import("../writer.js").Writer} w */227 write(w) {228 const type_body = () => {229 if (this.union || this.generic) {230 return w.ts.wrap([231 w.token(this.tokens.base, w.ts.generic),232 w.token(this.tokens.open),233 ...this.subtype.map((t) => t.write(w)),234 w.token(this.tokens.close),235 ]);236 }237 const firstToken = this.tokens.prefix || this.tokens.base;238 const prefix = this.tokens.prefix239 ? [this.tokens.prefix.value, w.ts.trivia(this.tokens.base.trivia)]240 : [];241 const ref = w.reference(242 w.ts.wrap([243 ...prefix,244 this.tokens.base.value,245 w.token(this.tokens.postfix),246 ]),247 {248 unescaped: /** @type {string} (because it's not union) */ (249 this.idlType250 ),251 context: this,252 }253 );254 return w.ts.wrap([w.ts.trivia(firstToken.trivia), ref]);255 };256 return w.ts.wrap([257 this.extAttrs.write(w),258 type_body(),259 w.token(this.tokens.nullable),260 w.token(this.tokens.separator),261 ]);262 }263}264/**265 * @param {Type} type266 */267function replaceVoid(type) {268 return () => {269 type.tokens.base.value = "undefined";270 };...
Using AI Code Generation
1var test = wpt.keyIdlType();2test.then(function (result) {3 console.log("test: " + result);4});5var test = wpt.keyIdlType();6test.then(function (result) {7 console.log("test: " + result);8});9var test = wpt.keyIdlType();10test.then(function (result) {11 console.log("test: " + result);12});13var test = wpt.keyIdlType();14test.then(function (result) {15 console.log("test: " + result);16});17var test = wpt.keyIdlType();18test.then(function (result) {19 console.log("test: " + result);20});21var test = wpt.keyIdlType();22test.then(function (result) {23 console.log("test: " + result);24});25var test = wpt.keyIdlType();26test.then(function (result) {27 console.log("test: " + result);28});29var test = wpt.keyIdlType();30test.then(function (result) {31 console.log("test: " + result);32});33var test = wpt.keyIdlType();34test.then(function (result) {35 console.log("test: " + result);36});37var test = wpt.keyIdlType();38test.then(function (result) {39 console.log("test: " + result);40});41var test = wpt.keyIdlType();42test.then(function (result) {43 console.log("test: " + result);44});
Using AI Code Generation
1function runTest() {2 var success;3 var doc;4 var keysetList;5 var keysetNode;6 var keyList;7 var keyNode;8 var keyIdlType;9 doc = load("staff");10 keysetList = doc.getElementsByTagName("keyset");11 keysetNode = keysetList.item(0);12 keyList = keysetNode.getElementsByTagName("key");13 keyNode = keyList.item(0);14 keyIdlType = keyNode.keyIdlType;15 jsUnitCore.assertEquals("keyIdlTypeLink","menu",keyIdlType);16}17function runTest() {18 var success;19 var doc;20 var keysetList;21 var keysetNode;22 var keyList;23 var keyNode;24 var keyIdlType;25 doc = load("staff");26 keysetList = doc.getElementsByTagName("keyset");27 keysetNode = keysetList.item(0);28 keyList = keysetNode.getElementsByTagName("key");29 keyNode = keyList.item(0);30 keyIdlType = keyNode.keyIdlType;31 jsUnitCore.assertEquals("keyIdlTypeLink","menu",keyIdlType);32}
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!!