'use strict' ; import * as fs from 'fs' ; import { Formatters } from 'tslint' ; import { RuleFailure } from 'tslint' ; /**
 * A base class for formatters that fix linting issues.
 */ export class BaseFormatter extends Formatters . AbstractFormatter { private ruleName : string ; private applyFix : ( this : BaseFormatter , failure : RuleFailure ) => void ; constructor ( ruleName , applyFix ) { super ( ) ; this . ruleName = ruleName ; this . applyFix = applyFix . bind ( this ) ; } public format ( allFailures ) { for ( let index = allFailures . length - 1 ; index >= 0 ; index -- ) { const failure = allFailures [ index ] ; if ( failure . getRuleName ( ) === this . ruleName ) { this . applyFix ( failure ) ; } } const outputLines = allFailures . map ( this . formatFailure ) ; return outputLines . join ( '\n' ) + '\n' ; } private formatFailure ( failure ) { const fileName = failure . getFileName ( ) ; const failureString = failure . getFailure ( ) ; const ruleName = failure . getRuleName ( ) ; const lineAndCharacter = failure . getStartPosition ( ) . getLineAndCharacter ( ) ; const positionTuple = '[' + ( lineAndCharacter . line + 1 ) + ', ' + ( lineAndCharacter . character + 1 ) + ']' ; return '(' + ruleName + ') ' + fileName + positionTuple + ': ' + failureString ; } protected readFile ( fileName ) { return fs . readFileSync ( fileName , { encoding : 'UTF-8' } ) ; } protected writeFile ( fileName , fileContents ) { fs . writeFileSync ( fileName , fileContents , { encoding : 'UTF-8' } ) ; } }