Skip to content

Commit 8a46de4

Browse files
committed
Rework lint to use tslint-as-a-lib
1 parent cd390fd commit 8a46de4

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

.vscode/tasks.json

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818
"problemMatcher": [
1919
"$tsc"
2020
]
21+
},
22+
{
23+
"taskName": "lint-server",
24+
"args": [],
25+
"problemMatcher": {
26+
"owner": "typescript",
27+
"fileLocation": ["relative", "${workspaceRoot}"],
28+
"pattern": {
29+
"regexp": "^(warning|error)\\s+([^(]+)\\s+\\((\\d+|\\d+,\\d+|\\d+,\\d+,\\d+,\\d+)\\):\\s+(.*)$",
30+
"severity": 1,
31+
"file": 2,
32+
"location": 3,
33+
"message": 4
34+
},
35+
"watchedTaskBeginsRegExp": "^\\*\\*\\*Lint failure\\*\\*\\*$",
36+
"watchedTaskEndsRegExp": "^\\*\\*\\* Total \\d+ failures\\.$"
37+
},
38+
"showOutput": "always",
39+
"isWatching": true
2140
}
2241
]
2342
}

Jakefile.js

+77-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var fs = require("fs");
44
var os = require("os");
55
var path = require("path");
66
var child_process = require("child_process");
7+
var Linter = require("tslint");
78

89
// Variables
910
var compilerDirectory = "src/compiler/";
@@ -799,17 +800,85 @@ tslintRulesFiles.forEach(function(ruleFile, i) {
799800
compileFile(tslintRulesOutFiles[i], [ruleFile], [ruleFile], [], /*useBuiltCompiler*/ true, /*noOutFile*/ true, /*generateDeclarations*/ false, path.join(builtLocalDirectory, "tslint"));
800801
});
801802

803+
function getLinterOptions() {
804+
return {
805+
configuration: require("./tslint.json"),
806+
formatter: "prose",
807+
formattersDirectory: undefined,
808+
rulesDirectory: "built/local/tslint"
809+
};
810+
}
811+
812+
function lintFileContents(options, path, contents) {
813+
var ll = new Linter(path, contents, options);
814+
return ll.lint();
815+
}
816+
817+
function lintFile(options, path) {
818+
var contents = fs.readFileSync(path, "utf8");
819+
return lintFileContents(options, path, contents);
820+
}
821+
822+
function lintFileAsync(options, path, cb) {
823+
fs.readFile(path, "utf8", function(err, contents) {
824+
if (err) {
825+
return cb(err);
826+
}
827+
var result = lintFileContents(options, path, contents);
828+
cb(undefined, result);
829+
});
830+
}
831+
832+
var lintTargets = compilerSources.concat(harnessCoreSources);
833+
802834
// if the codebase were free of linter errors we could make jake runtests
803835
// run this task automatically
804836
desc("Runs tslint on the compiler sources");
805837
task("lint", ["build-rules"], function() {
806-
function success(f) { return function() { console.log('SUCCESS: No linter errors in ' + f + '\n'); }};
807-
function failure(f) { return function() { console.log('FAILURE: Please fix linting errors in ' + f + '\n') }};
808-
809-
var lintTargets = compilerSources.concat(harnessCoreSources);
838+
var lintOptions = getLinterOptions();
810839
for (var i in lintTargets) {
811-
var f = lintTargets[i];
812-
var cmd = 'tslint --rules-dir built/local/tslint -c tslint.json ' + f;
813-
exec(cmd, success(f), failure(f));
840+
var result = lintFile(lintOptions, lintTargets[i]);
841+
if (result.failureCount > 0) {
842+
console.log(result.output);
843+
}
814844
}
815-
}, { async: true });
845+
});
846+
847+
var lintSemaphores = {};
848+
849+
function lintWatchFile(filename) {
850+
fs.watch(filename, {persistent: true}, function(event) {
851+
if (event !== "change") {
852+
return;
853+
}
854+
855+
if (!lintSemaphores[filename]) {
856+
lintSemaphores[filename] = true;
857+
lintFileAsync(getLinterOptions(), filename, function(err, result) {
858+
delete lintSemaphores[filename];
859+
if (err) {
860+
console.log(err);
861+
return;
862+
}
863+
if (result.failureCount > 0) {
864+
console.log("***Lint failure***");
865+
for (var i = 0; i < result.failures.length; i++) {
866+
var failure = result.failures[i];
867+
var s = failure.startPosition.lineAndCharacter;
868+
var e = failure.endPosition.lineAndCharacter;
869+
console.log("warning "+filename+" ("+(s.line+1)+","+(s.character+1)+","+(e.line+1)+","+(e.character+1)+"): "+failure.failure);
870+
}
871+
console.log("*** Total "+result.failureCount+" failures.");
872+
}
873+
});
874+
}
875+
});
876+
}
877+
878+
desc("Watches files for changes to rerun a lint pass");
879+
task("lint-server", ["build-rules"], function() {
880+
console.log('Watching ./src for changes to linted files');
881+
for (var i=0; i<lintTargets.length; i++) {
882+
lintWatchFile(lintTargets[i]);
883+
}
884+
});

0 commit comments

Comments
 (0)