Skip to content

Commit

Permalink
define /** @type{xmlDomElement} */ (xmlDoc.documentElement)
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuCocoa committed Sep 14, 2024
1 parent c548532 commit 3bdb65b
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/defaults-utils.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
*/
Expand All @@ -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('<array></array>', '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 ? '<true/>' : '<false/>', 'text/xml');
Expand All @@ -46,17 +46,17 @@ export function toXmlArg (value, serialize = true) {
} else if (_.isString(value)) {
xmlDoc = new DOMParser().parseFromString(`<string></string>`, '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);
}

/**
Expand Down Expand Up @@ -154,3 +154,7 @@ export class NSUserDefaults {
}
}
}

/**
* @typedef {import('@xmldom/xmldom').Element} xmlDomElement
*/

0 comments on commit 3bdb65b

Please sign in to comment.