Skip to content

Instantly share code, notes, and snippets.

@PaulBGD
Last active November 11, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulBGD/be273a4c27a1279dd025 to your computer and use it in GitHub Desktop.
Save PaulBGD/be273a4c27a1279dd025 to your computer and use it in GitHub Desktop.
TypeScript linter
var configuration = {
rules: {
'variable-name': true,
'comment-format': [true, 'check-space'],
'curly': true,
'eofline': true,
'forin': true,
'indent': [true, 'spaces'],
'jsdoc-format': true,
'no-switch-case-fall-through': true,
'no-trailing-whitespace': true,
'no-unreachable': true,
'no-unused-variable': true,
'no-var-keyword': true,
'quotemark': [true, 'single', 'avoid-escape'],
'semicolon': true,
'switch-default': true,
'trailing-comma': [true],
'triple-equals': true,
'typedef': [true],
'typedef-whitespace': [true],
'variable-name': [false],
'whitespace': [
true,
'check-branch',
'check-decl',
'check-operator',
'check-separator',
'check-type'
]
}
};
var options = {
formatter: 'json',
configuration: configuration
};
var Linter = require('tslint');
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
function checkDirectory (directory) {
var errors = 0;
fs.readdirSync(directory).forEach(function (file) {
file = path.join(directory, file);
if (fs.lstatSync(file).isDirectory()) {
errors += checkDirectory(file);
} else {
var extension = path.extname(file);
if (extension === '.ts' || extension == '.tsx') {
errors += lint(file);
}
}
});
return errors;
}
function lint (file) {
var linter = new Linter(file, fs.readFileSync(file).toString('utf8'), options);
var results = linter.lint();
var output = JSON.parse(results.output);
if (output.length === 0) {
console.log(chalk.bold.green(' \u2714 ' + chalk.gray(file)));
return 0;
}
console.log(chalk.bold.red(' \u2718 ' + chalk.gray(file + ' ' + chalk.red(output.length + ' Errors'))));
output.forEach(function (error) {
console.log(chalk.gray(' ' + parsePosition(error.startPosition) + ' - ' + parsePosition(error.endPosition) + ' ' + error.failure));
});
return output.length;
}
function parsePosition (position) {
return position.line + ':' + position.character;
}
var errors = checkDirectory('./');
if (errors !== 0) {
console.log(chalk.bold(errors + ' errors found.'));
process.exit(-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment