-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (54 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
const TSLint = require('tslint');
const Linter = TSLint.Linter;
const Configuration = TSLint.Configuration;
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const options = {
fix: false,
formatter: 'json'
};
function getFileNames(configFilePath, pathsToLint) {
if (typeof pathsToLint === 'string') {
pathsToLint = [pathsToLint];
} else if (!pathsToLint) {
pathsToLint = [path.dirname(configFilePath)];
}
let allFiles = [];
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();
if (result.failureCount > 0 || result.errorCount > 0) {
const errorMessage = [chalk.red('Code did not pass lint rules')];
result.failures.forEach((failure) => {
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
const character = lineAndCharacter.character;
const line = lineAndCharacter.line;
errorMessage.push(`${failure.getFailure()} at line ${line + 1}, character ${character + 1}`);
});
done(new Error(errorMessage.join('\n\t')));
} else {
done();
}
});
});
}
module.exports = function(configFilePath, pathsToLint) {
describe('tslint', () => {
const fileNames = getFileNames(configFilePath, pathsToLint);
const tslintConfig = Configuration.loadConfigurationFromPath(configFilePath);
fileNames.forEach((file) => test(file, tslintConfig));
});
};