From 4da4827bcbcdd1ef163f635d9b29416ff272b0bb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 27 Nov 2023 12:48:14 -0500 Subject: Add JSDoc type annotations to project (rebased) --- ext/js/general/object-property-accessor.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'ext/js/general/object-property-accessor.js') diff --git a/ext/js/general/object-property-accessor.js b/ext/js/general/object-property-accessor.js index b8309ed2..d818c9d1 100644 --- a/ext/js/general/object-property-accessor.js +++ b/ext/js/general/object-property-accessor.js @@ -22,9 +22,10 @@ export class ObjectPropertyAccessor { /** * Create a new accessor for a specific object. - * @param {object} target The object which the getter and mutation methods are applied to. + * @param {unknown} target The object which the getter and mutation methods are applied to. */ constructor(target) { + /** @type {unknown} */ this._target = target; } @@ -33,7 +34,7 @@ export class ObjectPropertyAccessor { * @param {(string|number)[]} pathArray The path to the property on the target object. * @param {number} [pathLength] How many parts of the pathArray to use. * This parameter is optional and defaults to the length of pathArray. - * @returns {*} The value found at the path. + * @returns {unknown} The value found at the path. * @throws {Error} An error is thrown if pathArray is not valid for the target object. */ get(pathArray, pathLength) { @@ -44,7 +45,7 @@ export class ObjectPropertyAccessor { if (!ObjectPropertyAccessor.hasProperty(target, key)) { throw new Error(`Invalid path: ${ObjectPropertyAccessor.getPathString(pathArray.slice(0, i + 1))}`); } - target = target[key]; + target = /** @type {import('core').SerializableObject} */ (target)[key]; } return target; } @@ -52,7 +53,7 @@ export class ObjectPropertyAccessor { /** * Sets the value at the specified path. * @param {(string|number)[]} pathArray The path to the property on the target object. - * @param {*} value The value to assign to the property. + * @param {unknown} value The value to assign to the property. * @throws {Error} An error is thrown if pathArray is not valid for the target object. */ set(pathArray, value) { @@ -65,7 +66,7 @@ export class ObjectPropertyAccessor { throw new Error(`Invalid path: ${ObjectPropertyAccessor.getPathString(pathArray)}`); } - target[key] = value; + /** @type {import('core').SerializableObject} */ (target)[key] = value; } /** @@ -87,7 +88,7 @@ export class ObjectPropertyAccessor { throw new Error('Invalid type'); } - delete target[key]; + delete /** @type {import('core').SerializableObject} */ (target)[key]; } /** @@ -110,16 +111,16 @@ export class ObjectPropertyAccessor { const key2 = pathArray2[ii2]; if (!ObjectPropertyAccessor.isValidPropertyType(target2, key2)) { throw new Error(`Invalid path 2: ${ObjectPropertyAccessor.getPathString(pathArray2)}`); } - const value1 = target1[key1]; - const value2 = target2[key2]; + const value1 = /** @type {import('core').SerializableObject} */ (target1)[key1]; + const value2 = /** @type {import('core').SerializableObject} */ (target2)[key2]; - target1[key1] = value2; + /** @type {import('core').SerializableObject} */ (target1)[key1] = value2; try { - target2[key2] = value1; + /** @type {import('core').SerializableObject} */ (target2)[key2] = value1; } catch (e) { // Revert try { - target1[key1] = value1; + /** @type {import('core').SerializableObject} */ (target1)[key1] = value1; } catch (e2) { // NOP } @@ -178,7 +179,7 @@ export class ObjectPropertyAccessor { let value = ''; let escaped = false; for (const c of pathString) { - const v = c.codePointAt(0); + const v = /** @type {number} */ (c.codePointAt(0)); switch (state) { case 'empty': // Empty case 'id-start': // Expecting identifier start @@ -288,7 +289,7 @@ export class ObjectPropertyAccessor { /** * Checks whether an object or array has the specified property. - * @param {*} object The object to test. + * @param {unknown} object The object to test. * @param {string|number} property The property to check for existence. * This value should be a string if the object is a non-array object. * For arrays, it should be an integer. @@ -317,7 +318,7 @@ export class ObjectPropertyAccessor { /** * Checks whether a property is valid for the given object - * @param {object} object The object to test. + * @param {unknown} object The object to test. * @param {string|number} property The property to check for existence. * @returns {boolean} `true` if the property is correct for the given object type, otherwise `false`. * For arrays, this means that the property should be a positive integer. -- cgit v1.2.3