'use strict' ; import { IRuleMetadata , RuleFailure , RuleFailurePosition } from 'tslint' ; import * as _ from 'lodash' ; import * as CodeClimate from './codeclimate' ; import { ContentRenderer } from './contentRenderer' ; import { ITsLinterOption } from './tsLinter' ; import autobind from 'autobind-decorator' ; export class RuleNameNotFoundError extends Error { constructor ( public ruleName ) { super ( `rule named ${ ruleName } is not found in the metadata.` ) ; } } @ autobind export class IssueConverter { readonly filePattern : RegExp ; readonly contentRenderer : ContentRenderer ; constructor ( public option ) { this . filePattern = new RegExp ( `${ option . targetPath }(.*)` , 'i' ) ; this . contentRenderer = new ContentRenderer ( option . linterPath ) ; } convert ( failure ) { return { type : CodeClimate . issueTypes . Issue , check_name : failure . getRuleName ( ) , content : { body : this . contentBody ( failure . getRuleName ( ) ) } , description : failure . getFailure ( ) , categories : [ 'Style' ] , remediation_points : 50000 , location : this . convertToLocation ( failure ) } ; } private contentBody ( name ) { const rule = _ . find ( this . option . rules , { ruleName : name } ) ; if ( rule != null ) { return this . contentRenderer . render ( rule ) ; } else { throw new RuleNameNotFoundError ( name ) ; } } private convertToLocation ( failure ) { return { path : this . getFilePath ( failure ) , positions : { begin : this . convertToLineColumnPosition ( failure . getStartPosition ( ) ) , end : this . convertToLineColumnPosition ( failure . getEndPosition ( ) ) } } ; } private getFilePath ( failure ) { return this . filePattern . exec ( failure . getFileName ( ) ) . pop ( ) ; } private convertToLineColumnPosition ( position ) { return { line : position . getLineAndCharacter ( ) . line + 1 , column : position . getLineAndCharacter ( ) . character + 1 } ; } }