fix(universal-xml-plugin): update dependency fast-xml-parser to v4 (#16781)

* fix(universal-xml-plugin): update dependency fast-xml-parser to v4

* Update typing

* Add more typing

* moar

* chore(universal-xml-plugin): update fast-xml-parser api use

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Mykola Mokhnach <mokhnach@gmail.com>
Co-authored-by: Jonathan Lipps <jlipps@gmail.com>
This commit is contained in:
renovate[bot]
2023-08-03 13:04:52 -07:00
committed by GitHub
parent 9a5c9ae55c
commit fc01fb958a
3 changed files with 94 additions and 30 deletions

33
package-lock.json generated
View File

@@ -9349,17 +9349,24 @@
"license": "MIT"
},
"node_modules/fast-xml-parser": {
"version": "3.21.1",
"license": "MIT",
"version": "4.2.7",
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.2.7.tgz",
"integrity": "sha512-J8r6BriSLO1uj2miOk1NW0YVm8AGOOu3Si2HQp/cSmo6EA4m3fcwu2WKjJ4RK9wMLBtg69y1kS8baDiQBR41Ig==",
"funding": [
{
"type": "paypal",
"url": "https://paypal.me/naturalintelligence"
},
{
"type": "github",
"url": "https://github.com/sponsors/NaturalIntelligence"
}
],
"dependencies": {
"strnum": "^1.0.4"
"strnum": "^1.0.5"
},
"bin": {
"xml2js": "cli.js"
},
"funding": {
"type": "paypal",
"url": "https://paypal.me/naturalintelligence"
"fxparser": "src/cli/cli.js"
}
},
"node_modules/fastq": {
@@ -22768,7 +22775,7 @@
"dependencies": {
"@types/xmldom": "0.1.31",
"@xmldom/xmldom": "0.8.8",
"fast-xml-parser": "3.21.1",
"fast-xml-parser": "4.2.7",
"lodash": "4.17.21",
"source-map-support": "0.5.21",
"xpath": "0.0.32"
@@ -23320,7 +23327,7 @@
"requires": {
"@types/xmldom": "0.1.31",
"@xmldom/xmldom": "0.8.8",
"fast-xml-parser": "3.21.1",
"fast-xml-parser": "4.2.7",
"lodash": "4.17.21",
"source-map-support": "0.5.21",
"xpath": "0.0.32"
@@ -29599,9 +29606,11 @@
"version": "2.0.6"
},
"fast-xml-parser": {
"version": "3.21.1",
"version": "4.2.7",
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.2.7.tgz",
"integrity": "sha512-J8r6BriSLO1uj2miOk1NW0YVm8AGOOu3Si2HQp/cSmo6EA4m3fcwu2WKjJ4RK9wMLBtg69y1kS8baDiQBR41Ig==",
"requires": {
"strnum": "^1.0.4"
"strnum": "^1.0.5"
}
},
"fastq": {

View File

@@ -1,43 +1,60 @@
import _ from 'lodash';
import parser, {j2xParser} from 'fast-xml-parser';
import {XMLParser, XMLBuilder} from 'fast-xml-parser';
import NODE_MAP from './node-map';
import {ATTR_MAP, REMOVE_ATTRS} from './attr-map';
import TRANSFORMS from './transformers';
const PARSE_OPTS = {
ignoreAttributes: false,
arrayMode: true,
};
const GEN_OPTS = {
ignoreAttributes: false,
arrayMode: true,
format: true,
};
export const ATTR_PREFIX = '@_';
export const IDX_PATH_PREFIX = `${ATTR_PREFIX}indexPath`;
export const IDX_PREFIX = `${ATTR_PREFIX}index`;
const isAttr = (k) => k.substring(0, 2) === ATTR_PREFIX;
const isNode = (k) => !isAttr(k);
const PARSE_OPTS = {
ignoreAttributes: false,
ignoreDeclaration: true,
attributeNamePrefix: ATTR_PREFIX,
isArray: (name, jPath, isLeafNode, isAttribute) => !isAttribute,
};
const GEN_OPTS = {
ignoreAttributes: false,
attributeNamePrefix: ATTR_PREFIX,
allowBooleanAttributes: true,
suppressBooleanAttributes: false,
format: true,
};
const isAttr = (/** @type {string} */ k) => k.startsWith(ATTR_PREFIX);
const isNode = (/** @type {string} */ k) => !isAttr(k);
/**
*
* @param {string} xmlStr
* @param {string} platform
* @param {{metadata?: Object, addIndexPath?: boolean}} opts
* @returns {{xml: string, unknowns: NodesAndAttributes}}
*/
export function transformSourceXml(xmlStr, platform, {metadata = {}, addIndexPath = false} = {}) {
// first thing we want to do is modify the ios source root node, because it doesn't include the
// necessary index attribute, so we add it if it's not there
xmlStr = xmlStr.replace('<AppiumAUT>', '<AppiumAUT index="0">');
const xmlObj = parser.parse(xmlStr, PARSE_OPTS);
const xmlObj = new XMLParser(PARSE_OPTS).parse(xmlStr);
const unknowns = transformNode(xmlObj, platform, {
metadata,
addIndexPath,
parentPath: '',
});
const jParser = new j2xParser(GEN_OPTS);
let transformedXml = jParser.parse(xmlObj).trim();
let transformedXml = new XMLBuilder(GEN_OPTS).build(xmlObj).trim();
transformedXml = `<?xml version="1.0" encoding="UTF-8"?>\n${transformedXml}`;
return {xml: transformedXml, unknowns};
}
/**
*
* @param {Object} nameMap
* @param {string} name
* @param {string} platform
* @returns {string | null}
*/
function getUniversalName(nameMap, name, platform) {
for (const translatedName of Object.keys(nameMap)) {
const sourceNodes = nameMap[translatedName]?.[platform];
@@ -51,14 +68,33 @@ function getUniversalName(nameMap, name, platform) {
return null;
}
/**
*
* @param {any} nodeName
* @param {string} platform
* @returns {string?}
*/
export function getUniversalNodeName(nodeName, platform) {
return getUniversalName(NODE_MAP, nodeName, platform);
}
/**
*
* @param {string} attrName
* @param {string} platform
* @returns {string?}
*/
export function getUniversalAttrName(attrName, platform) {
return getUniversalName(ATTR_MAP, attrName, platform);
}
/**
*
* @param {any} nodeObj
* @param {string} platform
* @param {{metadata?: Object, addIndexPath?: boolean, parentPath?: string}} opts
* @returns {NodesAndAttributes}
*/
export function transformNode(nodeObj, platform, {metadata, addIndexPath, parentPath}) {
const unknownNodes = [];
const unknownAttrs = [];
@@ -103,6 +139,14 @@ export function transformNode(nodeObj, platform, {metadata, addIndexPath, parent
};
}
/**
*
* @param {any} nodeObj
* @param {string[]} childNodeNames
* @param {string} platform
* @param {{metadata?: Object, addIndexPath?: boolean, parentPath?: string}} opts
* @returns {NodesAndAttributes}
*/
export function transformChildNodes(
nodeObj,
childNodeNames,
@@ -144,6 +188,13 @@ export function transformChildNodes(
return {nodes: unknownNodes, attrs: unknownAttrs};
}
/**
*
* @param {any} nodeObj
* @param {string[]} attrs
* @param {string} platform
* @returns {string[]}
*/
export function transformAttrs(nodeObj, attrs, platform) {
const unknownAttrs = [];
for (const attr of attrs) {
@@ -165,3 +216,7 @@ export function transformAttrs(nodeObj, attrs, platform) {
}
return unknownAttrs;
}
/**
* @typedef {{nodes: string[], attrs: string[]}} NodesAndAttributes
*/

View File

@@ -38,7 +38,7 @@
"dependencies": {
"@types/xmldom": "0.1.31",
"@xmldom/xmldom": "0.8.8",
"fast-xml-parser": "3.21.1",
"fast-xml-parser": "4.2.7",
"lodash": "4.17.21",
"source-map-support": "0.5.21",
"xpath": "0.0.32"