Skip to content

Commit

Permalink
Add --max-warnings flag to tsdx lint (#858)
Browse files Browse the repository at this point in the history
Works the same as ESLint's --max-warnings flag

If `tsdx lint` finds no errors but more than `--max-warnings`
number of warnings, it exits with a non-zero code. Otherwise,
it exits with 0

- Add lint warning tests for this
- Use default value of Infinity for --max-warnings flag
- Update README's API Reference with new flag
  • Loading branch information
georgevarghese185 authored Sep 17, 2020
1 parent d4c3bb2 commit dd92fec
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
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
);
});

it('should not lint', () => {
const output = shell.exec(`node dist/index.js lint`);
expect(output.code).toBe(1);
Expand Down

1 comment on commit dd92fec

@vercel
Copy link

@vercel vercel bot commented on dd92fec Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.