import * as fxp from 'fast-xml-parser' ; import * as path from 'path' ; import * as file from '../../utils/file' ; import { IssueType , Issue , File } from "./models" ; param filePath export function parsefile ( filePath ) { const xml = file . readFileSync ( filePath ) ; const json = fxp . parse ( xml , { attrNodeName : 'attributes' , ignoreAttributes : false , parseAttributeValue : true } ) ; const fileIssueLists = [ ] ; const issueTypes = [ ] ; for ( let i = 0 ; i < json . Report . IssueTypes . IssueType . length ; i ++ ) { const item = json . Report . IssueTypes . IssueType [ i ] ; const issueType = { category : item . attributes [ "@_Category" ] , categoryId : item . attributes [ "@_CategoryId" ] , description : item . attributes [ "@_Description" ] , id : item . attributes [ "@_Id" ] , severity : item . attributes [ "@_Severity" ] , wikiUrl : item . attributes [ "@_WikiUrl" ] } ; issueTypes . push ( issueType ) ; } const addIssue = function ( item ) : void { const issue = { file : item . attributes [ "@_File" ] . replace ( /\\/g , path . sep ) , line : parseInt ( item . attributes [ "@_Line" ] ) , message : item . attributes [ "@_Message" ] , offset : { start : parseInt ( item . attributes [ "@_Offset" ] . split ( '-' ) [ 0 ] ) , end : parseInt ( item . attributes [ "@_Offset" ] . split ( '-' ) [ 1 ] ) , } , typeId : item . attributes [ "@_TypeId" ] , issueType : issueTypes . filter ( x => x . id === item . attributes [ "@_TypeId" ] ) [ 0 ] } ; let added = false ; for ( let i = 0 ; i < fileIssueLists . length ; i ++ ) { if ( fileIssueLists [ i ] . path === issue . file ) { fileIssueLists [ i ] . issues . push ( issue ) ; added = true ; break ; } } if ( ! added ) { fileIssueLists . push ( { path : issue . file , issues : [ issue ] } ) ; } } ; if ( json . Report . Issues . Project . Issue ) { for ( let i = 0 ; i < json . Report . Issues . Project . Issue . length ; i ++ ) { const item = json . Report . Issues . Project . Issue [ i ] ; addIssue ( item ) ; } } else { for ( let i = 0 ; i < json . Report . Issues . Project . length ; i ++ ) { for ( let j = 0 ; j < json . Report . Issues . Project [ i ] . Issue . length ; j ++ ) { const item = json . Report . Issues . Project [ i ] . Issue [ j ] ; addIssue ( item ) ; } } } return fileIssueLists ; }