From 12380901e753f2dcd67c0100a792574a21ae6391 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Fri, 1 Jun 2018 15:44:48 +0200 Subject: [PATCH] feat: ability to use user provided tslint config This change adds the ability to use tslint.conf provided by the user. If the user doesn't have a config file, we fall back to the default gts config file. --- src/lint.ts | 9 ++++++--- test/test-lint.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/lint.ts b/src/lint.ts index 5c68e503..554446bf 100644 --- a/src/lint.ts +++ b/src/lint.ts @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import * as fs from 'fs'; import * as path from 'path'; import {Configuration, Linter} from 'tslint'; import * as ts from 'typescript'; @@ -27,11 +28,13 @@ import {Options} from './cli'; */ export function lint( options: Options, files: string[] = [], fix = false): boolean { - const tslintConfigPath = path.join(options.gtsRootDir, 'tslint.json'); + const configPath = + fs.existsSync(path.join(options.targetRootDir, 'tslint.json')) ? + path.join(options.targetRootDir, 'tslint.json') : + path.join(options.gtsRootDir, 'tslint.json'); const program = createProgram(options); - const configuration = - Configuration.findConfiguration(tslintConfigPath, '').results; + const configuration = Configuration.findConfiguration(configPath, '').results; const linter = new Linter({fix, formatter: 'codeFrame'}, program); const srcFiles = files.length > 0 ? files : Linter.getFileNames(program); srcFiles.forEach(file => { diff --git a/test/test-lint.ts b/test/test-lint.ts index 06cfe0a2..0bca9c50 100644 --- a/test/test-lint.ts +++ b/test/test-lint.ts @@ -42,7 +42,6 @@ test.serial('createProgram should return an object', async t => { }); }); - test.serial('lint should return true on good code', async t => { await withFixtures( { @@ -148,4 +147,31 @@ test.serial('lint should not throw for unrecognized files', async t => { }); }); +test.serial('lint should prefer user config file over default', async t => { + const CUSTOM_LINT_CODE = 'const t: Object;'; + + // By defualt the above should fail lint. + await withFixtures( + { + 'tsconfig.json': JSON.stringify({files: ['a.ts']}), + 'a.ts': CUSTOM_LINT_CODE + }, + async () => { + const okay = lint.lint(OPTIONS); + t.is(okay, false); + }); + + // User should be able to override the default config. + await withFixtures( + { + 'tsconfig.json': JSON.stringify({files: ['a.ts']}), + 'tslint.json': JSON.stringify({}), + 'a.ts': CUSTOM_LINT_CODE + }, + async () => { + const okay = lint.lint(OPTIONS); + t.is(okay, true); + }); +}); + // TODO: test for when tsconfig.json is missing.