Skip to content

Commit

Permalink
Add quiet flag to hide warnings (palantir#4025)
Browse files Browse the repository at this point in the history
Sometimes you may want to run linting but ignore any warnings
produced, such as in a CI environment where you only want lint
failures to fail the build (many runners will check both the exit
code and STDERR to see if the program has passed).

This mimics the `quiet` flag available in ESLint, which hides
errors from output when passed.
  • Loading branch information
MatthewHerbst authored and johnwiseheart committed Jul 18, 2018
1 parent 0e8f9e0 commit 6b52df3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/usage/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Options:
-r, --rules-dir [rules-dir] rules directory
-s, --formatters-dir [formatters-dir] formatters directory
-t, --format [format] output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist, codeFrame)
-q, --quiet hide non "error" severity linting errors from output
--test test that tslint produces the correct output for the specified directory
-p, --project [project] tsconfig.json file
--type-check (deprecated) check for type errors before linting the project
Expand Down Expand Up @@ -117,6 +118,11 @@ tslint accepts the following command-line options:
Additional formatters can be added and used if the --formatters-dir
option is set.
-q, --quiet
Hide non "error" severity linting errors from output. This can be
especially useful in CI environments, where you may want only "error"
severity linting errors to cause linting to fail.
--test:
Runs tslint on matched directories and checks if tslint outputs
match the expected output in .lint files. Automatically loads the
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export interface ILinterOptions {
fix: boolean;
formatter?: string | FormatterConstructor;
formattersDirectory?: string;
quiet?: boolean;
rulesDirectory?: string | string[];
}
11 changes: 7 additions & 4 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,26 @@ export class Linter {
}

public getResult(): LintResult {
const errors = this.failures.filter((failure) => failure.getRuleSeverity() === "error");
const failures = this.options.quiet ? errors : this.failures;

const formatterName = this.options.formatter !== undefined ? this.options.formatter : "prose";
const Formatter = findFormatter(formatterName, this.options.formattersDirectory);
if (Formatter === undefined) {
throw new Error(`formatter '${formatterName}' not found`);
}
const formatter = new Formatter();

const output = formatter.format(this.failures, this.fixes);
const output = formatter.format(failures, this.fixes);

const errorCount = this.failures.filter((failure) => failure.getRuleSeverity() === "error").length;
const errorCount = errors.length;
return {
errorCount,
failures: this.failures,
failures,
fixes: this.fixes,
format: formatterName,
output,
warningCount: this.failures.length - errorCount,
warningCount: failures.length - errorCount,
};
}

Expand Down
9 changes: 8 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export interface Options {
*/
project?: string;

/**
* Whether to hide warnings
*/
quiet?: boolean;

/**
* Rules directory paths.
*/
Expand Down Expand Up @@ -241,9 +246,11 @@ async function doLinting(options: Options, files: string[], program: ts.Program
fix: !!options.fix,
formatter: options.format,
formattersDirectory: options.formattersDirectory,
quiet: !!options.quiet,
rulesDirectory: options.rulesDirectory,
},
program);
program,
);

let lastFolder: string | undefined;
let configFile = options.config !== undefined ? findConfiguration(options.config).results : undefined;
Expand Down
9 changes: 9 additions & 0 deletions src/tslintCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface Argv {
typeCheck?: boolean;
test?: boolean;
version?: boolean;
quiet?: boolean;
}

interface Option {
Expand Down Expand Up @@ -179,6 +180,13 @@ const options: Option[] = [
files will be linted. This flag also enables rules that require the
type checker.`,
},
{
short: "q",
name: "quiet",
type: "boolean",
describe: "hide errors on lint",
description: "If true, hides warnings from linting output.",
},
{
name: "type-check",
type: "boolean",
Expand Down Expand Up @@ -262,6 +270,7 @@ run(
out: argv.out,
outputAbsolutePaths: argv.outputAbsolutePaths,
project: argv.project,
quiet: argv.quiet,
rulesDirectory: argv.rulesDir,
test: argv.test,
typeCheck: argv.typeCheck,
Expand Down
44 changes: 42 additions & 2 deletions test/linterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import { assert } from "chai";
import * as fs from "fs";
import { createSourceFile, ScriptTarget } from "typescript";
import { DEFAULT_CONFIG } from "../src/configuration";
import { Replacement, RuleFailure } from "../src/language/rule/rule";
import { createTempFile } from "./utils";

import { Linter } from "../src/linter";
import { createTempFile } from "./utils";

class TestLinter extends Linter {
public applyFixesHelper(fileName: string, source: string, ruleFailures: RuleFailure[]) {
Expand Down Expand Up @@ -49,6 +49,11 @@ const templateDeclarationFixed =
<div></div>
`;

const withWarningDeclaration =
`
console.log("This line will not pass linting with the default rule set");
`;

describe("Linter", () => {

it("apply fixes to correct files", () => {
Expand All @@ -64,4 +69,39 @@ describe("Linter", () => {
assert.equal(fs.readFileSync(templateFile, "utf-8"), templateDeclarationFixed);
});

it("shows warnings", () => {
const config = DEFAULT_CONFIG;
config.rules.set("no-console", {
ruleArguments: ["log"],
ruleName: "no-console",
ruleSeverity: "warning",
});

const linter = new TestLinter({ fix: false });
const fileToLint = createTempFile("ts");
fs.writeFileSync(fileToLint, withWarningDeclaration);
linter.lint(fileToLint, withWarningDeclaration, config);
const result = linter.getResult();

assert.equal(result.warningCount, 1);
assert.equal(result.errorCount, 0);
});

it("does not show warnings when `quiet` is `true`", () => {
const config = DEFAULT_CONFIG;
config.rules.set("no-console", {
ruleArguments: ["log"],
ruleName: "no-console",
ruleSeverity: "warning",
});

const linter = new TestLinter({ fix: false, quiet: true });
const fileToLint = createTempFile("ts");
fs.writeFileSync(fileToLint, withWarningDeclaration);
linter.lint(fileToLint, withWarningDeclaration, config);
const result = linter.getResult();

assert.equal(result.warningCount, 0);
assert.equal(result.errorCount, 0);
});
});

0 comments on commit 6b52df3

Please sign in to comment.