Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Enable typed rules with --project #2773

Merged
merged 3 commits into from
May 24, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/usage/type-checking/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const results = files.map(file => {
});
```

When using the CLI, the `--project` flag will automatically create a program from the specified `tsconfig.json` file. Adding the `--type-check` flag then enables rules that require the type checker.
When using the CLI, the `--project` flag will automatically create a program from the specified `tsconfig.json` file and enable rules that require the type checker.

Use the `--type-check` flag to make sure your program has no type errors. TSLint will check for any errors before before linting. This flag requires `--project` to be specified.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"compile:test": "tsc -p test",
"lint": "npm-run-all -p lint:global lint:from-bin",
"lint:global": "tslint --project test/tsconfig.json --format stylish --type-check # test includes 'src' too",
"lint:from-bin": "node bin/tslint --project test/tsconfig.json --format stylish --type-check",
"lint:from-bin": "node bin/tslint --project test/tsconfig.json --format stylish",
"test": "npm-run-all test:pre -p test:mocha test:rules",
"test:pre": "cd ./test/config && npm install",
"test:mocha": "mocha --reporter spec --colors \"build/test/**/*Tests.js\"",
Expand Down
4 changes: 0 additions & 4 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ class Linter {
`;
throw new Error(INVALID_SOURCE_ERROR);
}
// check if the program has been type checked
if (!("resolvedModules" in sourceFile)) {
throw new Error("Program must be type checked before linting");
}
return sourceFile;
} else {
return utils.getSourceFile(fileName, source);
Expand Down
3 changes: 0 additions & 3 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ export class Runner {
console.error(messages.join("\n"));
return onComplete(this.options.force ? 0 : 1);
}
} else {
// if not type checking, we don't need to pass in a program object
program = undefined;
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/tslint-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const processed = optimist
type: "boolean",
},
"type-check": {
describe: "enable type checking when linting a project",
describe: "check for type errors before linting the project",
type: "boolean",
},
"v": {
Expand Down Expand Up @@ -225,11 +225,12 @@ tslint accepts the following commandline options:
this can be used to test custom rules.

-p, --project:
The path or directory containing a tsconfig.json file that will be used to determine which
files will be linted.
The path or directory containing a tsconfig.json file that will be
used to determine which files will be linted. This flag also enables
rules that require the type checker.

--type-check
Enables the type checker when running linting rules. --project must be
Checks for type errors before linting a project. --project must be
specified in order to enable type checking.

-v, --version:
Expand Down
16 changes: 13 additions & 3 deletions test/executable/executableTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
});
});

describe("tsconfig.json", () => {
describe("--project flag", () => {
it("exits with code 0 if `tsconfig.json` is passed and it specifies files without errors", (done) => {
execCli(["-c", "test/files/tsconfig-test/tslint.json", "--project", "test/files/tsconfig-test/tsconfig.json"], (err) => {
assert.isNull(err, "process should exit without an error");
Expand Down Expand Up @@ -274,7 +274,7 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
});
});

it("exits with code 2 if both `tsconfig.json` and files arguments are passed and files contain lint errors", (done) => {
it("exits with code 1 if file is not included in project", (done) => {
execCli(
[
"-c",
Expand All @@ -285,7 +285,7 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
],
(err) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 2, "error code should be 2");
assert.strictEqual(err.code, 1, "error code should be 1");
done();
});
});
Expand Down Expand Up @@ -318,6 +318,16 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
done();
});
});

it("can execute typed rules without --type-check", (done) => {
execCli(
[ "-p", "test/files/typed-rule/tsconfig.json"],
(err) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 2, "error code should be 2");
done();
});
});
});

describe("--type-check", () => {
Expand Down
2 changes: 2 additions & 0 deletions test/files/typed-rule/fail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const fn: any;
fn();
5 changes: 5 additions & 0 deletions test/files/typed-rule/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strictNullChecks": true
}
}
5 changes: 5 additions & 0 deletions test/files/typed-rule/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-unsafe-any": true
}
}