1- import { parse as parseToTree , TSESTreeOptions as ParserOptions } from '@typescript-eslint/typescript-estree'
2- import { Program } from '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree'
1+ import { getProcessLogger as getLogger , Logger } from '../utils/logger'
32
4- import { Solution } from '../solution'
5- import { get as getLogger , Logger } from '../utils/logger'
6-
7- import { AnalyzerOutput } from './analyzer_output'
83import { Comment } from '../comments/comment'
4+ import { AnalyzerOutput } from '../output/AnalyzerOutput' ;
5+ import { ParsedSource , AstParser } from '../parsers/AstParser' ;
96
107class EarlyFinalization extends Error {
118 constructor ( ) {
@@ -15,29 +12,15 @@ class EarlyFinalization extends Error {
1512 }
1613}
1714
18- export abstract class BaseAnalyzer {
15+ export abstract class AnalyzerImpl implements Analyzer {
1916 protected readonly logger : Logger
20- protected readonly output : AnalyzerOutput
21-
22- /**
23- * The parser options passed to typescript-estree.parse
24- *
25- * @readonly
26- * @static
27- * @type {(ParserOptions | undefined) }
28- */
29- static get parseOptions ( ) : ParserOptions | undefined {
30- return undefined
31- }
17+ private output ! : AnalyzerOutput
3218
3319 /**
3420 * Creates an instance of an analyzer
35- *
36- * @param {Solution } solution the solution
3721 */
38- constructor ( protected readonly solution : Solution ) {
22+ constructor ( ) {
3923 this . logger = getLogger ( )
40- this . output = new AnalyzerOutput ( )
4124 }
4225
4326 /**
@@ -50,8 +33,11 @@ export abstract class BaseAnalyzer {
5033 *
5134 * @memberof BaseAnalyzer
5235 */
53- public async run ( ) : Promise < AnalyzerOutput > {
54- await this . execute ( )
36+ public async run ( input : Input ) : Promise < Output > {
37+ // Ensure each run has a fresh output
38+ this . output = new AnalyzerOutput ( )
39+
40+ await this . execute ( input )
5541 . catch ( ( err ) => {
5642 if ( err instanceof EarlyFinalization ) {
5743 this . logger . log ( `=> early finialization (${ this . output . status } )` )
@@ -126,50 +112,13 @@ export abstract class BaseAnalyzer {
126112
127113 /**
128114 * Property that returns true if there is at least one comment in the output.
129- *
130- * @readonly
131- * @memberof BaseAnalyzer
132115 */
133116 get hasCommentary ( ) {
134117 return this . output . comments . length > 0
135118 }
136119
137120 /**
138121 * Execute the analyzer
139- *
140- * @protected
141- * @abstract
142- * @returns {Promise<void> }
143- * @memberof BaseAnalyzer
144122 */
145- protected abstract execute ( ) : Promise < void >
146-
147- /**
148- * Read n files from the solution
149- *
150- * @param solution
151- * @param n
152- * @returns
153- */
154- protected static read ( solution : Solution , n : number ) : Promise < Buffer [ ] > {
155- return solution . read ( n )
156- }
157-
158- /**
159- * Parse a solution's files
160- *
161- * @param solution
162- * @param n number of files expected
163- * @returns n programs
164- */
165- protected static async parse ( solution : Solution , n = 1 ) : Promise < { program : Program , source : string } [ ] > {
166- const sourceBuffers = await this . read ( solution , n )
167- const sources = sourceBuffers . map ( source => source . toString ( ) )
168- const logger = getLogger ( )
169-
170- logger . log ( `=> inputs: ${ sources . length } ` )
171- sources . forEach ( source => logger . log ( `\n${ source } \n` ) )
172-
173- return sources . map ( source => ( { program : parseToTree ( source , this . parseOptions ) , source } ) )
174- }
123+ protected abstract execute ( input : Input ) : Promise < void >
175124}
0 commit comments