Skip to content

Commit

Permalink
Add support for type checked rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
panuhorsmalahti committed Aug 2, 2016
1 parent cc1e597 commit da6024a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ npm install tslint@next
}));
```

Type checked rules
------------------

Type checked rules require a TypeScript program object to be provided to the linter in the options. For more information see tslint documentation.

```javascript
var gulpTslint = require("gulp-tslint");
var tslint = require("tslint");
var program = tslint.createProgram("./tsconfig.json");

// ...
.pipe(gulpTslint({ program }))
```

All default tslint options
--------------------------

Expand All @@ -161,7 +175,8 @@ const tslintOptions = {
formatter: "prose",
formattersDirectory: null,
rulesDirectory: null,
tslint: null
tslint: null,
program: null
};
```

Expand Down
13 changes: 9 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
var gulp = require("gulp");
var tslint = require("./index");
var gulpTslint = require("./index");
var tslint = require("tslint");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");

// To enable rules that work with the type checker
var program = tslint.createProgram("./tsconfig.json");

gulp.task("default", function() {
var tsResult = tsProject.src()
.pipe(tslint({
formatter: "prose"
.pipe(gulpTslint({
formatter: "prose",
program: program
}))
.pipe(tslint.report({
.pipe(gulpTslint.report({
emitError: false
}))
.pipe(ts(tsProject));
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export interface PluginOptions {
configuration?: any;
formatter?: string | Function;
formatter?: string;
formattersDirectory?: string;
rulesDirectory?: string;
tslint?: any;
program?: any;
}
export interface ReportOptions {
emitError?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var tslintPlugin = function (pluginOptions) {
configuration: pluginOptions.configuration,
formatter: pluginOptions.formatter || "prose",
formattersDirectory: pluginOptions.formattersDirectory || null,
program: pluginOptions.program || null,
rulesDirectory: pluginOptions.rulesDirectory || null
};
var linter = getTslint(pluginOptions);
Expand All @@ -99,7 +100,7 @@ var tslintPlugin = function (pluginOptions) {
// configuration can be a file path or null, if it's unknown
options.configuration = linter.findConfiguration(pluginOptions.configuration || null, file.path);
}
tslint = new linter(file.relative, file.contents.toString("utf8"), options);
tslint = new linter(file.relative, file.contents.toString("utf8"), options, options.program);
file.tslint = tslint.lint();
// Pass file
cb(null, file);
Expand Down
6 changes: 5 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export interface PluginOptions {
formattersDirectory?: string;
rulesDirectory?: string;
tslint?: any;

// ts.program, used for type checked rules
program?: any;
}

export interface ReportOptions {
Expand Down Expand Up @@ -137,6 +140,7 @@ const tslintPlugin = <TslintPlugin> function(pluginOptions?: PluginOptions) {
configuration: pluginOptions.configuration,
formatter: pluginOptions.formatter || "prose",
formattersDirectory: pluginOptions.formattersDirectory || null,
program: pluginOptions.program || null,
rulesDirectory: pluginOptions.rulesDirectory || null
};

Expand All @@ -152,7 +156,7 @@ const tslintPlugin = <TslintPlugin> function(pluginOptions?: PluginOptions) {
);
}

tslint = new linter(file.relative, file.contents.toString("utf8"), options);
tslint = new linter(file.relative, file.contents.toString("utf8"), options, options.program);
file.tslint = tslint.lint();

// Pass file
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gulp-tslint",
"preferGlobal": false,
"version": "6.0.3",
"version": "6.1.0",
"author": "Panu Horsmalahti <panu.horsmalahti@iki.fi>",
"description": "TypeScript linter Gulp plugin",
"contributors": [
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"no-use-before-declare": true,
"no-var-keyword": true,
"object-literal-sort-keys": true,
"no-for-in-array": true,
"one-line": [true,
"check-open-brace",
"check-catch",
Expand Down

1 comment on commit da6024a

@alexeagle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for me:

TypeError: Cannot use 'in' operator to search for 'resolvedModules' in undefined
    at MultiLinter.lint (/Users/alexeagle/Projects/angular/node_modules/tslint/lib/tslintMulti.js:40:40)
    at Linter.lint (/Users/alexeagle/Projects/angular/node_modules/tslint/lib/tslint.js:20:21)
    at /Users/alexeagle/Projects/angular/node_modules/gulp-tslint/index.js:104:30

The reason is that tslint sees files which are not in the ts.Program. The program.getSourceFiles() should be the files to lint, when a program is available.

See how the files are defaulted to be those in the program in the CLI:
https://github.com/palantir/tslint/blob/master/src/tslint-cli.ts#L275

Please sign in to comment.