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

fix: fix with --dry-run should not modify files #169

Merged
merged 3 commits into from Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/lint.ts
Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions test/test-lint.ts
Expand Up @@ -15,6 +15,7 @@
*/

import test from 'ava';
import fs from 'fs';
import * as path from 'path';

import {Options} from '../src/cli';
Expand All @@ -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<string> = []';
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);
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down