This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an optional
TSLintErrorSeverity
flag (#45)
* Added an optional `TSLintErrorSeverity` flag Optional MSBuild error severity override, as `"error"` or `"warning"`. Fixes #19. * Fixed README.md ordering for error severity * Moved the "X errors found" message above errors Now that error printing is a source of errors (albeit very unlikely) it makes sense to have the summary first.
- Loading branch information
Josh Goldberg
authored
Jul 2, 2016
1 parent
d115db3
commit f4ce855
Showing
7 changed files
with
140 additions
and
14 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,110 @@ | ||
/** | ||
* MSBuild error printers, keyed by severity level. | ||
*/ | ||
interface IErrorPrinters { | ||
[i: string]: IErrorPrinter; | ||
} | ||
|
||
/** | ||
* Prints a single pre-formatted MSBuild error. | ||
* | ||
* @param error A pre-formatted MSBuild error. | ||
*/ | ||
interface IErrorPrinter { | ||
(error: string): void; | ||
} | ||
|
||
/** | ||
* Prints MSBuild errors from the TSLint CLI with a severity level. | ||
*/ | ||
export class ErrorsPrinter { | ||
/** | ||
* MSBuild error printers, keyed by severity level. | ||
*/ | ||
private errorPrinters: IErrorPrinters = { | ||
error: (error: string): void => console.error(error), | ||
warning: (error: string): void => console.warn(error) | ||
}; | ||
|
||
/** | ||
* Overridden severity level for errors, if provided. | ||
*/ | ||
private severityOverride: string; | ||
|
||
/** | ||
* Initializes a new instance of the ErrorsPrinter class. | ||
* | ||
* @param severityOverride Overridden severity level for errors, if provided. | ||
*/ | ||
public constructor(severityOverride?: string) { | ||
this.severityOverride = severityOverride; | ||
} | ||
|
||
/** | ||
* Prints MSBuild errors. | ||
* | ||
* @param errors MSBuild errors. | ||
*/ | ||
public print(errors: string[]): void { | ||
for (const error of errors) { | ||
this.printError(error); | ||
} | ||
} | ||
|
||
/** | ||
* Prints an MSBuild error. | ||
* | ||
* @param error An MSBuild error. | ||
*/ | ||
public printError(error: string): void { | ||
const errorSeverity = this.getSeverityFromError(error); | ||
|
||
if (this.severityOverride && this.severityOverride !== errorSeverity) { | ||
error = this.replaceErrorSeverity(error, errorSeverity, this.severityOverride); | ||
} | ||
|
||
const severity = this.severityOverride || errorSeverity; | ||
const printer = this.errorPrinters[severity]; | ||
|
||
if (!printer) { | ||
throw new Error(`Unknown error severity: '${severity}'.`); | ||
} | ||
|
||
printer(error); | ||
} | ||
|
||
/** | ||
* @param error An MSBuild error. | ||
* @returns The error's severity. | ||
*/ | ||
private getSeverityFromError(error: string): string { | ||
return error | ||
.match(/\):\s.+:/) | ||
[0] | ||
.replace(/\W/g, ""); | ||
} | ||
|
||
/** | ||
* Replaces an error's severity with a new severity. | ||
* | ||
* @param error An MSBuild error. | ||
* @param originalSeverity The current severity level of the error. | ||
* @param newSeverity A new severity level for the error. | ||
* @returns A copy of the error with thenew severity level. | ||
*/ | ||
private replaceErrorSeverity(error: string, originalSeverity: string, newSeverity: string): string { | ||
return error.replace( | ||
this.wrapErrorFormat(originalSeverity), | ||
this.wrapErrorFormat(newSeverity)); | ||
} | ||
|
||
/** | ||
* Wraps a severity string with find-and-replace safe markers. | ||
* | ||
* @param severity A severity level. | ||
* @returns The severity with find-and-replace safe markers. | ||
*/ | ||
private wrapErrorFormat(severity: string): string { | ||
return `): ${severity}`; | ||
} | ||
} |
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
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