From c548532fcbc901028a44baeaba18b7c6d925a55c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 13 Sep 2024 09:18:08 -0700 Subject: [PATCH 1/2] fix: type error by xmldom --- lib/defaults-utils.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/defaults-utils.js b/lib/defaults-utils.js index 1b4950cd..c79b3b80 100644 --- a/lib/defaults-utils.js +++ b/lib/defaults-utils.js @@ -1,5 +1,5 @@ import _ from 'lodash'; -import { DOMParser, XMLSerializer } from '@xmldom/xmldom'; +import { DOMParser, Element, XMLSerializer } from '@xmldom/xmldom'; import { exec } from 'teen_process'; import B from 'bluebird'; import log from './logger'; @@ -12,7 +12,7 @@ import log from './logger'; * @param {any} value The value to be serialized * @param {boolean} serialize [true] Whether to serialize the resulting * XML to string or to return raw HTMLElement instance - * @returns {Node|string} Either string or raw node representation of + * @returns {Element|string} Either string or raw node representation of * the given value * @throws {TypeError} If it is not known how to serialize the given value */ @@ -25,17 +25,17 @@ export function toXmlArg (value, serialize = true) { const keyEl = xmlDoc.createElement('key'); const keyTextEl = xmlDoc.createTextNode(subKey); keyEl.appendChild(keyTextEl); - xmlDoc.documentElement.appendChild(keyEl); + xmlDoc.documentElement?.appendChild(keyEl); // @ts-ignore The typecast here is fine const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true); - xmlDoc.documentElement.appendChild(subValueEl); + xmlDoc.documentElement?.appendChild(subValueEl); } } else if (_.isArray(value)) { xmlDoc = new DOMParser().parseFromString('', 'text/xml'); for (const subValue of value) { // @ts-ignore The typecast here is fine const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true); - xmlDoc.documentElement.appendChild(subValueEl); + xmlDoc.documentElement?.appendChild(subValueEl); } } else if (_.isBoolean(value)) { xmlDoc = new DOMParser().parseFromString(value ? '' : '', 'text/xml'); @@ -46,15 +46,15 @@ export function toXmlArg (value, serialize = true) { } else if (_.isString(value)) { xmlDoc = new DOMParser().parseFromString(``, 'text/xml'); const valueTextEl = xmlDoc.createTextNode(value); - xmlDoc.documentElement.appendChild(valueTextEl); + xmlDoc.documentElement?.appendChild(valueTextEl); } - if (!xmlDoc) { + if (!xmlDoc || !xmlDoc.documentElement) { throw new TypeError(`The defaults value ${JSON.stringify(value)} cannot be written, ` + `because it is not known how to handle its type`); } - return serialize + return serialize && xmlDoc.documentElement ? new XMLSerializer().serializeToString(xmlDoc.documentElement) : xmlDoc.documentElement; } From 3bdb65bc2d353d614bd65ceb29386d06100ccab8 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 13 Sep 2024 20:27:05 -0700 Subject: [PATCH 2/2] define /** @type{xmlDomElement} */ (xmlDoc.documentElement) --- lib/defaults-utils.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/defaults-utils.js b/lib/defaults-utils.js index c79b3b80..077488d3 100644 --- a/lib/defaults-utils.js +++ b/lib/defaults-utils.js @@ -1,5 +1,5 @@ import _ from 'lodash'; -import { DOMParser, Element, XMLSerializer } from '@xmldom/xmldom'; +import { DOMParser, XMLSerializer } from '@xmldom/xmldom'; import { exec } from 'teen_process'; import B from 'bluebird'; import log from './logger'; @@ -12,7 +12,7 @@ import log from './logger'; * @param {any} value The value to be serialized * @param {boolean} serialize [true] Whether to serialize the resulting * XML to string or to return raw HTMLElement instance - * @returns {Element|string} Either string or raw node representation of + * @returns {xmlDomElement|string} Either string or raw node representation of * the given value * @throws {TypeError} If it is not known how to serialize the given value */ @@ -25,17 +25,17 @@ export function toXmlArg (value, serialize = true) { const keyEl = xmlDoc.createElement('key'); const keyTextEl = xmlDoc.createTextNode(subKey); keyEl.appendChild(keyTextEl); - xmlDoc.documentElement?.appendChild(keyEl); + /** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(keyEl); // @ts-ignore The typecast here is fine const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true); - xmlDoc.documentElement?.appendChild(subValueEl); + /** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(subValueEl); } } else if (_.isArray(value)) { xmlDoc = new DOMParser().parseFromString('', 'text/xml'); for (const subValue of value) { // @ts-ignore The typecast here is fine const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true); - xmlDoc.documentElement?.appendChild(subValueEl); + /** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(subValueEl); } } else if (_.isBoolean(value)) { xmlDoc = new DOMParser().parseFromString(value ? '' : '', 'text/xml'); @@ -46,17 +46,17 @@ export function toXmlArg (value, serialize = true) { } else if (_.isString(value)) { xmlDoc = new DOMParser().parseFromString(``, 'text/xml'); const valueTextEl = xmlDoc.createTextNode(value); - xmlDoc.documentElement?.appendChild(valueTextEl); + /** @type{xmlDomElement} */ (xmlDoc.documentElement).appendChild(valueTextEl); } - if (!xmlDoc || !xmlDoc.documentElement) { + if (!xmlDoc) { throw new TypeError(`The defaults value ${JSON.stringify(value)} cannot be written, ` + `because it is not known how to handle its type`); } - return serialize && xmlDoc.documentElement - ? new XMLSerializer().serializeToString(xmlDoc.documentElement) - : xmlDoc.documentElement; + return serialize + ? new XMLSerializer().serializeToString(/** @type{xmlDomElement} */ (xmlDoc.documentElement)) + : /** @type{xmlDomElement} */ (xmlDoc.documentElement); } /** @@ -154,3 +154,7 @@ export class NSUserDefaults { } } } + +/** + * @typedef {import('@xmldom/xmldom').Element} xmlDomElement + */