@oracle/oraclejet-audit 19.0.2 → 20.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/corePackMigrator/resource/htmlUtils.js +1 -1
- package/jaf-amd.js +1 -1
- package/lib/AST_Ts.js +1 -1
- package/lib/checkage.js +1 -1
- package/lib/defaults.js +1 -1
- package/lib/ns.js +1 -1
- package/lib/scaffold.js +1 -1
- package/meta/19.0.0/jetauditmeta.js +1 -1
- package/meta/20.0.0/jetauditmeta.js +9 -0
- package/meta/metaverlist.json +1 -1
- package/package.json +2 -2
- package/rulepacks/jetwc/jetwc-css-scoping.js +1 -1
- package/rulepacks/jetwc/rules.json +1 -1
- package/rulepacks/jetwc-lib/jetwcutils-componentFinder.js +1 -1
- package/rulepacks/jetwc-lib/jetwcutils-tsxUtils.js +1 -1
- package/rules/jet/msgid.json +1 -1
- package/rules/jet/oj-html-ojoption-text.js +1 -1
- package/rules/jet/rules.json +1 -1
|
@@ -3,4 +3,4 @@
|
|
|
3
3
|
* Licensed under The Universal Permissive License (UPL), Version 1.0
|
|
4
4
|
* as shown at https://oss.oracle.com/licenses/upl/
|
|
5
5
|
*/
|
|
6
|
-
const{parseDocument,DomUtils}=require("htmlparser2");const render=require("dom-serializer").default;const logging=require("../utilities/logging");const self=require("./htmlUtils");const astUtils=require("./astUtils");function inManifestData(component){for(let obj of global.manifestData){if(obj.componentName&&obj.componentName===component.name){return obj}else if(obj.componentClass){if(component.attribs["class"]&&component.attribs["class"].includes(obj.componentClass)){return obj}}}return false}function checkFileUpdate(){if(!global.fileIsUpdated){global.fileIsUpdated=true;global.migrationMetrics.files+=1;logging.logFilePath(global.filePath);return true}return false}function _getLabelFromDOMNodeHelper(child,slotName,cnodes=[],label=""){const expRegex=/[\[{].*?[\]}]/g;if(child.type&&child.type=="text"){if(/\S/.test(child.data)){if(label){if(expRegex.test(label)){if(expRegex.test(child.data.trim())){label=label.endsWith("()]]")?`${label.substring(0,label.length-2)} + ${child.data.trim().substring(2,child.data.length-2)}()]]`:`${label.substring(0,label.length-2)}() + ${child.data.trim().substring(2,child.data.length-2)}()]]`}else{label=label.endsWith("()]]")?`${label.substring(0,label.length-2)} + ' ${child.data.trim()}']]`:`${label.substring(0,label.length-2)}() + ' ${child.data.trim()}']]`}}else{if(expRegex.test(child.data.trim())){label=`[['${label} ' + ${child.data.trim().substring(2,child.data.length-2)}()]]`}else{label+=" "+child.data.trim()}}}else{label=child.data.trim()}cnodes.push(child)}}else if(child.type&&child.type=="tag"&&child.name.toUpperCase()=="OJ-BIND-TEXT"&&(!slotName&&!child.attribs.slot||slotName&&child.attribs.slot===slotName)){if(label){if(expRegex.test(label)){if(expRegex.test(child.attribs.value)){label=label.endsWith("()]]")?`${label.substring(0,label.length-2)}() + ${child.attribs.value.substring(2,child.attribs.value.length-2)}()]]`:`${label.substring(0,label.length-2)} + ${child.attribs.value.substring(2,child.attribs.value.length-2)}()]]`}else{label=label.endsWith("()]]")?`${label.substring(0,label.length-2)} + ' ${child.attribs.value}']]`:`${label.substring(0,label.length-2)}() + ' ${child.attribs.value}']]`}}else{if(expRegex.test(child.attribs.value)){label=`[['${label} ' + ${child.attribs.value.substring(2,child.attribs.value.length-2)}()]]`}else{label+=" "+child.attribs.value}}}else{label=child.attribs.value}cnodes.push(child)}else if(child.children&&!!child.children.length){if(child.attribs.slot===slotName||!slotName&&!child.attribs.slot){var regex=new RegExp("^[^\\dA-Za-z]*$");for(let c of child.children){label=_getLabelFromDOMNodeHelper(c,slotName,cnodes,label);if(c.type==="text"&®ex.test(c.data)){cnodes.push(c)}}cnodes.push(child)}}return label}function _getLabelFromDOMNode(node,slotName,label=""){if(node&&node.children&&!!node.children.length){let cnodes=[];for(let child of node.children){label=_getLabelFromDOMNodeHelper(child,slotName,cnodes,label)}if(cnodes.length>0){for(let node of cnodes){DomUtils.removeElement(node)}}}return label}function _findDescendant(component,tagName,slotName){if(component.children){for(let child of component.children){if(child.name===tagName){if(!slotName||child.attribs.slot&&child.attribs.slot===slotName){return child}}else if(child.children){const descendant=_findDescendant(child,tagName);if(descendant){return descendant}}}}return false}function getDom(text=""){return parseDocument(text,{withStartIndices:true,withEndIndices:true})}function _transferAttribute(fromTag,toTag,attribute,value,joined){if(fromTag.attribs[attribute]||joined&&fromTag.attribs[joined]){toTag.attribs[attribute]=value;delete fromTag.attribs[attribute]?fromTag.attribs[attribute]:fromTag.attribs[joined]}}function _hasAttributesToIgnoreHelper(dom,component,rule,key,parent,corePackTag,modelCode){if(rule.missPeerTag){const peerRule=rule.missPeerTag;for(let tagAttrSet of peerRule.tagAttributes){let tag=tagAttrSet.name;for(let attrSet of peerRule.matchValues){const peer=_findPeer(dom,component,tag,attrSet.ownAttribute,attrSet.peerAttribute);if(peer){let migratable=true;if(tagAttrSet.attribute){for(let set of tagAttrSet.attribute){migratable=_hasAttributesToIgnoreHelper(dom,peer,{"missAttribute":set});if(!migratable){break}}}if(tagAttrSet.missSlot&&migratable){migratable=_findAttribute(peer,"slot",peerRule.missSlot)?true:false}if(tagAttrSet.mapper&&migratable){migratable=modelCode?true:false}if(tagAttrSet.descendant&&migratable){migratable=!_hasAttributesToIgnoreHelper(dom,peer,tagAttrSet.descendant)}if(migratable){return false}}}}return true};if(rule.missDescendant){const descRule=rule.missDescendant;for(let tagAttrSet of descRule.tagAttributes){let tag=tagAttrSet.name;const desc=_findDescendant(component,tag,tagAttrSet.missSlot);if(desc){let migratable=true;if(tagAttrSet.attribute){for(let set of tagAttrSet.attribute){migratable=_hasAttributesToIgnoreHelper(dom,desc,{"missAttribute":set});if(!migratable){break}}}if(tagAttrSet.mapper&&migratable){migratable=modelCode?true:false}if(tagAttrSet.descendant&&migratable){migratable=!_hasAttributesToIgnoreHelper(dom,desc,tagAttrSet.descendant)}if(migratable){return false}}}return true};if(!rule.descendant&&component.attribs){if(rule.missAttribute){for(let key in rule.missAttribute){if(_findAttribute(component,key,rule.missAttribute[key])){return false}}if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(component.name,rule.missAttribute,"",parent,true)}return true}if(rule.attribute&&!rule.typeDefined){let attr=_findAttribute(component,rule.attribute);if(attr){rule.attribute=attr.attribute;if(rule.allowedValues&&!rule.allowedValues.includes(component.attribs[rule.attribute])||rule.prohibitedValues&&rule.prohibitedValues.includes(component.attribs[rule.attribute])||!rule.allowedValues&&!rule.prohibitedValues){if(corePackTag){checkFileUpdate();let value=(rule.allowedValues||rule.prohibitedValues)&&component.attribs[rule.attribute]?component.attribs[rule.attribute]:"";logging.logIgnoreComponent(component.name,rule.attribute,value,parent)}return true}}}else if(component.attribs[key]&&component.attribs[key]===rule[key]){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(component.name,key,rule[key],parent)}return true}}if(component.children){for(let j=0;j<component.children.length;j++){const child=component.children[j];if(child.type==="tag"){if(rule.descendant){if(child.name===rule.descendant){if(rule.attribute){const attr=_findAttribute(child,rule.attribute);if(attr){rule.attribute=attr.attribute;if(rule.allowedValues&&!rule.allowedValues.includes(child.attribs[rule.attribute])||rule.prohibitedValues&&rule.prohibitedValues.includes(child.attribs[rule.attribute])||!rule.prohibitedValues&&!rule.allowedValues){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(child.name,rule.attribute,child.attribs[rule.attribute],parent)}return true}}}else if(rule.slotName&&child.attribs["slot"]&&child.attribs["slot"]===rule.slotName){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(child.name,"slot",child.attribs[rule.attribute],parent)}return true}else if(rule.missSlot){if(!child.attribs["slot"]||child.attribs["slot"]!==rule.missSlot){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(child.name,"slot",rule.missSlot,parent,true)}return true}else if(child.attribs["slot"]&&child.attribs["slot"]===rule.missSlot){return false}}else{if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(rule.descendant,"","",parent)}return true}}else if(_hasAttributesToIgnoreHelper(dom,child,rule,key,parent,corePackTag)){return true}}if(!rule.attribute&&!rule.missAttribute&&_hasAttributesToIgnoreHelper(dom,child,rule,key,parent,corePackTag)){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(component.name,key,rule[key])}return true}}}}}function _toCamelCase(attribute){let camelCaseName=attribute.split("-");for(let i in camelCaseName){if(i>0){camelCaseName[i]=camelCaseName[i][0].toUpperCase()+camelCaseName[i].slice(1)}}camelCaseName=camelCaseName.join("");return camelCaseName}function _findAttribute(component,name,value=""){if(name){const joined=name.split("-").join("");if(DomUtils.hasAttrib(component,name)&&(!value||value===component.attribs[name])){return{attribute:name,attrValue:component.attribs[name]}}else if(DomUtils.hasAttrib(component,joined)&&(!value||value===component.attribs[joined])){return{attribute:joined,format:"camel",attrValue:component.attribs[joined]}}else if(DomUtils.hasAttrib(component,`:${name}`)&&(!value||value===component.attribs[`:${name}`])){return{attribute:`:${name}`,format:"colon",attrValue:component.attribs[`:${name}`]}}else if(DomUtils.hasAttrib(component,`:${joined}`)&&(!value||value===component.attribs[`:${joined}`])){return{attribute:`:${joined}`,format:"colonCamel",attrValue:component.attribs[`:${joined}`]}}}return false}function _findPeer(dom,component,peerTag,componentAttr,peerAttr){const attributeValue=_findAttribute(component,componentAttr).attrValue;if(attributeValue){const peer=DomUtils.findOne(elem=>elem.name===peerTag&&_findAttribute(elem,peerAttr,attributeValue),dom.children);return peer}}function hasAttributesToIgnore(dom,component,metadata,parent="",modelCode){const ignoreRuleData=metadata.ruleSetToIgnore;if(ignoreRuleData){for(let i=0;i<ignoreRuleData.length;i++){rule=ignoreRuleData[i];let arr=Object.keys(rule);let key=arr[0];if(!rule.type){if(rule.descendant){parent=component.name}if(_hasAttributesToIgnoreHelper(dom,component,rule,key,parent,metadata.corePackTag,modelCode)){return true}}}}return false}function hasExpressionAttributes(component,componentName,importantAttributes){const excludedAttributes=global.configData.excludeAttributeExpressions[componentName];const regex=new RegExp(`((\\{\\{)|(\\[\\[)).*((\\}\\})|(\\]\\]))`);if(global.jaf.options.dryRunMode&&importantAttributes){for(let attribute of importantAttributes){if(component.attribs[`:${attribute}`]){attribute=`:${attribute}`}if(regex.test(component.attribs[attribute])){if(checkFileUpdate()){logging.logAttributeExpression(componentName,attribute,component.attribs[attribute])}}}}if(excludedAttributes&&excludedAttributes.length){for(let i=0;i<excludedAttributes.length;i++){if(component.attribs[`:${excludedAttributes[i]}`]){excludedAttributes[i]=`:${excludedAttributes[i]}`}if(regex.test(component.attribs[excludedAttributes[i]])){checkFileUpdate();logging.logExcludeAttributeExpression(componentName,excludedAttributes[i],component.attribs[excludedAttributes[i]]);return true}}}return false}function commentSlotContent(component,data){const slotValue=data.slot;for(let child of component.children){if(child.name&&child.attribs.slot==slotValue){const cnode=getDom("<!-- "+render(child,{decodeEntities:false})+" -->");DomUtils.replaceElement(child,cnode);checkFileUpdate();return[logging.logCommentSlotContent(component.name,slotValue,child.name)]}};}function slotContentToAttribute(component,data,dom){let logs=[];for(let obj of data){let toNode;if(obj.toDescendant){const descendant=_findDescendant(component,obj.toDescendant,obj.slotNameTo?obj.slotNameTo:"");if(descendant){toNode=descendant}else{continue}}if(obj.fromDescendant){const descendant=_findDescendant(component,obj.fromDescendant,obj.slotNameFrom?obj.slotNameFrom:"");if(descendant){component=descendant}else{continue}}if(obj.toPeer||obj.fromPeer){const peer=_findPeer(dom,component,obj.toPeer?obj.toPeer:obj.fromPeer,obj.ownAttribute,obj.peerAttribute);if(peer){if(obj.toPeer){toNode=peer}else{component=peer}}else{continue}}if(!Object.keys(component.attribs).includes(obj.attribute)){component.attribs[obj.attribute]=_getLabelFromDOMNode(component,data.slotNameFrom);if(!component.attribs[obj.attribute]){delete component.attribs[obj.attribute]}else{checkFileUpdate();logs.push(logging.logSlotContentToAttribute(component.name,obj.attribute,component.attribs[obj.attribute]));if(toNode){_transferAttribute(component,toNode,obj.attribute,component.attribs[obj.attribute]);logs.push(logging.logAttributeTransfer(component.name,toNode.name,obj.attribute,toNode.attribs[obj.attribute]))}return logs}}}}function migrateTag(component,legacyComponentName,coreComponentName,isDescendant){component.name=coreComponentName;checkFileUpdate();global.migrationMetrics.components+=1;return logging.logTagMigration(legacyComponentName,coreComponentName,isDescendant)}function migrateClassToTag(component,legacyClassName,coreComponentName,isDescendant){const fromTag=component.name;component.name=coreComponentName;classNames=component.attribs.class.split(" ");index=classNames.indexOf(legacyClassName);classNames.splice(index,1);if(classNames.length===0){delete component.attribs.class}else{component.attribs.class=classNames.join(" ")}checkFileUpdate();global.migrationMetrics.components+=1;return logging.logClassToTagMigration(fromTag,legacyClassName,coreComponentName,isDescendant)}function migrateTagByAttribute(component,data,isDescendant){for(const tagSet of data){if(tagSet.attribute){const attribute=_findAttribute(component,tagSet.attribute,tagSet.attributeValue);if(attribute){component.name=tagSet.toTag;checkFileUpdate();global.migrationMetrics.components+=1;return logging.logTagMigration(tagSet.fromTag,tagSet.toTag,isDescendant)}}else{component.name=tagSet.toTag;checkFileUpdate();global.migrationMetrics.components+=1;return logging.logTagMigration(tagSet.fromTag,tagSet.toTag)}}};function classNameToAttribute(component,data){let logs=[];data.forEach(obj=>{const from=obj.className;const to=obj.attributeValue;const attribute=obj.attribute;if(component.attribs.class){classNames=component.attribs.class.split(" ");if(typeof obj.className==="string"&&classNames.includes(obj.className)){index=classNames.indexOf(from);classNames.splice(index,1);component.attribs[attribute]=to;checkFileUpdate();logs.push(logging.logClassNameToAttribute(from,attribute,to))}else if(typeof obj.className==="object"&&from.every(c=>classNames.includes(c))){obj.className.forEach(cl=>{index=classNames.indexOf(cl);classNames.splice(index,1)});component.attribs[attribute]=to;checkFileUpdate();logs.push(logging.logClassNameToAttribute(from,attribute,to))}if(classNames.length==0)delete component.attribs.class;else component.attribs.class=classNames.join(" ")}});return logs}function migrateAttributes(component,data,dom){let toNode;let logs=[];for(const attributeSet in data){let camelFromAttribute="";let camelToAttribute="";let tagExists=true;let obj=data[attributeSet];if(obj.fromDescendant||obj.fromPeer||obj.toDescendant||obj.toPeer){tagExists=false;if(obj.fromDescendant||obj.toDescendant){const descendant=_findDescendant(component,obj.fromDescendant?obj.fromDescendant:obj.toDescendant,obj.slotName);if(component.children&&descendant){tagExists=true;if(obj.fromDescendant){toNode=component;component=descendant}else{toNode=descendant}}else{continue}}else{const peerTag=_findPeer(dom,component,obj.toPeer?obj.toPeer:obj.fromPeer,obj.ownAttribute,obj.peerAttribute);if(peerTag){tagExists=true;if(obj.fromPeer){toNode=component;component=peerTag}else{toNode=peerTag}}else{continue}}}if(tagExists){let hasDepAttribute=true;if(obj.depAttribute){hasDepAttribute=_findAttribute(component,obj.depAttribute)}const hasFromAttribute=_findAttribute(component,obj.fromAttribute,obj.fromValue);let fromAttribute=obj.fromAttribute;if(hasFromAttribute){fromAttribute=hasFromAttribute.attribute}let toAttribute=obj.toAttribute;if(hasFromAttribute.format){if(toAttribute){if(hasFromAttribute.format==="camel"){toAttribute=toAttribute.split("-").join("")}else if(hasFromAttribute.format==="colonCamel"){toAttribute=`:${toAttribute.split("-").join("")}`}else{toAttribute=`:${toAttribute}`}}}const hasToAttribute=_findAttribute(component,toAttribute);if(!(obj.fromAttribute===obj.toAttribute&&obj.fromValue===obj.toValue)){if(hasDepAttribute&&hasFromAttribute&&toAttribute&&(!hasToAttribute||fromAttribute===toAttribute)){if(obj.toValue){switch(hasFromAttribute.format){case"camel":camelFromAttribute=_toCamelCase(obj.fromAttribute);camelToAttribute=_toCamelCase(obj.toAttribute);break;case"colonCamel":camelFromAttribute=`:${_toCamelCase(obj.fromAttribute)}`;camelToAttribute=`:${_toCamelCase(obj.toAttribute)}`;break}component.attribs[camelToAttribute?camelToAttribute:toAttribute]=obj.toValue;checkFileUpdate();logs.push(logging.logAttributeMigration(camelFromAttribute?camelFromAttribute:fromAttribute,obj.fromValue,camelToAttribute?camelToAttribute:toAttribute,obj.toValue))}else{let fromValue=obj.fromValue?obj.fromValue:component.attribs[fromAttribute];switch(hasFromAttribute.format){case"camel":camelFromAttribute=_toCamelCase(obj.fromAttribute);camelToAttribute=_toCamelCase(obj.toAttribute);break;case"colonCamel":camelFromAttribute=`:${_toCamelCase(obj.fromAttribute)}`;camelToAttribute=`:${_toCamelCase(obj.toAttribute)}`;break}if(obj.fromValue){component.attribs[camelToAttribute?camelToAttribute:toAttribute]=""}else{component.attribs[camelToAttribute?camelToAttribute:toAttribute]=component.attribs[fromAttribute]}checkFileUpdate();logs.push(logging.logAttributeMigration(camelFromAttribute?camelFromAttribute:fromAttribute,fromValue,camelToAttribute?camelToAttribute:toAttribute))}if(fromAttribute!==toAttribute){delete component.attribs[fromAttribute]}}else if(hasDepAttribute&&hasFromAttribute&&!toAttribute){delete component.attribs[fromAttribute];switch(hasFromAttribute.format){case"camel":fromAttribute=_toCamelCase(obj.fromAttribute);break;case"colonCamel":fromAttribute=`:${_toCamelCase(obj.fromAttribute)}`;break}checkFileUpdate();logs.push(logging.logAttributeDeprecation(fromAttribute,obj.fromValue))}else if(hasDepAttribute&&!obj.fromAttribute&&obj.toAttribute&&!hasToAttribute){if(toNode){toNode.attribs[obj.toAttribute]=obj.toValue}else{component.attribs[obj.toAttribute]=obj.toValue}checkFileUpdate();logs.push(logging.logAttributeAddition(obj.toAttribute,obj.toValue,toNode?toNode.name:""))}}if(toNode&&hasFromAttribute){switch(hasFromAttribute.format){case"camel":camelToAttribute=_toCamelCase(obj.toAttribute);break;case"colonCamel":camelToAttribute=`:${_toCamelCase(obj.toAttribute)}`;break}checkFileUpdate();_transferAttribute(component,toNode,camelToAttribute?camelToAttribute:toAttribute,component.attribs[camelToAttribute?camelToAttribute:toAttribute],toAttribute);const toValue=toNode.attribs[camelToAttribute?camelToAttribute:toAttribute];logs.push(logging.logAttributeTransfer(component.name,toNode.name,camelToAttribute?camelToAttribute:toAttribute,toValue));if(obj.fromDescendant||obj.fromPeer){component=toNode}}}}return logs}function handleSpecialCase(code,componentName,data){let obj;let regex;if(data[1]){obj=data[1];regex=new RegExp(obj.fromString.split(" ").join("\\s*"),"igm");code=code.replace(regex,obj.toString)}obj=data[0];regex=new RegExp(obj.fromString.split(" ").join("\\s*"),"igm");code=code.replace(regex,obj.toString);return code}function _migrateDescendantsHelper(dom,children,componentMetadata,parent,compCount,modelCode,fileMigratedCount,revertMig,logs=[]){let descendants=[];for(let child of children){let newLogs=[];if(child.name===componentMetadata.legacyTag&&!revertMig){let componentCopy=child.cloneNode(true);descendants.push({original:child,copy:componentCopy});if(!hasAttributesToIgnore(dom,child,componentMetadata,parent)&&!hasExpressionAttributes(child,componentMetadata.legacyTag,componentMetadata.importantAttributes)){if(componentMetadata["migrateTagByAttribute"]){newLogs.push(migrateTagByAttribute(child,componentMetadata["migrateTagByAttribute"],true))}else{newLogs.push(migrateTag(child,componentMetadata.legacyTag,componentMetadata.corePackTag,true))}for(let key in componentMetadata){if(typeof self[key]=="function"&&key!="migrateTagByAttribute"&&key!="migrateImports"&&key!="migrateOptions"){newLogs=newLogs.concat(self[key](child,componentMetadata[key]))}else if(key==="migrateOptions"){let info=migrateOptions(child,componentMetadata[key],modelCode,fileMigratedCount,componentMetadata.legacyTag);if(!modelCode&&info){logging.logMapperRequest(componentMetadata.legacyTag);revertMig=true}else if(info){if(info.revert){revertMig=true}else{modelCode=info.mCode;newLogs=newLogs.concat(info.newLogs)}}}}if(!compCount.migrated[componentMetadata.legacyTag]){compCount.migrated[componentMetadata.legacyTag]=1}else{compCount.migrated[componentMetadata.legacyTag]+=1}if(revertMig){for(let obj of descendants){DomUtils.replaceElement(obj.original,obj.copy);global.migrationMetrics.components-=1;compCount.migrated[componentMetadata.legacyTag]-=1;if(compCount.migrated[componentMetadata.legacyTag]===0){delete compCount.migrated[componentMetadata.legacyTag]}if(compCount.skipped[componentMetadata.legacyTag]){compCount.skipped[componentMetadata.legacyTag]+=1}else{compCount.skipped[componentMetadata.legacyTag]=1}}}}}else{if(child.name===componentMetadata.legacyTag&&revertMig){if(compCount.skipped[componentMetadata.legacyTag]){compCount.skipped[componentMetadata.legacyTag]+=1}else{compCount.skipped[componentMetadata.legacyTag]=1}}else if(child.children){returnValue=_migrateDescendantsHelper(dom,child.children,componentMetadata,parent,compCount);newLogs=newLogs.concat(returnValue.logs);compCount=returnValue.count;revertMig=returnValue.revert}}if(!revertMig){logs=logs.concat(newLogs)}}return{logs:logs,count:compCount,mCode:modelCode,revert:revertMig}}function migrateDescendants(dom,component,metadata,modelCode,fileComponentsInfo,logs=[]){let compCount={migrated:{},skipped:{}};const data=metadata.migrateDescendants;let returnValue=false;let revertMig;for(let metaName of data){let componentMetadata=require("../metadata/"+metaName+".json");let fileMigratedCount=fileComponentsInfo.migrated[componentMetadata.legacyTag]?fileComponentsInfo.migrated[componentMetadata.legacyTag]:0;if(component.children){returnValue=_migrateDescendantsHelper(dom,component.children,componentMetadata,metadata.legacyTag,compCount,modelCode,fileMigratedCount,revertMig);if(returnValue){logs=logs.concat(returnValue.logs);modelCode=returnValue.mCode;revertMig=returnValue.revert;compCount=returnValue.count}}}return{logs:logs,count:compCount,mCode:modelCode}}function removeNode(component){DomUtils.removeElement(component);return logging.logComponentRemoved(component.name)}function replaceComponent(fromNode,data){for(let obj of data){let descendant=_findDescendant(fromNode,obj.descendant);if(descendant){DomUtils.replaceElement(fromNode,descendant);return logging.logComponentReplacement(fromNode.name,descendant.name)}}}function migrateOptions(component,data,modelCode,compCount,legacyTag){if(modelCode){if(!_findAttribute(component,data.toAttribute)&&(_findDescendant(component,"oj-option")||_findDescendant(component,"template"))){let optionsInfo=[];let tag=legacyTag.slice(3);tag=tag[0].toUpperCase()+tag.slice(1);let varName=`optionsMig${tag}${compCount}`;let template;for(let child of component.children){if(child.name==="oj-option"){for(let ignoreAttr of data.ignoreAttribute){if(child.attribs[ignoreAttr]){logging.logIgnoreComponent(child.name,ignoreAttr,"",legacyTag);return{revert:true}}}child.attribs.label=_getLabelFromDOMNode(child,"");if(child.children){for(let c of child.children){if(c.name==="span"||c.name==="div"){if(c.attribs.slot&&c.attribs.class){child.attribs[c.attribs.slot]={"class":c.attribs.class}}}else if(c.name==="oj-avatar"){if(c.attribs.slot){if(c.attribs.src&&c.attribs.role){child.attribs[c.attribs.slot]={"type":c.attribs.role,"src":c.attribs.src}}else{child.attribs[c.attribs.slot]={"class":c.attribs.class}}}}}}optionsInfo.push(child.attribs);DomUtils.removeElement(child)}else if(child.name==="oj-bind-for-each"){template=child.attribs.data.slice(2,child.attribs.data.length-2);let optionComp=_findDescendant(child,"oj-option");for(let ignoreAttr of data.ignoreAttribute){if(optionComp.attribs[ignoreAttr]){logging.logIgnoreComponent(optionComp.name,ignoreAttr,"",legacyTag);return{revert:true}}}optionComp.attribs.label=_getLabelFromDOMNode(optionComp,"");if(optionComp.children){for(let c of optionComp.children){if(c.name==="span"||c.name==="div"){if(c.attribs.slot&&c.attribs.class){optionComp.attribs[c.attribs.slot]={"class":c.attribs.class}}}else if(c.name==="oj-avatar"){if(c.attribs.slot){if(c.attribs.src&&c.attribs.role){optionComp.attribs[c.attribs.slot]={"type":c.attribs.role,"src":c.attribs.src}}else{optionComp.attribs[c.attribs.slot]={"class":c.attribs.class}}}}}}optionsInfo.push(optionComp.attribs);DomUtils.removeElement(child)}}let varValue=optionsInfo;let info=astUtils.migrateOptionsModel(data,varName,varValue,modelCode,template);if(info){if(info.emptyModel){logging.logIgnoreComponent(legacyTag,"","","","To migrate data driven component, model file provided needs a class where a new variable can be created");return{revert:true}}else{varName=info.varName;let code=info.code;let logs=info.logs;component.attribs[data.toAttribute]=`[[${varName}]]`;return{"mCode":code,"newLogs":logs}}}else{return false}}else{return false}}if(!component.attribs[data.toAttribute]&&(_findDescendant(component,"oj-option")||_findDescendant(component,"template"))){return{"mCode":modelCode,"newLogs":[]}}return false}exports.getDom=getDom;exports.inManifestData=inManifestData;exports.checkFileUpdate=checkFileUpdate;exports.hasAttributesToIgnore=hasAttributesToIgnore;exports.hasExpressionAttributes=hasExpressionAttributes;exports.slotContentToAttribute=slotContentToAttribute;exports.commentSlotContent=commentSlotContent;exports.migrateTag=migrateTag;exports.migrateClassToTag=migrateClassToTag;exports.classNameToAttribute=classNameToAttribute;exports.handleSpecialCase=handleSpecialCase;exports.migrateAttributes=migrateAttributes;exports.migrateTagByAttribute=migrateTagByAttribute;exports.migrateDescendants=migrateDescendants;exports.replaceComponent=replaceComponent;exports.removeNode=removeNode;exports.migrateOptions=migrateOptions;
|
|
6
|
+
const{parseDocument,DomUtils}=require("htmlparser2");const render=require("dom-serializer").default;const logging=require("../utilities/logging");const self=require("./htmlUtils");const astUtils=require("./astUtils");function inManifestData(component){for(let obj of global.manifestData){if(obj.componentName&&obj.componentName===component.name){return obj}else if(obj.componentClass){const classAttr=_findAttribute(component,"class");const expRegex=/^(?:\[\[.*\]\]|\{\{.*\}\})$/;if(classAttr&&classAttr.attrValue&&expRegex.test(classAttr.attrValue)===false&&classAttr.attrValue.includes(obj.componentClass)){return obj}}}return false}function checkFileUpdate(){if(!global.fileIsUpdated){global.fileIsUpdated=true;global.migrationMetrics.files+=1;logging.logFilePath(global.filePath);return true}return false}function _getLabelFromDOMNodeHelper(child,slotName,cnodes=[],label=""){const expRegex=/[\[{].*?[\]}]/g;if(child.type&&child.type=="text"){if(/\S/.test(child.data)){if(label){if(expRegex.test(label)){if(expRegex.test(child.data.trim())){label=label.endsWith("()]]")?`${label.substring(0,label.length-2)} + ${child.data.trim().substring(2,child.data.length-2)}()]]`:`${label.substring(0,label.length-2)}() + ${child.data.trim().substring(2,child.data.length-2)}()]]`}else{label=label.endsWith("()]]")?`${label.substring(0,label.length-2)} + ' ${child.data.trim()}']]`:`${label.substring(0,label.length-2)}() + ' ${child.data.trim()}']]`}}else{if(expRegex.test(child.data.trim())){label=`[['${label} ' + ${child.data.trim().substring(2,child.data.length-2)}()]]`}else{label+=" "+child.data.trim()}}}else{label=child.data.trim()}cnodes.push(child)}}else if(child.type&&child.type=="tag"&&child.name.toUpperCase()=="OJ-BIND-TEXT"&&(!slotName&&!child.attribs.slot||slotName&&child.attribs.slot===slotName)){if(label){if(expRegex.test(label)){if(expRegex.test(child.attribs.value)){label=label.endsWith("()]]")?`${label.substring(0,label.length-2)}() + ${child.attribs.value.substring(2,child.attribs.value.length-2)}()]]`:`${label.substring(0,label.length-2)} + ${child.attribs.value.substring(2,child.attribs.value.length-2)}()]]`}else{label=label.endsWith("()]]")?`${label.substring(0,label.length-2)} + ' ${child.attribs.value}']]`:`${label.substring(0,label.length-2)}() + ' ${child.attribs.value}']]`}}else{if(expRegex.test(child.attribs.value)){label=`[['${label} ' + ${child.attribs.value.substring(2,child.attribs.value.length-2)}()]]`}else{label+=" "+child.attribs.value}}}else{label=child.attribs.value}cnodes.push(child)}else if(child.children&&!!child.children.length){if(child.attribs.slot===slotName||!slotName&&!child.attribs.slot){var regex=new RegExp("^[^\\dA-Za-z]*$");for(let c of child.children){label=_getLabelFromDOMNodeHelper(c,slotName,cnodes,label);if(c.type==="text"&®ex.test(c.data)){cnodes.push(c)}}cnodes.push(child)}}return label}function _getLabelFromDOMNode(node,slotName,label=""){if(node&&node.children&&!!node.children.length){let cnodes=[];for(let child of node.children){label=_getLabelFromDOMNodeHelper(child,slotName,cnodes,label)}if(cnodes.length>0){for(let node of cnodes){DomUtils.removeElement(node)}}}return label}function _findDescendant(component,tagName,slotName){if(component.children){for(let child of component.children){if(child.name===tagName){if(!slotName||child.attribs.slot&&child.attribs.slot===slotName){return child}}else if(child.children){const descendant=_findDescendant(child,tagName);if(descendant){return descendant}}}}return false}function getDom(text=""){return parseDocument(text,{withStartIndices:true,withEndIndices:true})}function _transferAttribute(fromTag,toTag,attribute,value,joined){if(fromTag.attribs[attribute]||joined&&fromTag.attribs[joined]){toTag.attribs[attribute]=value;delete fromTag.attribs[attribute]?fromTag.attribs[attribute]:fromTag.attribs[joined]}}function _hasAttributesToIgnoreHelper(dom,component,rule,key,parent,corePackTag,modelCode){if(rule.missPeerTag){const peerRule=rule.missPeerTag;for(let tagAttrSet of peerRule.tagAttributes){let tag=tagAttrSet.name;for(let attrSet of peerRule.matchValues){const peer=_findPeer(dom,component,tag,attrSet.ownAttribute,attrSet.peerAttribute);if(peer){let migratable=true;if(tagAttrSet.attribute){for(let set of tagAttrSet.attribute){migratable=_hasAttributesToIgnoreHelper(dom,peer,{"missAttribute":set});if(!migratable){break}}}if(tagAttrSet.missSlot&&migratable){migratable=_findAttribute(peer,"slot",peerRule.missSlot)?true:false}if(tagAttrSet.mapper&&migratable){migratable=modelCode?true:false}if(tagAttrSet.descendant&&migratable){migratable=!_hasAttributesToIgnoreHelper(dom,peer,tagAttrSet.descendant)}if(migratable){return false}}}}return true};if(rule.missDescendant){const descRule=rule.missDescendant;for(let tagAttrSet of descRule.tagAttributes){let tag=tagAttrSet.name;const desc=_findDescendant(component,tag,tagAttrSet.missSlot);if(desc){let migratable=true;if(tagAttrSet.attribute){for(let set of tagAttrSet.attribute){migratable=_hasAttributesToIgnoreHelper(dom,desc,{"missAttribute":set});if(!migratable){break}}}if(tagAttrSet.mapper&&migratable){migratable=modelCode?true:false}if(tagAttrSet.descendant&&migratable){migratable=!_hasAttributesToIgnoreHelper(dom,desc,tagAttrSet.descendant)}if(migratable){return false}}}return true};if(!rule.descendant&&component.attribs){if(rule.missAttribute){for(let key in rule.missAttribute){if(_findAttribute(component,key,rule.missAttribute[key])){return false}}if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(component.name,rule.missAttribute,"",parent,true)}return true}if(rule.attribute&&!rule.typeDefined){let attr=_findAttribute(component,rule.attribute);if(attr){rule.attribute=attr.attribute;if(rule.allowedValues&&!rule.allowedValues.includes(component.attribs[rule.attribute])||rule.prohibitedValues&&rule.prohibitedValues.includes(component.attribs[rule.attribute])||!rule.allowedValues&&!rule.prohibitedValues){if(corePackTag){checkFileUpdate();let value=(rule.allowedValues||rule.prohibitedValues)&&component.attribs[rule.attribute]?component.attribs[rule.attribute]:"";logging.logIgnoreComponent(component.name,rule.attribute,value,parent)}return true}}}else if(component.attribs[key]&&component.attribs[key]===rule[key]){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(component.name,key,rule[key],parent)}return true}}if(component.children){for(let j=0;j<component.children.length;j++){const child=component.children[j];if(child.type==="tag"){if(rule.descendant){if(child.name===rule.descendant){if(rule.attribute){const attr=_findAttribute(child,rule.attribute);if(attr){rule.attribute=attr.attribute;if(rule.allowedValues&&!rule.allowedValues.includes(child.attribs[rule.attribute])||rule.prohibitedValues&&rule.prohibitedValues.includes(child.attribs[rule.attribute])||!rule.prohibitedValues&&!rule.allowedValues){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(child.name,rule.attribute,child.attribs[rule.attribute],parent)}return true}}}else if(rule.slotName&&child.attribs["slot"]&&child.attribs["slot"]===rule.slotName){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(child.name,"slot",child.attribs[rule.attribute],parent)}return true}else if(rule.missSlot){if(!child.attribs["slot"]||child.attribs["slot"]!==rule.missSlot){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(child.name,"slot",rule.missSlot,parent,true)}return true}else if(child.attribs["slot"]&&child.attribs["slot"]===rule.missSlot){return false}}else{if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(rule.descendant,"","",parent)}return true}}else if(_hasAttributesToIgnoreHelper(dom,child,rule,key,parent,corePackTag)){return true}}if(!rule.attribute&&!rule.missAttribute&&_hasAttributesToIgnoreHelper(dom,child,rule,key,parent,corePackTag)){if(corePackTag){checkFileUpdate();logging.logIgnoreComponent(component.name,key,rule[key])}return true}}}}}function _toCamelCase(attribute){let camelCaseName=attribute.split("-");for(let i in camelCaseName){if(i>0){camelCaseName[i]=camelCaseName[i][0].toUpperCase()+camelCaseName[i].slice(1)}}camelCaseName=camelCaseName.join("");return camelCaseName}function _findAttribute(component,name,value=""){if(name){const joined=name.split("-").join("");if(DomUtils.hasAttrib(component,name)&&(!value||value===component.attribs[name])){return{attribute:name,attrValue:component.attribs[name]}}else if(DomUtils.hasAttrib(component,joined)&&(!value||value===component.attribs[joined])){return{attribute:joined,format:"camel",attrValue:component.attribs[joined]}}else if(DomUtils.hasAttrib(component,`:${name}`)&&(!value||value===component.attribs[`:${name}`])){return{attribute:`:${name}`,format:"colon",attrValue:component.attribs[`:${name}`]}}else if(DomUtils.hasAttrib(component,`:${joined}`)&&(!value||value===component.attribs[`:${joined}`])){return{attribute:`:${joined}`,format:"colonCamel",attrValue:component.attribs[`:${joined}`]}}}return false}function _findPeer(dom,component,peerTag,componentAttr,peerAttr){const attributeValue=_findAttribute(component,componentAttr).attrValue;if(attributeValue){const peer=DomUtils.findOne(elem=>elem.name===peerTag&&_findAttribute(elem,peerAttr,attributeValue),dom.children);return peer}}function hasAttributesToIgnore(dom,component,metadata,parent="",modelCode){const ignoreRuleData=metadata.ruleSetToIgnore;if(ignoreRuleData){for(let i=0;i<ignoreRuleData.length;i++){rule=ignoreRuleData[i];let arr=Object.keys(rule);let key=arr[0];if(!rule.type){if(rule.descendant){parent=component.name}if(_hasAttributesToIgnoreHelper(dom,component,rule,key,parent,metadata.corePackTag,modelCode)){return true}}}}return false}function hasExpressionAttributes(component,componentName,importantAttributes){const excludedAttributes=global.configData.excludeAttributeExpressions[componentName];const regex=new RegExp(`((\\{\\{)|(\\[\\[)).*((\\}\\})|(\\]\\]))`);if(global.jaf.options.dryRunMode&&importantAttributes){for(let attribute of importantAttributes){if(component.attribs[`:${attribute}`]){attribute=`:${attribute}`}if(regex.test(component.attribs[attribute])){if(checkFileUpdate()){logging.logAttributeExpression(componentName,attribute,component.attribs[attribute])}}}}if(excludedAttributes&&excludedAttributes.length){for(let i=0;i<excludedAttributes.length;i++){if(component.attribs[`:${excludedAttributes[i]}`]){excludedAttributes[i]=`:${excludedAttributes[i]}`}if(regex.test(component.attribs[excludedAttributes[i]])){checkFileUpdate();logging.logExcludeAttributeExpression(componentName,excludedAttributes[i],component.attribs[excludedAttributes[i]]);return true}}}return false}function commentSlotContent(component,data){const slotValue=data.slot;for(let child of component.children){if(child.name&&child.attribs.slot==slotValue){const cnode=getDom("<!-- "+render(child,{decodeEntities:false})+" -->");DomUtils.replaceElement(child,cnode);checkFileUpdate();return[logging.logCommentSlotContent(component.name,slotValue,child.name)]}};}function slotContentToAttribute(component,data,dom){let logs=[];for(let obj of data){let toNode;if(obj.toDescendant){const descendant=_findDescendant(component,obj.toDescendant,obj.slotNameTo?obj.slotNameTo:"");if(descendant){toNode=descendant}else{continue}}if(obj.fromDescendant){const descendant=_findDescendant(component,obj.fromDescendant,obj.slotNameFrom?obj.slotNameFrom:"");if(descendant){component=descendant}else{continue}}if(obj.toPeer||obj.fromPeer){const peer=_findPeer(dom,component,obj.toPeer?obj.toPeer:obj.fromPeer,obj.ownAttribute,obj.peerAttribute);if(peer){if(obj.toPeer){toNode=peer}else{component=peer}}else{continue}}if(!Object.keys(component.attribs).includes(obj.attribute)){component.attribs[obj.attribute]=_getLabelFromDOMNode(component,data.slotNameFrom);if(!component.attribs[obj.attribute]){delete component.attribs[obj.attribute]}else{checkFileUpdate();logs.push(logging.logSlotContentToAttribute(component.name,obj.attribute,component.attribs[obj.attribute]));if(toNode){_transferAttribute(component,toNode,obj.attribute,component.attribs[obj.attribute]);logs.push(logging.logAttributeTransfer(component.name,toNode.name,obj.attribute,toNode.attribs[obj.attribute]))}return logs}}}}function migrateTag(component,legacyComponentName,coreComponentName,isDescendant){component.name=coreComponentName;checkFileUpdate();global.migrationMetrics.components+=1;return logging.logTagMigration(legacyComponentName,coreComponentName,isDescendant)}function migrateClassToTag(component,legacyClassName,coreComponentName,isDescendant){const fromTag=component.name;component.name=coreComponentName;classAttr=_findAttribute(component,"class");if(classAttr){classNames=classAttr.attrValue.split(" ");index=classNames.indexOf(legacyClassName);classNames.splice(index,1);if(classNames.length===0){delete component.attribs[classAttr.attribute]}else{component.attribs[classAttr.attribute]=classNames.join(" ")}checkFileUpdate();global.migrationMetrics.components+=1;return logging.logClassToTagMigration(fromTag,legacyClassName,coreComponentName,isDescendant)}}function migrateTagByAttribute(component,data,isDescendant){for(const tagSet of data){if(tagSet.attribute){const attribute=_findAttribute(component,tagSet.attribute,tagSet.attributeValue);if(attribute){component.name=tagSet.toTag;checkFileUpdate();global.migrationMetrics.components+=1;return logging.logTagMigration(tagSet.fromTag,tagSet.toTag,isDescendant)}}else{component.name=tagSet.toTag;checkFileUpdate();global.migrationMetrics.components+=1;return logging.logTagMigration(tagSet.fromTag,tagSet.toTag)}}};function classNameToAttribute(component,data){let logs=[];data.forEach(obj=>{const from=obj.className;const to=obj.attributeValue;const attribute=obj.attribute;if(component.attribs.class){classNames=component.attribs.class.split(" ");if(typeof obj.className==="string"&&classNames.includes(obj.className)){index=classNames.indexOf(from);classNames.splice(index,1);component.attribs[attribute]=to;checkFileUpdate();logs.push(logging.logClassNameToAttribute(from,attribute,to))}else if(typeof obj.className==="object"&&from.every(c=>classNames.includes(c))){obj.className.forEach(cl=>{index=classNames.indexOf(cl);classNames.splice(index,1)});component.attribs[attribute]=to;checkFileUpdate();logs.push(logging.logClassNameToAttribute(from,attribute,to))}if(classNames.length==0)delete component.attribs.class;else component.attribs.class=classNames.join(" ")}});return logs}function migrateAttributes(component,data,dom){let toNode;let logs=[];for(const attributeSet in data){let camelFromAttribute="";let camelToAttribute="";let tagExists=true;let obj=data[attributeSet];if(obj.fromDescendant||obj.fromPeer||obj.toDescendant||obj.toPeer){tagExists=false;if(obj.fromDescendant||obj.toDescendant){const descendant=_findDescendant(component,obj.fromDescendant?obj.fromDescendant:obj.toDescendant,obj.slotName);if(component.children&&descendant){tagExists=true;if(obj.fromDescendant){toNode=component;component=descendant}else{toNode=descendant}}else{continue}}else{const peerTag=_findPeer(dom,component,obj.toPeer?obj.toPeer:obj.fromPeer,obj.ownAttribute,obj.peerAttribute);if(peerTag){tagExists=true;if(obj.fromPeer){toNode=component;component=peerTag}else{toNode=peerTag}}else{continue}}}if(tagExists){let hasDepAttribute=true;if(obj.depAttribute){hasDepAttribute=_findAttribute(component,obj.depAttribute)}const hasFromAttribute=_findAttribute(component,obj.fromAttribute,obj.fromValue);let fromAttribute=obj.fromAttribute;if(hasFromAttribute){fromAttribute=hasFromAttribute.attribute}let toAttribute=obj.toAttribute;if(hasFromAttribute.format){if(toAttribute){if(hasFromAttribute.format==="camel"){toAttribute=toAttribute.split("-").join("")}else if(hasFromAttribute.format==="colonCamel"){toAttribute=`:${toAttribute.split("-").join("")}`}else{toAttribute=`:${toAttribute}`}}}const hasToAttribute=_findAttribute(component,toAttribute);if(!(obj.fromAttribute===obj.toAttribute&&obj.fromValue===obj.toValue)){if(hasDepAttribute&&hasFromAttribute&&toAttribute&&(!hasToAttribute||fromAttribute===toAttribute)){if(obj.toValue){switch(hasFromAttribute.format){case"camel":camelFromAttribute=_toCamelCase(obj.fromAttribute);camelToAttribute=_toCamelCase(obj.toAttribute);break;case"colonCamel":camelFromAttribute=`:${_toCamelCase(obj.fromAttribute)}`;camelToAttribute=`:${_toCamelCase(obj.toAttribute)}`;break}component.attribs[camelToAttribute?camelToAttribute:toAttribute]=obj.toValue;checkFileUpdate();logs.push(logging.logAttributeMigration(camelFromAttribute?camelFromAttribute:fromAttribute,obj.fromValue,camelToAttribute?camelToAttribute:toAttribute,obj.toValue))}else{let fromValue=obj.fromValue?obj.fromValue:component.attribs[fromAttribute];switch(hasFromAttribute.format){case"camel":camelFromAttribute=_toCamelCase(obj.fromAttribute);camelToAttribute=_toCamelCase(obj.toAttribute);break;case"colonCamel":camelFromAttribute=`:${_toCamelCase(obj.fromAttribute)}`;camelToAttribute=`:${_toCamelCase(obj.toAttribute)}`;break}if(obj.fromValue){component.attribs[camelToAttribute?camelToAttribute:toAttribute]=""}else{component.attribs[camelToAttribute?camelToAttribute:toAttribute]=component.attribs[fromAttribute]}checkFileUpdate();logs.push(logging.logAttributeMigration(camelFromAttribute?camelFromAttribute:fromAttribute,fromValue,camelToAttribute?camelToAttribute:toAttribute))}if(fromAttribute!==toAttribute){delete component.attribs[fromAttribute]}}else if(hasDepAttribute&&hasFromAttribute&&!toAttribute){delete component.attribs[fromAttribute];switch(hasFromAttribute.format){case"camel":fromAttribute=_toCamelCase(obj.fromAttribute);break;case"colonCamel":fromAttribute=`:${_toCamelCase(obj.fromAttribute)}`;break}checkFileUpdate();logs.push(logging.logAttributeDeprecation(fromAttribute,obj.fromValue))}else if(hasDepAttribute&&!obj.fromAttribute&&obj.toAttribute&&!hasToAttribute){if(toNode){toNode.attribs[obj.toAttribute]=obj.toValue}else{component.attribs[obj.toAttribute]=obj.toValue}checkFileUpdate();logs.push(logging.logAttributeAddition(obj.toAttribute,obj.toValue,toNode?toNode.name:""))}}if(toNode&&hasFromAttribute){switch(hasFromAttribute.format){case"camel":camelToAttribute=_toCamelCase(obj.toAttribute);break;case"colonCamel":camelToAttribute=`:${_toCamelCase(obj.toAttribute)}`;break}checkFileUpdate();_transferAttribute(component,toNode,camelToAttribute?camelToAttribute:toAttribute,component.attribs[camelToAttribute?camelToAttribute:toAttribute],toAttribute);const toValue=toNode.attribs[camelToAttribute?camelToAttribute:toAttribute];logs.push(logging.logAttributeTransfer(component.name,toNode.name,camelToAttribute?camelToAttribute:toAttribute,toValue));if(obj.fromDescendant||obj.fromPeer){component=toNode}}}}return logs}function handleSpecialCase(code,componentName,data){let obj;let regex;if(data[1]){obj=data[1];regex=new RegExp(obj.fromString.split(" ").join("\\s*"),"igm");code=code.replace(regex,obj.toString)}obj=data[0];regex=new RegExp(obj.fromString.split(" ").join("\\s*"),"igm");code=code.replace(regex,obj.toString);return code}function _migrateDescendantsHelper(dom,children,componentMetadata,parent,compCount,modelCode,fileMigratedCount,revertMig,logs=[]){let descendants=[];for(let child of children){let newLogs=[];if(child.name===componentMetadata.legacyTag&&!revertMig){let componentCopy=child.cloneNode(true);descendants.push({original:child,copy:componentCopy});if(!hasAttributesToIgnore(dom,child,componentMetadata,parent)&&!hasExpressionAttributes(child,componentMetadata.legacyTag,componentMetadata.importantAttributes)){if(componentMetadata["migrateTagByAttribute"]){newLogs.push(migrateTagByAttribute(child,componentMetadata["migrateTagByAttribute"],true))}else{newLogs.push(migrateTag(child,componentMetadata.legacyTag,componentMetadata.corePackTag,true))}for(let key in componentMetadata){if(typeof self[key]=="function"&&key!="migrateTagByAttribute"&&key!="migrateImports"&&key!="migrateOptions"){newLogs=newLogs.concat(self[key](child,componentMetadata[key]))}else if(key==="migrateOptions"){let info=migrateOptions(child,componentMetadata[key],modelCode,fileMigratedCount,componentMetadata.legacyTag);if(!modelCode&&info){logging.logMapperRequest(componentMetadata.legacyTag);revertMig=true}else if(info){if(info.revert){revertMig=true}else{modelCode=info.mCode;newLogs=newLogs.concat(info.newLogs)}}}}if(!compCount.migrated[componentMetadata.legacyTag]){compCount.migrated[componentMetadata.legacyTag]=1}else{compCount.migrated[componentMetadata.legacyTag]+=1}if(revertMig){for(let obj of descendants){DomUtils.replaceElement(obj.original,obj.copy);global.migrationMetrics.components-=1;compCount.migrated[componentMetadata.legacyTag]-=1;if(compCount.migrated[componentMetadata.legacyTag]===0){delete compCount.migrated[componentMetadata.legacyTag]}if(compCount.skipped[componentMetadata.legacyTag]){compCount.skipped[componentMetadata.legacyTag]+=1}else{compCount.skipped[componentMetadata.legacyTag]=1}}}}}else{if(child.name===componentMetadata.legacyTag&&revertMig){if(compCount.skipped[componentMetadata.legacyTag]){compCount.skipped[componentMetadata.legacyTag]+=1}else{compCount.skipped[componentMetadata.legacyTag]=1}}else if(child.children){returnValue=_migrateDescendantsHelper(dom,child.children,componentMetadata,parent,compCount);newLogs=newLogs.concat(returnValue.logs);compCount=returnValue.count;revertMig=returnValue.revert}}if(!revertMig){logs=logs.concat(newLogs)}}return{logs:logs,count:compCount,mCode:modelCode,revert:revertMig}}function migrateDescendants(dom,component,metadata,modelCode,fileComponentsInfo,logs=[]){let compCount={migrated:{},skipped:{}};const data=metadata.migrateDescendants;let returnValue=false;let revertMig;for(let metaName of data){let componentMetadata=require("../metadata/"+metaName+".json");let fileMigratedCount=fileComponentsInfo.migrated[componentMetadata.legacyTag]?fileComponentsInfo.migrated[componentMetadata.legacyTag]:0;if(component.children){returnValue=_migrateDescendantsHelper(dom,component.children,componentMetadata,metadata.legacyTag,compCount,modelCode,fileMigratedCount,revertMig);if(returnValue){logs=logs.concat(returnValue.logs);modelCode=returnValue.mCode;revertMig=returnValue.revert;compCount=returnValue.count}}}return{logs:logs,count:compCount,mCode:modelCode}}function removeNode(component){DomUtils.removeElement(component);return logging.logComponentRemoved(component.name)}function replaceComponent(fromNode,data){for(let obj of data){let descendant=_findDescendant(fromNode,obj.descendant);if(descendant){DomUtils.replaceElement(fromNode,descendant);return logging.logComponentReplacement(fromNode.name,descendant.name)}}}function migrateOptions(component,data,modelCode,compCount,legacyTag){if(modelCode){if(!_findAttribute(component,data.toAttribute)&&(_findDescendant(component,"oj-option")||_findDescendant(component,"template"))){let optionsInfo=[];let tag=legacyTag.slice(3);tag=tag[0].toUpperCase()+tag.slice(1);let varName=`optionsMig${tag}${compCount}`;let template;for(let child of component.children){if(child.name==="oj-option"){for(let ignoreAttr of data.ignoreAttribute){if(child.attribs[ignoreAttr]){logging.logIgnoreComponent(child.name,ignoreAttr,"",legacyTag);return{revert:true}}}child.attribs.label=_getLabelFromDOMNode(child,"");if(child.children){for(let c of child.children){if(c.name==="span"||c.name==="div"){if(c.attribs.slot&&c.attribs.class){child.attribs[c.attribs.slot]={"class":c.attribs.class}}}else if(c.name==="oj-avatar"){if(c.attribs.slot){if(c.attribs.src&&c.attribs.role){child.attribs[c.attribs.slot]={"type":c.attribs.role,"src":c.attribs.src}}else{child.attribs[c.attribs.slot]={"class":c.attribs.class}}}}}}optionsInfo.push(child.attribs);DomUtils.removeElement(child)}else if(child.name==="oj-bind-for-each"){template=child.attribs.data.slice(2,child.attribs.data.length-2);let optionComp=_findDescendant(child,"oj-option");for(let ignoreAttr of data.ignoreAttribute){if(optionComp.attribs[ignoreAttr]){logging.logIgnoreComponent(optionComp.name,ignoreAttr,"",legacyTag);return{revert:true}}}optionComp.attribs.label=_getLabelFromDOMNode(optionComp,"");if(optionComp.children){for(let c of optionComp.children){if(c.name==="span"||c.name==="div"){if(c.attribs.slot&&c.attribs.class){optionComp.attribs[c.attribs.slot]={"class":c.attribs.class}}}else if(c.name==="oj-avatar"){if(c.attribs.slot){if(c.attribs.src&&c.attribs.role){optionComp.attribs[c.attribs.slot]={"type":c.attribs.role,"src":c.attribs.src}}else{optionComp.attribs[c.attribs.slot]={"class":c.attribs.class}}}}}}optionsInfo.push(optionComp.attribs);DomUtils.removeElement(child)}}let varValue=optionsInfo;let info=astUtils.migrateOptionsModel(data,varName,varValue,modelCode,template);if(info){if(info.emptyModel){logging.logIgnoreComponent(legacyTag,"","","","To migrate data driven component, model file provided needs a class where a new variable can be created");return{revert:true}}else{varName=info.varName;let code=info.code;let logs=info.logs;component.attribs[data.toAttribute]=`[[${varName}]]`;return{"mCode":code,"newLogs":logs}}}else{return false}}else{return false}}if(!component.attribs[data.toAttribute]&&(_findDescendant(component,"oj-option")||_findDescendant(component,"template"))){return{"mCode":modelCode,"newLogs":[]}}return false}exports.getDom=getDom;exports.inManifestData=inManifestData;exports.checkFileUpdate=checkFileUpdate;exports.hasAttributesToIgnore=hasAttributesToIgnore;exports.hasExpressionAttributes=hasExpressionAttributes;exports.slotContentToAttribute=slotContentToAttribute;exports.commentSlotContent=commentSlotContent;exports.migrateTag=migrateTag;exports.migrateClassToTag=migrateClassToTag;exports.classNameToAttribute=classNameToAttribute;exports.handleSpecialCase=handleSpecialCase;exports.migrateAttributes=migrateAttributes;exports.migrateTagByAttribute=migrateTagByAttribute;exports.migrateDescendants=migrateDescendants;exports.replaceComponent=replaceComponent;exports.removeNode=removeNode;exports.migrateOptions=migrateOptions;
|