Commit b2782072 authored by Robert Knight's avatar Robert Knight

Enable typechecking for `src/shared/*.js`

Enable typechecking of annotated functions in src/shared/, excluding
subdirectories for the moment.

This found a few minor mistakes with existing JSDoc usage.
parent 487a1b43
...@@ -75,7 +75,7 @@ export default class Bridge { ...@@ -75,7 +75,7 @@ export default class Bridge {
* *
* @param {string} method - Name of remote method to call. * @param {string} method - Name of remote method to call.
* @param {any[]} args - Arguments to method. * @param {any[]} args - Arguments to method.
* @param [Function] callback - Called with an array of results. * @return {Promise<any[]>} - Array of results, one per connected frame
*/ */
call(method, ...args) { call(method, ...args) {
let cb; let cb;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Callback invoked when another frame is discovered in this window which runs * Callback invoked when another frame is discovered in this window which runs
* the Hypothesis sidebar or annotation layer code. * the Hypothesis sidebar or annotation layer code.
* *
* @type {Function} DiscoveryCallback * @callback DiscoveryCallback
* @param {Window} source - The frame that was discovered. * @param {Window} source - The frame that was discovered.
* @param {string} origin - The origin to use when posting messages to this frame. * @param {string} origin - The origin to use when posting messages to this frame.
* @param {string} token - A random identifier used by this frame. * @param {string} token - A random identifier used by this frame.
...@@ -115,7 +115,7 @@ export default class Discovery { ...@@ -115,7 +115,7 @@ export default class Discovery {
// window and send messages to them. // window and send messages to them.
const queue = [this.target.top]; const queue = [this.target.top];
while (queue.length > 0) { while (queue.length > 0) {
const parent = queue.shift(); const parent = /** @type {Window} */ (queue.shift());
if (parent !== this.target) { if (parent !== this.target) {
parent.postMessage(beaconMessage, this.origin); parent.postMessage(beaconMessage, this.origin);
} }
...@@ -176,7 +176,7 @@ export default class Discovery { ...@@ -176,7 +176,7 @@ export default class Discovery {
// Notify caller of `startDiscovery` in this frame that we found another // Notify caller of `startDiscovery` in this frame that we found another
// frame. // frame.
if (discovered) { if (discovered && this.onDiscovery) {
this.onDiscovery.call(null, source, origin, token); this.onDiscovery.call(null, source, origin, token);
} }
} }
......
...@@ -96,6 +96,7 @@ export class Injector { ...@@ -96,6 +96,7 @@ export class Injector {
const resolveErr = new Error( const resolveErr = new Error(
`Failed to construct dependency "${dependency}" of "${name}": ${e.message}` `Failed to construct dependency "${dependency}" of "${name}": ${e.message}`
); );
// @ts-ignore - `cause` is a custom property
resolveErr.cause = e; resolveErr.cause = e;
throw resolveErr; throw resolveErr;
} }
...@@ -143,7 +144,7 @@ export class Injector { ...@@ -143,7 +144,7 @@ export class Injector {
* Run a function which uses one or more dependencies provided by the * Run a function which uses one or more dependencies provided by the
* container. * container.
* *
* @param {Function} - * @param {Function} callback -
* A callback to run, with dependencies annotated in the same way as * A callback to run, with dependencies annotated in the same way as
* functions or classes passed to `register`. * functions or classes passed to `register`.
* @return {any} - Returns the result of running the function. * @return {any} - Returns the result of running the function.
......
...@@ -34,7 +34,7 @@ export function jsonConfigsFrom(document) { ...@@ -34,7 +34,7 @@ export function jsonConfigsFrom(document) {
for (let i = 0; i < settingsElements.length; i++) { for (let i = 0; i < settingsElements.length; i++) {
let settings; let settings;
try { try {
settings = JSON.parse(settingsElements[i].textContent); settings = JSON.parse(settingsElements[i].textContent || '');
} catch (err) { } catch (err) {
console.warn( console.warn(
'Could not parse settings from js-hypothesis-config tags', 'Could not parse settings from js-hypothesis-config tags',
......
...@@ -44,10 +44,10 @@ export function matchShortcut(event, shortcut) { ...@@ -44,10 +44,10 @@ export function matchShortcut(event, shortcut) {
} }
const actualModifiers = const actualModifiers =
(event.ctrlKey && modifiers.ctrl) | (event.ctrlKey ? modifiers.ctrl : 0) |
(event.metaKey && modifiers.meta) | (event.metaKey ? modifiers.meta : 0) |
(event.altKey && modifiers.alt) | (event.altKey ? modifiers.alt : 0) |
(event.shiftKey && modifiers.shift); (event.shiftKey ? modifiers.shift : 0);
return ( return (
actualModifiers === requiredModifiers && actualModifiers === requiredModifiers &&
...@@ -57,7 +57,7 @@ export function matchShortcut(event, shortcut) { ...@@ -57,7 +57,7 @@ export function matchShortcut(event, shortcut) {
/** /**
* @typedef ShortcutOptions * @typedef ShortcutOptions
* @prop {HTMLElement} rootElement - * @prop {HTMLElement} [rootElement] -
* Element on which the key event listener should be installed. Defaults to * Element on which the key event listener should be installed. Defaults to
* `document.body`. * `document.body`.
*/ */
...@@ -110,7 +110,7 @@ export function useShortcut( ...@@ -110,7 +110,7 @@ export function useShortcut(
) { ) {
useEffect(() => { useEffect(() => {
if (!shortcut) { if (!shortcut) {
return null; return undefined;
} }
return installShortcut(shortcut, onPress, { rootElement }); return installShortcut(shortcut, onPress, { rootElement });
}, [shortcut, onPress, rootElement]); }, [shortcut, onPress, rootElement]);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"target": "ES2020" "target": "ES2020"
}, },
"include": [ "include": [
"shared/*.js",
"sidebar/util/*.js" "sidebar/util/*.js"
] ]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment