import { SQ_QUALIFIER_FILE , SQ_QUALIFIER_UNIT_TEST_FILE , } from "../services/sonarqube/measures/api/SonarQubeMeasureResponse" ; import Metric from "./Metric" ; export class TreeElement { private static sortByNameAndType ( a , b ) { if ( a . isFile ( ) === b . isFile ( ) ) { return a . name . localeCompare ( b . name ) ; } return a . isFile ( ) ? 1 : - 1 ; } public readonly key : string ; public measures : MeasureList ; public name : string ; public readonly path : string ; public parent ? : TreeElement ; public readonly children : TreeElement [ ] ; public readonly qualifier : string ; public constructor ( key , measures , name , path , qualifier , parent ? ) { this . key = key ; this . measures = measures ; this . name = name ; this . path = path ; this . qualifier = qualifier ; this . parent = parent ; this . children = [ ] ; } public isFile ( ) { return ( this . qualifier === SQ_QUALIFIER_FILE || this . qualifier === SQ_QUALIFIER_UNIT_TEST_FILE ) ; } public getSortedChildren ( ) { return this . children . sort ( TreeElement . sortByNameAndType . bind ( this ) ) ; } public replaceChildByKey ( key , replaceChild ) { for ( let index = 0 ; index < this . children . length ; index ++ ) { if ( key === this . children [ index ] . key ) { replaceChild . name = `${ this . children [ index ] . name }/${ replaceChild . name }` ; this . children [ index ] = replaceChild ; break ; } } } public getMeasureValue ( metric ) { if ( this . measures && metric . id in this . measures ) { return this . measures [ metric . id ] ; } else { return null ; } } public clone ( ) { const treeElement = new TreeElement ( this . key , this . measures , this . name , this . path , this . qualifier , this . parent ) ; for ( const child of this . children ) { treeElement . children . push ( child . clone ( ) ) ; } return treeElement ; } }