Skip to content

Commit

Permalink
feat: ability to use user provided tslint config (#159)
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 0fd3a2d commit e8cca36
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -11,7 +11,8 @@

- **No configuration**. The easiest way to enforce consistent style in your project. Just drop it in.
- **Automatically format code**. Just run `gts fix` and say goodbye to messy or inconsistent code.
- **Catch style issues & programmer errors early.** Save precious code review time by eliminating back-and-forth between reviewer & contributor.
- **Catch style issues & programmer errors early**. Save precious code review time by eliminating back-and-forth between reviewer & contributor.
- **Opinionated, but not to a fault**. We recommend you use the default configuration, but if you *need* to customize compiler or linter config, you can.

Under the covers, we use [tslint][tslint-url] to enforce the style guide and provide automated fixes, and [clang-format][clang-format-url] to re-format code.

Expand Down Expand Up @@ -42,6 +43,8 @@ When you run the `npx gts init` command, it's going to do a few things for you:
- `compile`: Compiles the source code using TypeScript compiler.
- `pretest`, `posttest` and `prepare`: convenience integrations.

We strongly recommend you use the default style config, but if you must tweak, you can edit the generated `tsconfig.json`. For linter, we use the default `tslint.json` unless we find that file in your project directory.

## Individual files
The commands above will all run in the scope of the current folder. Some commands can be run on individual files:

Expand Down
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 e8cca36

Please sign in to comment.