import { some , includes } from "lodash" ; import { XMLElement } from "@xml-tools/ast" ; import { resolveXMLNS } from "@ui5-language-assistant/logic-utils" ; import { UI5SemanticModel } from "@ui5-language-assistant/semantic-model-types" ; import { validations , buildMessage , } from "@ui5-language-assistant/user-facing-text" ; import { NonStableIDIssue } from "../../../api" ; import { isPossibleCustomClass , isKnownUI5Class , } from "../../utils/ui5-classes" ; import { CORE_NS } from "../../utils/special-namespaces" ; const { NON_STABLE_ID } = validations ; export function validateNonStableId ( xmlElement , model ) { if ( xmlElement . name === null || xmlElement . syntax . openName === undefined ) { return [ ] ; } if ( isAllowedListedTag ( xmlElement ) ) { return [ ] ; } if ( ! isPossibleCustomClass ( xmlElement ) && ! isKnownUI5Class ( xmlElement , model ) ) { return [ ] ; } if ( hasNonAdaptableMetaData ( xmlElement ) || hasNonAdaptableTreeMetaData ( xmlElement ) ) { return [ ] ; } if ( isElementWithStableID ( xmlElement ) ) { return [ ] ; } const nonStableIDIssue = { kind : "NonStableIDIssue" , message : buildMessage ( NON_STABLE_ID . msg , xmlElement . name ) , severity : "error" , offsetRange : { start : xmlElement . syntax . openName . startOffset , end : xmlElement . syntax . openName . endOffset , } , } ; return [ nonStableIDIssue ] ; } function isAllowedListedTag ( xmlElement ) { const rootAllowedListedExceptions = { "sap.ui.core.mvc" : [ "View" ] , "sap.ui.core" : [ "View" , "FragmentDefinition" ] , } ; if ( xmlElement . parent . type === "XMLDocument" ) { const resolvedXMLNS = resolveXMLNS ( xmlElement ) ; const exceptionsForResolvedXMLNS = rootAllowedListedExceptions [ resolvedXMLNS ] ; if ( includes ( exceptionsForResolvedXMLNS , xmlElement . name ) ) { return true ; } } const coreNsAllowedListedExceptions = [ "Fragment" , "CustomData" , "ExtensionPoint" , ] ; const isCoreNsAllowedListed = resolveXMLNS ( xmlElement ) === CORE_NS && includes ( coreNsAllowedListedExceptions , xmlElement . name ) ; return isCoreNsAllowedListed ; } function hasNonAdaptableMetaData ( xmlElement ) { return some ( xmlElement . attributes , ( attribute ) => attribute . key === "sap.ui.dt:designtime" && attribute . value === "not-adaptable" ) ; } function hasNonAdaptableTreeMetaData ( xmlElement ) { let currElement = xmlElement ; while ( currElement . parent . type !== "XMLDocument" ) { const hasNonAdaptableTreeMetaData = some ( currElement . attributes , ( attribute ) => attribute . key === "sap.ui.dt:designtime" && attribute . value === "not-adaptable-tree" ) ; if ( hasNonAdaptableTreeMetaData ) { return true ; } currElement = currElement . parent ; } return false ; } function isElementWithStableID ( xmlElement ) { return some ( xmlElement . attributes , ( attribute ) => attribute . key === "id" && attribute . value !== null && /\S/ . test ( attribute . value ) ) ; }