diff --git a/README.md b/README.md index a9babfe28..4c73f182c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` diff --git a/src/index.ts b/src/index.ts index 330454e12..ff18452ab 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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') @@ -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']) { @@ -597,6 +604,9 @@ prog if (report.errorCount) { process.exit(1); } + if (report.warningCount > opts['max-warnings']) { + process.exit(1); + } } ); diff --git a/test/e2e/fixtures/lint/file-with-lint-warnings.ts b/test/e2e/fixtures/lint/file-with-lint-warnings.ts new file mode 100644 index 000000000..fe0df4995 --- /dev/null +++ b/test/e2e/fixtures/lint/file-with-lint-warnings.ts @@ -0,0 +1,5 @@ +// this file should have 3 "unused var" lint warnings +const unusedVar1 = () => { + const unusedVar2 = 'baz'; + const unusedVar3 = ''; +}; diff --git a/test/e2e/tsdx-lint.test.ts b/test/e2e/tsdx-lint.test.ts index f04327e39..e9ce66f3d 100644 --- a/test/e2e/tsdx-lint.test.ts +++ b/test/e2e/tsdx-lint.test.ts @@ -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);