-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
158 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {EOL} from 'os' | ||
|
||
import {codeFrameColumns, Location} from '@babel/code-frame' | ||
|
||
import chalk from 'chalk' | ||
import normalizePath = require('normalize-path') | ||
import * as ts from 'typescript' | ||
|
||
export function formatDiagnostics(diagnostics: ReadonlyArray<ts.Diagnostic>, context: string): string { | ||
return diagnostics.map(diagnostic => { | ||
const messageText = formatDiagnosticMessage(diagnostic.messageText, '', context) | ||
const {file} = diagnostic | ||
let message = messageText | ||
|
||
if(file != null && diagnostic.start != null) { | ||
const lineChar = file.getLineAndCharacterOfPosition(diagnostic.start) | ||
const source = file.text || diagnostic.source | ||
const start = { | ||
line: lineChar.line + 1, | ||
column: lineChar.character + 1 | ||
} | ||
const location: Location = {start} | ||
const red = chalk.red(`🚨 ${file.fileName}(${start.line},${start.column})`) | ||
|
||
const messages = [`${red}\n${chalk.redBright(messageText)}`] | ||
|
||
if(source != null) { | ||
if(typeof diagnostic.length === 'number') { | ||
const end = file.getLineAndCharacterOfPosition(diagnostic.start + diagnostic.length) | ||
|
||
location.end = { | ||
line: end.line + 1, | ||
column: end.character + 1 | ||
} | ||
} | ||
|
||
const frame = codeFrameColumns(source, location, { | ||
linesAbove: 1, | ||
linesBelow: 1, | ||
highlightCode: true | ||
}) | ||
|
||
messages.push( | ||
frame | ||
.split('\n') | ||
.map(str => ` ${str}`) | ||
.join('\n') | ||
) | ||
} | ||
|
||
message = messages.join('\n') | ||
} | ||
|
||
return message + EOL | ||
}).join(EOL) + EOL | ||
} | ||
|
||
function formatDiagnosticMessage(diagnostic: string|ts.DiagnosticMessageChain, delimiter: string, context: string) { | ||
const contextPath = normalizePath(context) | ||
|
||
return ts | ||
.flattenDiagnosticMessageText(diagnostic, delimiter) | ||
.replace(new RegExp(contextPath, 'g'), '.') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './format' | ||
export * from './reporter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as tslint from 'tslint' | ||
import * as ts from 'typescript' | ||
|
||
export class Linter { | ||
private readonly linter: tslint.Linter | null = null | ||
|
||
constructor( | ||
program: ts.Program, | ||
private readonly config: tslint.Configuration.IConfigurationFile | ||
) { | ||
try { | ||
const tslintModule: typeof tslint = require('tslint') | ||
|
||
this.linter = new tslintModule.Linter({fix: false}, program) | ||
} | ||
catch { | ||
this.linter = null | ||
} | ||
} | ||
|
||
public lint(file: string, code: string): tslint.RuleFailure[] { | ||
const {linter} = this | ||
|
||
if(!linter) { | ||
return [] | ||
} | ||
|
||
linter.lint(file, code, this.config) | ||
|
||
return linter | ||
.getResult() | ||
.failures | ||
.filter(failure => failure.getFileName() === file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.