Skip to content

Commit

Permalink
Added parameter to specify directories to lint
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sauer committed Jan 16, 2017
1 parent 746adc2 commit f8dbd7c
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 11 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
env: {
browser: false,
node: true,
es6: true
},
globals: {
'describe': true,
Expand Down
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
A simple way to run [TSLint](https://palantir.github.io/tslint/) in your
[Mocha](http://mochajs.org/) tests without a task runner like Grunt or Gulp.

Inspired by [mocha-jshint](https://github.com/Muscula/mocha-jshint) and [mocha-eslint](https://github.com/BadgeLabs/mocha-eslint).
Inspired by [mocha-jshint](https://github.com/Muscula/mocha-jshint) and
[mocha-eslint](https://github.com/BadgeLabs/mocha-eslint).

## Installation

Expand All @@ -29,20 +30,46 @@ var lint = require('mocha-tslint');
This will return a function with the signature:

```javascript
lint(configFilePath)
lint(configFilePath, pathsToLint)
```

where `configFilePath` is the path to your `tslint.json` from your project's top
level directory.
* `configFilePath`: the path to your `tslint.json` from your project's top
level directory
* `pathsToLint`: This can either be a string, an array of strings or it can
be left off
* If left off, linting will be done from the directory of your `tslint.json`
* If you put in a string or an array of strings you can define which folders
to lint.


So, a full test file to run in Mocha might look like:
```javascript
var lint = require('mocha-tslint');
var configFilePath = './tslint.json';

lint(configFilePath);
```

If you only want to lint files placed in `src/` and `test/`:
```javascript
var lint = require('mocha-tslint');
var configFilePath = './tslint.json';
var folders = [
'src',
'test'
];

lint(configFilePath, folders);
```

If you only want to lint all files that are in a folder placed in `src/`:
```javascript
var lint = require('mocha-tslint');
var configFilePath = './tslint.json';

lint(configFilePath, 'src/*'); // src/foo.ts won't be linted, but src/foo/bar.ts will be
```

## Notes

This module does not make any decisions about which TSLint rules to run. You need a
Expand Down
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ const options = {
formatter: 'json'
};

const linter = new Linter(options);
function getFileNames(configFilePath, pathsToLint) {
if (typeof pathsToLint === 'string') {
pathsToLint = [pathsToLint];
} else if (!pathsToLint) {
pathsToLint = [path.dirname(configFilePath)];
}

function getFileNames(configFilePath) {
const program = Linter.createProgram(configFilePath, path.dirname(configFilePath));
const fileNames = Linter.getFileNames(program);
let allFiles = [];

return fileNames;
pathsToLint.forEach((pathToLint) => {
const program = Linter.createProgram(configFilePath, pathToLint);

allFiles = allFiles.concat(Linter.getFileNames(program));
});

allFiles = Array.from(new Set(allFiles));
return allFiles;
}

function test(file, config) {
it(`should have no errors in ${file}`, (done) => {
fs.readFile(file, (err, sourceBuffer) => {
const linter = new Linter(options);
const source = sourceBuffer.toString();
linter.lint(file, source.toString(), config);
const result = linter.getResult();
Expand All @@ -43,9 +54,9 @@ function test(file, config) {
});
}

module.exports = function(configFilePath) {
module.exports = function(configFilePath, pathsToLint) {
describe('tslint', () => {
const fileNames = getFileNames(configFilePath);
const fileNames = getFileNames(configFilePath, pathsToLint);
let tslintConfig = {};

try {
Expand Down
16 changes: 16 additions & 0 deletions tests/acceptance/acceptanceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ describe('Acceptance: mocha-tslint', () => {
}
});
});

it('should only test for passed in subfolders', () => {
return runTest('tests/lint/subfolderLintTest.js').then((results) => {
if (results[3].indexOf('2 passing') === -1) {
throw new Error('Did not get two passing tests');
}
});
});

it('should only test for passed in subfolders with wildcard', () => {
return runTest('tests/lint/subfolderWildcardTest.js').then((results) => {
if (results[3].indexOf('2 passing') === -1) {
throw new Error('Did not get two passing tests');
}
});
});
});
1 change: 1 addition & 0 deletions tests/fixtures/subfolder/lint1/lintSucceed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = "bar";
1 change: 1 addition & 0 deletions tests/fixtures/subfolder/lint2/lintSucceed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = "bar";
1 change: 1 addition & 0 deletions tests/fixtures/subfolder/notlint/lintFail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = 'bar';
8 changes: 8 additions & 0 deletions tests/fixtures/subfolder/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"quotemark": [
true,
"double"
]
}
}
11 changes: 11 additions & 0 deletions tests/lint/subfolderLintTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const lint = require('../../index.js');
const configPath = 'tests/fixtures/subfolder/tslint.json';

const folders = [
'tests/fixtures/subfolder/lint1',
'tests/fixtures/subfolder/lint2'
];

lint(configPath, folders);
8 changes: 8 additions & 0 deletions tests/lint/subfolderWildcardTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const lint = require('../../index.js');
const configPath = 'tests/fixtures/subfolder/tslint.json';

const folders = 'tests/fixtures/subfolder/*';

lint(configPath, folders);

0 comments on commit f8dbd7c

Please sign in to comment.