Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --max-warnings flag to tsdx lint #858

Merged
merged 8 commits into from
Sep 17, 2020
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ Usage
Options
--fix Fixes fixable errors and warnings
--ignore-pattern Ignore a pattern
--max-warnings Exits with non-zero error code if number of warnings exceed this number (default Infinity)
--write-file Write the config file locally
--report-file Write JSON report to file locally
-h, --help Displays this message
Expand All @@ -510,6 +511,7 @@ Examples
$ tsdx lint src
$ tsdx lint src --fix
$ tsdx lint src test --ignore-pattern test/foo.ts
$ tsdx lint src test --max-warnings 10
$ tsdx lint src --write-file
$ tsdx lint src --report-file report.json
```
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ prog
.example('lint src test --fix')
.option('--ignore-pattern', 'Ignore a pattern')
.example('lint src test --ignore-pattern test/foobar.ts')
.option(
'--max-warnings',
'Exits with non-zero error code if number of warnings exceed this number',
Infinity
)
.example('lint src test --max-warnings 10')
.option('--write-file', 'Write the config file locally')
.example('lint --write-file')
.option('--report-file', 'Write JSON report to file locally')
Expand All @@ -555,6 +561,7 @@ prog
'ignore-pattern': string;
'write-file': boolean;
'report-file': string;
'max-warnings': number;
_: string[];
}) => {
if (opts['_'].length === 0 && !opts['write-file']) {
Expand Down Expand Up @@ -597,6 +604,9 @@ prog
if (report.errorCount) {
process.exit(1);
}
if (report.warningCount > opts['max-warnings']) {
process.exit(1);
}
}
);

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/fixtures/lint/file-with-lint-warnings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// this file should have 3 "unused var" lint warnings
const unusedVar1 = () => {
const unusedVar2 = 'baz';
const unusedVar3 = '';
};
42 changes: 42 additions & 0 deletions test/e2e/tsdx-lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,48 @@ describe('tsdx lint', () => {
expect(output.code).toBe(0);
});

it('should succeed linting a ts file with warnings when --max-warnings is not used', () => {
const testFile = `${lintDir}/file-with-lint-warnings.ts`;
const output = shell.exec(`node dist/index.js lint ${testFile}`);
expect(output.code).toBe(0);
expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
true
);
});

it('should succeed linting a ts file with fewer warnings than --max-warnings', () => {
const testFile = `${lintDir}/file-with-lint-warnings.ts`;
const output = shell.exec(
`node dist/index.js lint ${testFile} --max-warnings 4`
);
expect(output.code).toBe(0);
expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
true
);
});

it('should succeed linting a ts file with same number of warnings as --max-warnings', () => {
const testFile = `${lintDir}/file-with-lint-warnings.ts`;
const output = shell.exec(
`node dist/index.js lint ${testFile} --max-warnings 3`
);
expect(output.code).toBe(0);
expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
true
);
});

it('should fail to lint a ts file with more warnings than --max-warnings', () => {
const testFile = `${lintDir}/file-with-lint-warnings.ts`;
const output = shell.exec(
`node dist/index.js lint ${testFile} --max-warnings 2`
);
expect(output.code).toBe(1);
expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
true
);
});

georgevarghese185 marked this conversation as resolved.
Show resolved Hide resolved
it('should not lint', () => {
const output = shell.exec(`node dist/index.js lint`);
expect(output.code).toBe(1);
Expand Down