diff --git a/src/lint.ts b/src/lint.ts index a9fc096d..0c6cf353 100644 --- a/src/lint.ts +++ b/src/lint.ts @@ -24,10 +24,16 @@ import {Options} from './cli'; * Run tslint with the default configuration. Returns true on success. * @param options gts options * @param files files to run linter on - * @param fix automatically fix linter errors + * @param fix automatically fix linter errors. Ignored when options.dryRun is + * set. */ export function lint( options: Options, files: string[] = [], fix = false): boolean { + if (options.dryRun && fix) { + options.logger.log('lint: skipping auto fix since --dry-run was passed'); + fix = false; + } + if (files.length > 0) { // manually provided filenames. const rcs = files.map(file => { // Different config files may apply to each file. diff --git a/test/test-lint.ts b/test/test-lint.ts index d8ce25d6..b3cc55f0 100644 --- a/test/test-lint.ts +++ b/test/test-lint.ts @@ -15,6 +15,7 @@ */ import test from 'ava'; +import fs from 'fs'; import * as path from 'path'; import {Options} from '../src/cli'; @@ -35,6 +36,10 @@ const OPTIONS: Options = { const BAD_CODE = `throw 'hello world';`; const GOOD_CODE = `throw new Error('hello world');`; +// missing semicolon, array-type simple. +const FIXABLE_CODE = 'const x : Array = []'; +const FIXABLE_CODE_FIXED = 'const x : string[] = [];'; + test.serial('createProgram should return an object', async t => { await withFixtures({'tsconfig.json': '{}'}, async () => { const program = lint.createProgram(OPTIONS); @@ -66,6 +71,37 @@ test.serial('lint should return false on bad code', async t => { }); }); +test.serial('lint should auto fix fixable errors', async t => { + await withFixtures( + { + 'tsconfig.json': JSON.stringify({files: ['a.ts']}), + 'a.ts': FIXABLE_CODE + }, + async (fixturesDir) => { + const okay = lint.lint(OPTIONS, [], true); + t.is(okay, true); + const contents = + fs.readFileSync(path.join(fixturesDir, 'a.ts'), 'utf8'); + t.deepEqual(contents, FIXABLE_CODE_FIXED); + }); +}); + +test.serial('lint should not auto fix on dry-run', async t => { + await withFixtures( + { + 'tsconfig.json': JSON.stringify({files: ['a.ts']}), + 'a.ts': FIXABLE_CODE + }, + async (fixturesDir) => { + const optionsWithDryRun = Object.assign({}, OPTIONS, {dryRun: true}); + const okay = lint.lint(optionsWithDryRun, [], true); + t.is(okay, false); + const contents = + fs.readFileSync(path.join(fixturesDir, 'a.ts'), 'utf8'); + t.deepEqual(contents, FIXABLE_CODE); + }); +}); + test.serial('lint should lint files listed in tsconfig.files', async t => { await withFixtures( { @@ -94,6 +130,25 @@ test.serial( }); }); +test.serial( + 'lint should lint files listed in tsconfig.files when empty list is provided', + async t => { + await withFixtures( + { + 'tsconfig.json': JSON.stringify({files: ['a.ts']}), + 'a.ts': FIXABLE_CODE, + 'b.ts': BAD_CODE + }, + async (fixturesDir) => { + const okay = lint.lint(OPTIONS, [], true); + t.is(okay, true); + const contents = + fs.readFileSync(path.join(fixturesDir, 'a.ts'), 'utf8'); + t.deepEqual(contents, FIXABLE_CODE_FIXED); + }); + }); + + test.serial('lint should not lint files listed in exclude', async t => { await withFixtures( {