11import { X2jOptions , XMLParser } from 'fast-xml-parser' ;
2+ import { number } from 'fp-ts' ;
23import * as fs from 'fs' ;
34
45/**
@@ -75,7 +76,7 @@ type metric_type = {
7576 * should be 'number' but we don't want to ask the XML parser to parse
7677 * numbers
7778 */
78- '@_count' : string ;
79+ '@_count' : number ;
7980 '@_ratio' : number ;
8081} ;
8182type metric_kind_type =
@@ -170,6 +171,21 @@ type message_kind_type =
170171 | 'undetermined_cov'
171172 | 'exclusion' ;
172173
174+ /**
175+ * Process attribute values to convert certain attributes to number values.
176+ *
177+ * @param name - attribute name
178+ * @param value - unprocessed value
179+ * @returns processed value
180+ */
181+ function attributeValueProcessor ( name : string , value : string ) : unknown {
182+ return [ 'scope_line' , 'count' , 'ratio' , 'num' , 'column_begin' , 'column_end' , 'id' ] . includes (
183+ name ,
184+ )
185+ ? Number . parseInt ( value )
186+ : value ;
187+ }
188+
173189/**
174190 *
175191 * @param path - path to GNATcoverage index.xml report
@@ -188,6 +204,8 @@ export function parseGnatcovIndexXml(path: string): CovIndex {
188204 isArray : ( _ , jPath ) => {
189205 return ( [ ] as string [ ] ) . indexOf ( jPath ) !== - 1 ;
190206 } ,
207+ trimValues : false ,
208+ attributeValueProcessor : attributeValueProcessor ,
191209 } ;
192210 const parser = new XMLParser ( options ) ;
193211 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -225,7 +243,9 @@ export function parseGnatcovFileXml(path: string): source_type {
225243 * original content.
226244 */
227245 trimValues : false ,
246+ attributeValueProcessor : attributeValueProcessor ,
228247 } ;
248+
229249 const parser = new XMLParser ( options ) ;
230250 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
231251 const parseResult = parser . parse ( fileContentAsBuffer ) ;
0 commit comments