Skip to content

Commit

Permalink
feat: ability to use user provided tslint config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ofrobots committed Jun 1, 2018
1 parent ea50344 commit 1238090
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/lint.ts
Expand Up @@ -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';
Expand All @@ -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 => {
Expand Down
28 changes: 27 additions & 1 deletion test/test-lint.ts
Expand Up @@ -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(
{
Expand Down Expand Up @@ -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.

0 comments on commit 1238090

Please sign in to comment.