From bc5abb63602f29ef64164753f346cd8ea549699b Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Sat, 19 Oct 2019 09:31:20 -0700 Subject: [PATCH] chore: use @ts-check in all src files (#2543) * chore: use @ts-check in all src files Fixes #1905 * Fix param name in src/type-helper.d.ts Co-Authored-By: Sid Vishnoi --- src/core/biblio-db.js | 55 ++++++++++++++++------------ src/core/expose-modules.js | 15 +++++++- src/core/inline-idl-parser.js | 63 +++++++++++++++++++++++++++++++- src/core/jquery-enhanced.js | 7 +++- src/core/ui.js | 12 +++--- src/core/utils.js | 21 +++++++---- src/geonovum/style.js | 2 + src/type-helper.d.ts | 56 +++++++++++++++++++++++++++- src/w3c/seo.js | 2 + src/w3c/templates/show-people.js | 2 + 10 files changed, 193 insertions(+), 42 deletions(-) diff --git a/src/core/biblio-db.js b/src/core/biblio-db.js index 0cb9970510..b53a199c44 100644 --- a/src/core/biblio-db.js +++ b/src/core/biblio-db.js @@ -1,3 +1,4 @@ +// @ts-check /** * Module core/biblio-db * @@ -12,10 +13,29 @@ import { importIdb } from "./idb.js"; import { pub } from "./pubsubhub.js"; export const name = "core/biblio-db"; +/** + * @typedef {keyof BilbioDb} AllowedType + * @type {Set} + */ const ALLOWED_TYPES = new Set(["alias", "reference"]); /* Database initialization tracker */ const readyPromise = openIdb(); +/** + * @typedef {object} BilbioDb + * + * @property {object} alias Object store for alias objects + * @property {string} alias.key + * @property {object} alias.value + * @property {object} alias.indexes + * @property {string} alias.aliasOf + * + * @property {object} reference Object store for reference objects + * @property {string} reference.key + * @property {object} reference.value + * + * @returns {Promise>} + */ async function openIdb() { const { openDB } = await importIdb(); return await openDB("respec-biblio2", 12, { @@ -50,7 +70,7 @@ export const biblioDB = { /** * Checks if the database has an id for a given type. * - * @param {String} type One of the ALLOWED_TYPES. + * @param {AllowedType} type One of the ALLOWED_TYPES. * @param {String} id The reference to find. * @return {Promise} True if it has it, false otherwise. */ @@ -62,7 +82,7 @@ export const biblioDB = { throw new TypeError("id is required"); } const db = await this.ready; - const objectStore = db.transaction([type], "readonly").objectStore(type); + const objectStore = db.transaction(type, "readonly").store; const range = IDBKeyRange.only(id); const result = await objectStore.openCursor(range); return !!result; @@ -74,16 +94,7 @@ export const biblioDB = { * @return {Promise} Resolves with true if found. */ async isAlias(id) { - if (!id) { - throw new TypeError("id is required"); - } - const db = await this.ready; - const objectStore = db - .transaction(["alias"], "readonly") - .objectStore("alias"); - const range = IDBKeyRange.only(id); - const result = await objectStore.openCursor(range); - return !!result; + return await this.has("alias", id); }, /** * Resolves an alias to its corresponding reference id. @@ -97,9 +108,7 @@ export const biblioDB = { } const db = await this.ready; - const objectStore = db - .transaction("alias", "readonly") - .objectStore("alias"); + const objectStore = db.transaction("alias", "readonly").store; const range = IDBKeyRange.only(id); const result = await objectStore.openCursor(range); return result ? result.value.aliasOf : result; @@ -107,8 +116,8 @@ export const biblioDB = { /** * Get a reference or alias out of the database. * - * @param {String} type The type as per ALLOWED_TYPES. - * @param {[type]} id The id for what to look up. + * @param {AllowedType} type The type as per ALLOWED_TYPES. + * @param {string} id The id for what to look up. * @return {Promise} Resolves with the retrieved object, or null. */ async get(type, id) { @@ -119,7 +128,7 @@ export const biblioDB = { throw new TypeError("id is required"); } const db = await this.ready; - const objectStore = db.transaction([type], "readonly").objectStore(type); + const objectStore = db.transaction(type, "readonly").store; const range = IDBKeyRange.only(id); const result = await objectStore.openCursor(range); return result ? result.value : result; @@ -157,7 +166,7 @@ export const biblioDB = { aliasesAndRefs.reference.add(obj); } }); - const promisesToAdd = Object.keys(aliasesAndRefs) + const promisesToAdd = [...ALLOWED_TYPES] .map(type => { return Array.from(aliasesAndRefs[type]).map(details => this.add(type, details) @@ -169,7 +178,7 @@ export const biblioDB = { /** * Adds a reference or alias to the database. * - * @param {String} type The type as per ALLOWED_TYPES. + * @param {AllowedType} type The type as per ALLOWED_TYPES. * @param {Object} details The object to store. */ async add(type, details) { @@ -184,14 +193,14 @@ export const biblioDB = { } const db = await this.ready; const isInDB = await this.has(type, details.id); - const store = db.transaction([type], "readwrite").objectStore(type); + const store = db.transaction(type, "readwrite").store; // update or add, depending of already having it in db return isInDB ? await store.put(details) : await store.add(details); }, /** * Closes the underlying database. * - * @return {Promise} Resolves after database closes. + * @return {Promise} Resolves after database closes. */ async close() { const db = await this.ready; @@ -204,7 +213,7 @@ export const biblioDB = { async clear() { const db = await this.ready; const storeNames = [...ALLOWED_TYPES]; - const stores = await db.transaction(storeNames, "readwrite"); + const stores = db.transaction(storeNames, "readwrite"); const clearStorePromises = storeNames.map(name => { return stores.objectStore(name).clear(); }); diff --git a/src/core/expose-modules.js b/src/core/expose-modules.js index 9a2da05c8b..3255b8f6fe 100644 --- a/src/core/expose-modules.js +++ b/src/core/expose-modules.js @@ -1,6 +1,12 @@ +// @ts-check const inAmd = !!window.require; if (!inAmd) { - window.require = function(deps, callback) { + /** + * @type {any} + * @param {string[]} deps + * @param {(...modules: any[]) => void} callback + */ + const require = function(deps, callback) { const modules = deps.map(dep => { if (!(dep in window.require.modules)) { throw new Error(`Unsupported dependency name: ${dep}`); @@ -9,9 +15,14 @@ if (!inAmd) { }); callback(...modules); }; - window.require.modules = {}; + require.modules = {}; + window.require = require; } +/** + * @param {string} name + * @param {any} object + */ export function expose(name, object) { if (!inAmd) { window.require.modules[name] = object; diff --git a/src/core/inline-idl-parser.js b/src/core/inline-idl-parser.js index 4eae376ea5..13182a2a7f 100644 --- a/src/core/inline-idl-parser.js +++ b/src/core/inline-idl-parser.js @@ -1,3 +1,4 @@ +// @ts-check // Parses an inline IDL string (`{{ idl string }}`) // and renders its components as HTML @@ -15,7 +16,57 @@ const enumRegex = /^(\w+)\["([\w- ]*)"\]$/; // https://github.com/w3c/respec/pull/1848/files#r225087385 const methodSplitRegex = /\.?(\w+\(.*\)$)/; -/** @param {string} str */ +/** + * @typedef {object} IdlBase + * @property {"base"} type + * @property {string} identifier + * @property {boolean} renderParent + * @property {InlineIdl | null} [parent] + * + * @typedef {object} IdlAttribute + * @property {"attribute"} type + * @property {string} identifier + * @property {boolean} renderParent + * @property {InlineIdl | null} [parent] + * + * @typedef {object} IdlInternalSlot + * @property {"internal-slot"} type + * @property {string} identifier + * @property {boolean} renderParent + * @property {InlineIdl | null} [parent] + * + * @typedef {object} IdlMethod + * @property {"method"} type + * @property {string} identifier + * @property {string[]} args + * @property {boolean} renderParent + * @property {InlineIdl | null} [parent] + * + * @typedef {object} IdlEnum + * @property {"enum"} type + * @property {string} [identifier] + * @property {string} enumValue + * @property {boolean} renderParent + * @property {InlineIdl | null} [parent] + * + * @typedef {object} IdlException + * @property {"exception"} type + * @property {string} identifier + * @property {InlineIdl | null} [parent] + * + * @typedef {object} IdlPrimitive + * @property {"idl-primitive"} type + * @property {string} identifier + * @property {boolean} renderParent + * @property {InlineIdl | null} [parent] + * + * @typedef {IdlBase | IdlAttribute | IdlInternalSlot | IdlMethod | IdlEnum | IdlException | IdlPrimitive} InlineIdl + */ + +/** + * @param {string} str + * @returns {InlineIdl[]} + */ function parseInlineIDL(str) { const [nonMethodPart, methodPart] = str.split(methodSplitRegex); const tokens = nonMethodPart @@ -24,6 +75,7 @@ function parseInlineIDL(str) { .filter(s => s && s.trim()) .map(s => s.trim()); const renderParent = !str.includes("/"); + /** @type {InlineIdl[]} */ const results = []; while (tokens.length) { const value = tokens.pop(); @@ -82,6 +134,9 @@ function parseInlineIDL(str) { return results.reverse(); } +/** + * @param {IdlBase} details + */ function renderBase(details) { // Check if base is a local variable in a section const { identifier, renderParent } = details; @@ -92,6 +147,7 @@ function renderBase(details) { /** * Internal slot: .[[identifier]] or [[identifier]] + * @param {IdlInternalSlot} details */ function renderInternalSlot(details) { const { identifier, parent, renderParent } = details; @@ -107,6 +163,7 @@ function renderInternalSlot(details) { /** * Attribute: .identifier + * @param {IdlAttribute} details */ function renderAttribute(details) { const { parent, identifier, renderParent } = details; @@ -121,6 +178,7 @@ function renderAttribute(details) { /** * Method: .identifier(arg1, arg2, ...), identifier(arg1, arg2, ...) + * @param {IdlMethod} details */ function renderMethod(details) { const { args, identifier, type, parent, renderParent } = details; @@ -140,6 +198,7 @@ function renderMethod(details) { * Enum: * Identifier["enum value"] * Identifer / "enum value" + * @param {IdlEnum} details */ function renderEnum(details) { const { identifier, enumValue, parent } = details; @@ -156,6 +215,7 @@ function renderEnum(details) { /** * Exception value: "NotAllowedError" * Only the WebIDL spec can define exceptions + * @param {IdlException} details */ function renderException(details) { const { identifier } = details; @@ -169,6 +229,7 @@ function renderException(details) { /** * Interface types: {{ unrestricted double }} {{long long}} * Only the WebIDL spec defines these types. + * @param {IdlPrimitive} details */ function renderIdlPrimitiveType(details) { const { identifier } = details; diff --git a/src/core/jquery-enhanced.js b/src/core/jquery-enhanced.js index bfe15b8178..1b8c3399d2 100644 --- a/src/core/jquery-enhanced.js +++ b/src/core/jquery-enhanced.js @@ -1,3 +1,4 @@ +// @ts-check import { addId, getDfnTitles, @@ -16,7 +17,9 @@ window.$ = window.jQuery = $; // return a jQuery object containing the new elements window.$.fn.renameElement = function(name) { const arr = []; + // @ts-ignore this.each(function() { + // @ts-ignore const elem = this; const newElem = renameElement(elem, name); arr.push(newElem); @@ -38,8 +41,8 @@ window.$.fn.renameElement = function(name) { // // This method will publish a warning if a title is used on a definition // instead of an @lt (as per specprod mailing list discussion). -window.$.fn.getDfnTitles = function(args) { - return getDfnTitles(this[0], args); +window.$.fn.getDfnTitles = function() { + return getDfnTitles(this[0]); }; // For any element (usually ), returns an array of targets that diff --git a/src/core/ui.js b/src/core/ui.js index 58c336e6ef..34a479a4a6 100644 --- a/src/core/ui.js +++ b/src/core/ui.js @@ -1,3 +1,4 @@ +// @ts-check // Module core/ui // Handles the ReSpec UI /* jshint laxcomma:true */ @@ -12,6 +13,7 @@ import css from "text!../../assets/ui.css"; import hyperHTML from "hyperhtml"; import { markdownToHtml } from "./markdown.js"; +// @ts-ignore import shortcut from "../shortcut.js"; import { sub } from "./pubsubhub.js"; export const name = "core/ui"; @@ -46,7 +48,7 @@ sub("end-all", () => document.body.prepend(respecUI), { once: true }); const respecPill = hyperHTML``; respecUI.appendChild(respecPill); -respecPill.addEventListener("click", function(e) { +respecPill.addEventListener("click", e => { e.stopPropagation(); if (menu.hidden) { menu.classList.remove("respec-hidden"); @@ -55,7 +57,7 @@ respecPill.addEventListener("click", function(e) { menu.classList.add("respec-hidden"); menu.classList.remove("respec-visible"); } - this.setAttribute("aria-expanded", String(menu.hidden)); + respecPill.setAttribute("aria-expanded", String(menu.hidden)); menu.hidden = !menu.hidden; }); document.documentElement.addEventListener("click", () => { @@ -87,8 +89,8 @@ function errWarn(msg, arr, butName, title) { function createWarnButton(butName, arr, title) { const buttonId = `respec-pill-${butName}`; const button = hyperHTML`