import Set from 'typescript-collections/dist/lib/Set' ; import * as vscode from 'vscode' ; import { ContextKeys } from '../../constants/contextKeys' ; import { GeneralInfo } from '../../types/generalInfo' ; import { IIssueKey } from '../../types/issueKey' ; import { ILicenseKey } from '../../types/licenseKey' ; import { Severity } from '../../types/severity' ; import { IconsPaths } from '../../utils/iconsPaths' ; export class DependenciesTreeNode extends vscode . TreeItem { private _children : DependenciesTreeNode [ ] = [ ] ; private _licenses : Set < ILicenseKey > = new Set ( license => license . licenseName ) ; private _issues : Set < IIssueKey > = new Set ( issue => issue . issue_id ) ; private _topSeverity : Severity ; constructor ( protected _generalInfo , collapsibleState ? , private _parent ? , contextValue ? ) { super ( _generalInfo . artifactId , collapsibleState ) ; this . _topSeverity = Severity . Normal ; this . iconPath = IconsPaths . NORMAL_SEVERITY ; this . description = this . generalInfo . version ; this . tooltip = this . componentId ; if ( contextValue === undefined ) { this . contextValue = ContextKeys . SHOW_IN_PROJECT_DESC_ENABLED ; } if ( _parent ) { _parent . children . push ( this ) ; } } public get componentId ( ) : string { return this . generalInfo . getComponentId ( ) ; } public get generalInfo ( ) : GeneralInfo { return this . _generalInfo ; } public set generalInfo ( generalInfo ) { this . _generalInfo = generalInfo ; this . description = this . generalInfo . version ; this . tooltip = this . componentId ; } public get parent ( ) : DependenciesTreeNode | undefined { return this . _parent ; } public set parent ( parent ) { this . _parent = parent ; } return { Set < License > } public get licenses ( ) : Set < ILicenseKey > { return this . _licenses ; } public set licenses ( value ) { this . _licenses = value ; } public get issues ( ) : Set < IIssueKey > { return this . _issues ; } public set issues ( value ) { this . _issues = value ; } public get children ( ) : DependenciesTreeNode [ ] { return this . _children ; } public set children ( value ) { this . _children = value ; } public get topSeverity ( ) : Severity { return this . _topSeverity ; } public set topSeverity ( severity ) { this . _topSeverity = severity ; } public isDependenciesTreeRoot ( ) { return ! ! this . generalInfo . path ; } public addChild ( child ) { this . children . push ( child ) ; child . parent = this ; } public shallowClone ( ) { let clone = new DependenciesTreeNode ( this . generalInfo , this . collapsibleState ) ; this . fillShallowClone ( clone ) ; return clone ; } public processTreeIssues ( ) { this . setIssuesComponent ( ) ; this . children . forEach ( child => { this . issues . union ( child . processTreeIssues ( ) ) ; this . setTopSeverity ( child ) ; } ) ; this . sortChildren ( ) ; return this . issues ; } protected fillShallowClone ( clone ) { clone . label = this . label ; clone . licenses = this . licenses ; clone . issues = this . issues ; clone . topSeverity = this . topSeverity ; clone . contextValue = this . contextValue ; } private setIssuesComponent ( ) { this . issues . forEach ( issue => { issue . component = this . componentId ; } ) ; } private setTopSeverity ( child ) { if ( child . topSeverity > this . topSeverity ) { this . _topSeverity = child . topSeverity ; } } public getViolatedLicenses ( ) { let violatedLicenses = new Map ( ) ; this . recursivelySetViolatedLicenses ( violatedLicenses ) ; return violatedLicenses ; } private recursivelySetViolatedLicenses ( violatedLicenses ) { this . licenses . forEach ( licenseKey => { if ( licenseKey . violated ) { let components = violatedLicenses . get ( licenseKey . licenseName ) ; if ( ! components ) { components = new Set < string > ( ) ; } components . add ( this . componentId ) ; violatedLicenses . set ( licenseKey . licenseName , components ) ; } } ) ; this . _children . forEach ( child => { child . recursivelySetViolatedLicenses ( violatedLicenses ) ; } ) ; } private sortChildren ( ) { this . children . sort ( ( lhs , rhs ) => lhs . issues . size ( ) - rhs . issues . size ( ) ) . sort ( ( lhs , rhs ) => rhs . children . length - lhs . children . length ) . sort ( ( lhs , rhs ) => rhs . topSeverity - lhs . topSeverity ) ; } }