import { TreeElement } from "../../../../classes/TreeElement" ; export default class SonarQubeOptimizeStructureService { public optimize ( element ) { if ( element . isFile ( ) || element . children . length === 0 ) { return ; } let checkAgain = false ; for ( let index = 0 ; index < element . children . length ; index ++ ) { checkAgain = this . processChild ( element . children , index ) ; } if ( checkAgain ) { this . optimize ( element ) ; } } private processChild ( children , index ) { const child = children [ index ] ; if ( child . isFile ( ) ) { return false ; } this . optimize ( child ) ; if ( child . children . length === 1 && ! child . children [ 0 ] . isFile ( ) ) { child . children [ 0 ] . parent = child . parent ; if ( child . parent ) { child . parent . replaceChildByKey ( child . key , child . children [ 0 ] ) ; } } if ( child . children . length === 0 ) { children . splice ( index , 1 ) ; return true ; } return false ; } }