|
| 1 | +import { |
| 2 | + AstParser, |
| 3 | + Input, |
| 4 | + NoExportError, |
| 5 | + NoMethodError, |
| 6 | +} from '@exercism/static-analysis' |
| 7 | +import { TSESTree } from '@typescript-eslint/typescript-estree' |
| 8 | +import { ExecutionOptions, WritableOutput } from '~src/interface' |
| 9 | +import { |
| 10 | + EXEMPLAR_SOLUTION, |
| 11 | + NO_METHOD, |
| 12 | + NO_NAMED_EXPORT, |
| 13 | +} from '../../../comments/shared' |
| 14 | +import { IsolatedAnalyzerImpl } from '../../IsolatedAnalyzerImpl' |
| 15 | +import { ExemplarSolution } from './ExemplarSolution' |
| 16 | + |
| 17 | +type Program = TSESTree.Program |
| 18 | + |
| 19 | +export class ExemplarAnalyzer extends IsolatedAnalyzerImpl { |
| 20 | + private solution!: ExemplarSolution |
| 21 | + |
| 22 | + protected async execute( |
| 23 | + input: Input, |
| 24 | + output: WritableOutput, |
| 25 | + options: ExecutionOptions |
| 26 | + ): Promise<void> { |
| 27 | + const [parsed] = await AstParser.ANALYZER.parse(input) |
| 28 | + |
| 29 | + this.solution = this.checkStructure(parsed.program, parsed.source, output) |
| 30 | + this.solution.readExemplar(options.inputDir) |
| 31 | + |
| 32 | + if (this.solution.isExemplar) { |
| 33 | + output.add(EXEMPLAR_SOLUTION()) |
| 34 | + output.finish() |
| 35 | + } |
| 36 | + |
| 37 | + output.finish() |
| 38 | + } |
| 39 | + |
| 40 | + private checkStructure( |
| 41 | + program: Readonly<Program>, |
| 42 | + source: Readonly<string>, |
| 43 | + output: WritableOutput |
| 44 | + ): ExemplarSolution | never { |
| 45 | + try { |
| 46 | + return new ExemplarSolution(program, source) |
| 47 | + } catch (error) { |
| 48 | + if (error instanceof NoMethodError) { |
| 49 | + output.add(NO_METHOD({ 'method.name': error.method })) |
| 50 | + output.finish() |
| 51 | + } |
| 52 | + |
| 53 | + if (error instanceof NoExportError) { |
| 54 | + output.add(NO_NAMED_EXPORT({ 'export.name': error.namedExport })) |
| 55 | + output.finish() |
| 56 | + } |
| 57 | + |
| 58 | + throw error |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments