Skip to content

Commit

Permalink
tslint emit failures as errors (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
zinserjan authored Sep 15, 2017
1 parent aad9653 commit acefc82
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ new TsCheckerWebpackPlugin(options: object)
|:--|:--:|:----------|
|**`tsconfig`**|`{String}`|Absolute path to tsconfig.json file.|
|**`tslint`**|`{String}`|Absolute path to tslint.json file. <br>Default: `undefined`|
|**`tslintEmitErrors`**|`{Boolean}`|Report all TSLint failures as webpack errors regardless of the rule severity. <br>Default: `false`|
|**`memoryLimit`**|`{Number}`|Memory limit for the type checker process in MB. <br>Default: `512`|
|**`diagnosticFormatter`**|`{String}`|Formatter for TypeScript Diagnostics. <br>One of `ts-loader`, `stylish` or `codeframe`.<br> Default: `ts-loader`|
|**`timings`**|`{Boolean}`|Logs timing information of the type checker. <br>Default: `false`|
Expand Down
12 changes: 10 additions & 2 deletions src/TsCheckerWebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { WebpackBuildResult } from "./checker/resultSerializer";
export interface TsCheckerWebpackPluginOptions {
tsconfig: string;
tslint?: string;
tslintEmitErrors?: boolean;
memoryLimit?: number;
timings?: boolean;
diagnosticFormatter?: string;
Expand All @@ -22,12 +23,19 @@ class TsCheckerWebpackPlugin {
private startTime: number = Date.now();

constructor(options: TsCheckerWebpackPluginOptions) {
const { tsconfig, tslint, memoryLimit = 512, timings = false, diagnosticFormatter = "ts-loader" } = options;
const {
tsconfig,
tslint,
tslintEmitErrors = false,
memoryLimit = 512,
timings = false,
diagnosticFormatter = "ts-loader",
} = options;
this.logger = new Logger();
if (timings) {
this.logger.enable();
}
this.checker = new TsCheckerWorker(memoryLimit, timings, tsconfig, diagnosticFormatter, tslint);
this.checker = new TsCheckerWorker(memoryLimit, timings, tsconfig, diagnosticFormatter, tslintEmitErrors, tslint);
this.checker.start();
}

Expand Down
11 changes: 8 additions & 3 deletions src/checker/resultSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export type WebpackBuildResult = {
export const transformToWebpackBuildResult = (
result: TsCheckerResult,
contextPath: string,
diagnosticFormat: string
diagnosticFormat: string,
tslintEmitErrors: boolean
): WebpackBuildResult => {
const diagnosticErrors = result.diagnostics.filter(
(diagnostic: Diagnostic) => DiagnosticCategory[diagnostic.category].toLowerCase() === "error"
Expand All @@ -27,8 +28,12 @@ export const transformToWebpackBuildResult = (
(diagnostic: Diagnostic) => DiagnosticCategory[diagnostic.category].toLowerCase() !== "error"
);

const lintErrors = result.lints.filter((failure: RuleFailure) => failure.getRuleSeverity() === "error");
const lintWarnings = result.lints.filter((failure: RuleFailure) => failure.getRuleSeverity() !== "error");
const lintErrors = result.lints.filter(
(failure: RuleFailure) => failure.getRuleSeverity() === "error" || tslintEmitErrors
);
const lintWarnings = tslintEmitErrors
? []
: result.lints.filter((failure: RuleFailure) => failure.getRuleSeverity() !== "error");

const errors = [
...diagnosticFormatter(diagnosticErrors, diagnosticFormat, contextPath),
Expand Down
3 changes: 2 additions & 1 deletion src/worker/TsCheckerRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface TsCheckerRuntimeConfig {
tsconfigPath: string;
diagnosticFormatter: string;
tslintPath?: string;
tslintEmitErrors: boolean;
timings: boolean;
}

Expand Down Expand Up @@ -45,7 +46,7 @@ process.on("message", function(message: any) {
incrementalChecker.updateBuiltFiles(message.files);
const result = incrementalChecker.run();

const webpackResult = transformToWebpackBuildResult(result, contextPath, config.diagnosticFormatter);
const webpackResult = transformToWebpackBuildResult(result, contextPath, config.diagnosticFormatter, config.tslintEmitErrors);
const serialized = serializeWebpackBuildResult(webpackResult);

sendMessage({
Expand Down
4 changes: 3 additions & 1 deletion src/worker/TsCheckerWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export default class TsCheckerWorker {
timings: boolean,
tsconfigPath: string,
diagnosticFormatter: string,
tslintPath?: string
tslintEmitErrors: boolean,
tslintPath?: string,
) {
this.memoryLimit = memoryLimit;
this.runtimeConfig = {
tsconfigPath,
diagnosticFormatter,
tslintPath,
tslintEmitErrors,
timings,
};
this.exitListener = () => {
Expand Down
11 changes: 11 additions & 0 deletions test/__snapshots__/TestCases.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ Module build failed: error TS5014: Failed to parse file 'xfile': Unexpected toke
}
`;

exports[`TestCases tslint-emit-warning-as-error 1`] = `
Object {
"errors": Array [
"xfile
2:11 warning ' should be \\" quotemark
",
],
"warnings": Array [],
}
`;

exports[`TestCases tslint-error 1`] = `
Object {
"errors": Array [
Expand Down
2 changes: 2 additions & 0 deletions test/testCases/tslint-emit-warning-as-error/src/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// prettier-ignore
const x = 'dddd';
15 changes: 15 additions & 0 deletions test/testCases/tslint-emit-warning-as-error/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es6",
"target": "es5",
"sourceMap": true,
"noImplicitAny": true
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
6 changes: 6 additions & 0 deletions test/testCases/tslint-emit-warning-as-error/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"defaultSeverity": "warning",
"rules": {
"quotemark": [true, "double"]
}
}
34 changes: 34 additions & 0 deletions test/testCases/tslint-emit-warning-as-error/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as path from "path";
import TsCheckerWebpackPlugin from "../../../src/TsCheckerWebpackPlugin";

module.exports = {
context: __dirname,
entry: "./src/entry.ts",
output: {
filename: "bundle.js",
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"], // note if using webpack 1 you'd also need a '' in the array as well
},
module: {
rules: [
{
test: /\.tsx?$/,
use: {
loader: "ts-loader",
options: {
transpileOnly: true,
},
},
},
],
},
plugins: [
new TsCheckerWebpackPlugin({
tsconfig: path.join(__dirname, "tsconfig.json"),
tslint: path.join(__dirname, "tslint.json"),
tslintEmitErrors: true,
}),
],
};

0 comments on commit acefc82

Please sign in to comment.