import { AxeResult , AxeResultsList } from 'axe-result-converter' ; import { injectable } from 'inversify' ; import { identity , sortBy } from 'lodash' ; import { BaselineFileContent , BaselineResult , UrlNormalizer } from './baseline-types' ; @ injectable ( ) export class BaselineGenerator { public generateBaseline ( axeResultsList , urlNormalizer ? ) { const combinedViolations = axeResultsList . values ( ) ; const unsortedBaselineResults = combinedViolations . map ( ( result ) => this . combinedViolationToBaselineResult ( result , urlNormalizer ) ) ; const sortedBaselineResults = this . sortBaselineResults ( unsortedBaselineResults ) ; const baselineContent = { metadata : { fileFormatVersion : '1' } , results : sortedBaselineResults , } ; return baselineContent ; } private combinedViolationToBaselineResult ( result , urlNormalizer ? ) { const node = result . junctionNode ; if ( node == null ) { throw new Error ( 'Invalid input result; does not contain a junctionNode' ) ; } const cssSelector = node . selectors . find ( ( s ) => s . type === 'css' ) ?. selector ; const xpathSelector = node . selectors . find ( ( s ) => s . type === 'xpath' ) ?. selector ; const urls = result . urls . map ( urlNormalizer ?? identity ) . sort ( ) ; if ( cssSelector == null ) { throw new Error ( 'Invalid input result; does not contain a css selector' ) ; } const baselineResult = { cssSelector , htmlSnippet : node . html , rule : result . id , urls , } ; if ( xpathSelector != null ) { baselineResult . xpathSelector = xpathSelector ; } return baselineResult ; } private sortBaselineResults ( results ) { return sortBy ( results , [ 'rule' , 'cssSelector' , 'xpathSelector' , 'htmlSnippet' ] ) ; } }