@@ -21,50 +21,48 @@ export interface ConfigModel {
2121 toFormattedOutput ( format : OutputFormat ) : string ;
2222}
2323
24- export type ConfigContext = {
25- config : CodeAnalyzerConfig ;
26- core : CodeAnalyzer ;
27- rules : RuleSelection ;
28- }
29-
30- export type ConfigModelGeneratorFunction = ( relevantEngines : Set < string > , userContext : ConfigContext , defaultContext : ConfigContext ) => ConfigModel ;
24+ export type ConfigModelConstructor = new ( config : CodeAnalyzerConfig , codeAnalyzer : CodeAnalyzer , userRules : RuleSelection , allDefaultRules : RuleSelection , relevantEngines : Set < string > ) => ConfigModel ;
3125
3226export class AnnotatedConfigModel implements ConfigModel {
33- private readonly relevantEngines : Set < string > ;
34- private readonly userContext : ConfigContext ;
35- private readonly defaultContext : ConfigContext ;
36-
37- private constructor ( relevantEngines : Set < string > , userContext : ConfigContext , defaultContext : ConfigContext ) {
27+ private readonly config : CodeAnalyzerConfig ; // TODO: It would be nice if we updated the CodeAnalyzer (in our core module) to just return its CodeAnalyzerConfig with a getter so we didn't need to pass it around
28+ private readonly codeAnalyzer : CodeAnalyzer ;
29+ private readonly userRules : RuleSelection ;
30+ private readonly allDefaultRules : RuleSelection ;
31+ private readonly relevantEngines : Set < string > ;
32+
33+ constructor ( config : CodeAnalyzerConfig , codeAnalyzer : CodeAnalyzer , userRules : RuleSelection , allDefaultRules : RuleSelection , relevantEngines : Set < string > ) {
34+ this . config = config ;
35+ this . codeAnalyzer = codeAnalyzer ;
36+ this . userRules = userRules ;
37+ this . allDefaultRules = allDefaultRules ;
3838 this . relevantEngines = relevantEngines ;
39- this . userContext = userContext ;
40- this . defaultContext = defaultContext ;
4139 }
4240
4341 toFormattedOutput ( format : OutputFormat ) : string {
4442 // istanbul ignore else: Should be impossible
4543 if ( format === OutputFormat . STYLED_YAML ) {
46- return new StyledYamlFormatter ( this . relevantEngines , this . userContext , this . defaultContext ) . toYaml ( ) ;
44+ return new StyledYamlFormatter ( this . config , this . codeAnalyzer , this . userRules , this . allDefaultRules , this . relevantEngines ) . toYaml ( ) ;
4745 } else if ( format === OutputFormat . RAW_YAML ) {
48- return new PlainYamlFormatter ( this . relevantEngines , this . userContext , this . defaultContext ) . toYaml ( ) ;
46+ return new PlainYamlFormatter ( this . config , this . codeAnalyzer , this . userRules , this . allDefaultRules , this . relevantEngines ) . toYaml ( ) ;
4947 } else {
5048 throw new Error ( `Unsupported` )
5149 }
5250 }
53-
54- public static fromSelection ( relevantEngines : Set < string > , userContext : ConfigContext , defaultContext : ConfigContext ) : AnnotatedConfigModel {
55- return new AnnotatedConfigModel ( relevantEngines , userContext , defaultContext ) ;
56- }
5751}
5852
5953abstract class YamlFormatter {
54+ private readonly config : CodeAnalyzerConfig ;
55+ private readonly codeAnalyzer : CodeAnalyzer ;
56+ private readonly userRules : RuleSelection ;
57+ private readonly allDefaultRules : RuleSelection ;
6058 private readonly relevantEngines : Set < string > ;
61- private readonly userContext : ConfigContext ;
62- private readonly defaultContext : ConfigContext ;
6359
64- protected constructor ( relevantEngines : Set < string > , userContext : ConfigContext , defaultContext : ConfigContext ) {
60+ protected constructor ( config : CodeAnalyzerConfig , codeAnalyzer : CodeAnalyzer , userRules : RuleSelection , allDefaultRules : RuleSelection , relevantEngines : Set < string > ) {
61+ this . config = config ;
62+ this . codeAnalyzer = codeAnalyzer ;
63+ this . userRules = userRules ;
64+ this . allDefaultRules = allDefaultRules ;
6565 this . relevantEngines = relevantEngines ;
66- this . userContext = userContext ;
67- this . defaultContext = defaultContext ;
6866 }
6967
7068 protected abstract toYamlComment ( commentText : string ) : string
@@ -109,20 +107,20 @@ abstract class YamlFormatter {
109107 }
110108
111109 const commentText : string = getMessage ( BundleName . ConfigModel , 'template.modified-from' , [ defaultValueJson ] ) ;
112- resolvedValue = replaceAbsolutePathsWithRelativePathsWherePossible ( resolvedValue , this . userContext . config . getConfigRoot ( ) + path . sep ) ;
110+ resolvedValue = replaceAbsolutePathsWithRelativePathsWherePossible ( resolvedValue , this . config . getConfigRoot ( ) + path . sep ) ;
113111 return this . toYamlUncheckedFieldWithInlineComment ( fieldName , resolvedValue , commentText ) ;
114112 }
115113
116114 toYaml ( ) : string {
117- const topLevelDescription : ConfigDescription = this . userContext . config . getConfigDescription ( ) ;
115+ const topLevelDescription : ConfigDescription = this . config . getConfigDescription ( ) ;
118116 return this . toYamlSectionHeadingComment ( topLevelDescription . overview ) + '\n' +
119117 '\n' +
120118 this . toYamlComment ( topLevelDescription . fieldDescriptions . config_root . descriptionText ) + '\n' +
121- this . toYamlFieldUsingFieldDescription ( 'config_root' , this . userContext . config . getConfigRoot ( ) ,
119+ this . toYamlFieldUsingFieldDescription ( 'config_root' , this . config . getConfigRoot ( ) ,
122120 topLevelDescription . fieldDescriptions . config_root ) + '\n' +
123121 '\n' +
124122 this . toYamlComment ( topLevelDescription . fieldDescriptions . log_folder . descriptionText ) + '\n' +
125- this . toYamlFieldUsingFieldDescription ( 'log_folder' , this . userContext . config . getLogFolder ( ) ,
123+ this . toYamlFieldUsingFieldDescription ( 'log_folder' , this . config . getLogFolder ( ) ,
126124 topLevelDescription . fieldDescriptions . log_folder ) + '\n' +
127125 '\n' +
128126 this . toYamlComment ( topLevelDescription . fieldDescriptions . rules . descriptionText ) + '\n' +
@@ -135,13 +133,13 @@ abstract class YamlFormatter {
135133 }
136134
137135 private toYamlRuleOverrides ( ) : string {
138- if ( this . userContext . rules . getCount ( ) === 0 ) {
136+ if ( this . userRules . getCount ( ) === 0 ) {
139137 const commentText : string = getMessage ( BundleName . ConfigModel , 'template.yaml.no-rules-selected' ) ;
140138 return `rules: {} ${ this . toYamlComment ( commentText ) } ` ;
141139 }
142140
143141 let yamlCode : string = 'rules:\n' ;
144- for ( const engineName of this . userContext . rules . getEngineNames ( ) ) {
142+ for ( const engineName of this . userRules . getEngineNames ( ) ) {
145143 yamlCode += '\n' ;
146144 yamlCode += indent ( this . toYamlRuleOverridesForEngine ( engineName ) , 2 ) + '\n' ;
147145 }
@@ -153,7 +151,7 @@ abstract class YamlFormatter {
153151 [ engineName . toUpperCase ( ) ] ) ;
154152 let yamlCode : string = this . toYamlSectionHeadingComment ( engineConfigHeader ) + '\n' ;
155153 yamlCode += `${ engineName } :\n` ;
156- for ( const userRule of this . userContext . rules . getRulesFor ( engineName ) ) {
154+ for ( const userRule of this . userRules . getRulesFor ( engineName ) ) {
157155 const defaultRule : Rule | null = this . getDefaultRuleFor ( engineName , userRule . getName ( ) ) ;
158156 yamlCode += indent ( this . toYamlRuleOverridesForRule ( userRule , defaultRule ) , 2 ) + '\n' ;
159157 }
@@ -162,7 +160,7 @@ abstract class YamlFormatter {
162160
163161 private getDefaultRuleFor ( engineName : string , ruleName : string ) : Rule | null {
164162 try {
165- return this . defaultContext . rules . getRule ( engineName , ruleName ) ;
163+ return this . allDefaultRules . getRule ( engineName , ruleName ) ;
166164 } catch ( e ) {
167165 // istanbul ignore next
168166 return null ;
@@ -191,8 +189,8 @@ abstract class YamlFormatter {
191189 }
192190
193191 private toYamlEngineOverridesForEngine ( engineName : string ) : string {
194- const engineConfigDescriptor : ConfigDescription = this . userContext . core . getEngineConfigDescription ( engineName ) ;
195- const userEngineConfig : EngineConfig = this . userContext . core . getEngineConfig ( engineName ) ;
192+ const engineConfigDescriptor : ConfigDescription = this . codeAnalyzer . getEngineConfigDescription ( engineName ) ;
193+ const userEngineConfig : EngineConfig = this . codeAnalyzer . getEngineConfig ( engineName ) ;
196194
197195 let yamlCode : string = '\n' +
198196 this . toYamlSectionHeadingComment ( engineConfigDescriptor . overview ) + '\n' +
@@ -212,8 +210,8 @@ abstract class YamlFormatter {
212210}
213211
214212class PlainYamlFormatter extends YamlFormatter {
215- constructor ( relevantEngines : Set < string > , userContext : ConfigContext , defaultContext : ConfigContext ) {
216- super ( relevantEngines , userContext , defaultContext ) ;
213+ constructor ( config : CodeAnalyzerConfig , codeAnalyzer : CodeAnalyzer , userRules : RuleSelection , allDefaultRules : RuleSelection , relevantEngines : Set < string > ) {
214+ super ( config , codeAnalyzer , userRules , allDefaultRules , relevantEngines ) ;
217215 }
218216
219217 protected toYamlComment ( commentText : string ) : string {
@@ -222,8 +220,8 @@ class PlainYamlFormatter extends YamlFormatter {
222220}
223221
224222class StyledYamlFormatter extends YamlFormatter {
225- constructor ( relevantEngines : Set < string > , userContext : ConfigContext , defaultContext : ConfigContext ) {
226- super ( relevantEngines , userContext , defaultContext ) ;
223+ constructor ( config : CodeAnalyzerConfig , codeAnalyzer : CodeAnalyzer , userRules : RuleSelection , allDefaultRules : RuleSelection , relevantEngines : Set < string > ) {
224+ super ( config , codeAnalyzer , userRules , allDefaultRules , relevantEngines ) ;
227225 }
228226
229227 protected toYamlComment ( commentText : string ) : string {
0 commit comments