What is tslint?
The tslint npm package is a static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors. It is widely used to enforce a consistent code style by checking the code against a set of linting rules.
What are tslint's main functionalities?
Linting TypeScript Files
This feature allows you to lint TypeScript files by specifying a configuration file and a pattern to match files. The command will process all TypeScript files in the 'src' directory and its subdirectories.
tslint -c tslint.json 'src/**/*.ts'
Fixing Linting Errors Automatically
This feature automatically fixes linting errors that can be corrected without human intervention. It is useful for fixing simple issues like whitespace or semicolon usage.
tslint --fix -c tslint.json 'src/**/*.ts'
Custom Rules
This feature allows you to use custom linting rules in addition to the predefined rules. You can specify a directory containing custom rule definitions to be applied to your code.
tslint -c tslint.json 'src/**/*.ts' --rules-dir custom_rules
Other packages similar to tslint
eslint
ESLint is a popular linting tool for JavaScript and TypeScript. It is highly configurable and extendable, with a large ecosystem of plugins. ESLint has effectively replaced TSLint as the preferred linter for TypeScript after TSLint's deprecation.
prettier
Prettier is an opinionated code formatter that supports many languages, including TypeScript. While it does not perform static code analysis, it formats code to a consistent style. Prettier can be used alongside linters like ESLint.
stylelint
Stylelint is a modern linter that helps you avoid errors and enforce conventions in your stylesheets. Although it is primarily used for CSS, it can be used in conjunction with PostCSS to lint SCSS, Sass, Less, and other CSS-like languages.
tslint
A linter for the TypeScript language.
Supported Rules
semicolon
enforces semicolons at the end of every statement.eqeqeq
enforces === and !== in favor of == and !=.maxlen
sets the maximum length of a line.whitespace
enforces whitespace between a keyword and a conditional, surrounding an operator,
and enclosing "=" within variable declarations and import/export statements.quotemark
enforces consistent single or double quoted string literals.oneline
enforces an opening brace to be on the same line as the expression preceding it.trailing
disallows trailing whitespace at the end of a line.bitwise
disallows bitwise operators.evil
disallows eval
function invocations.eofline
enforces the file to end with a newline.classname
enforces PascalCased class names.varname
allows only camelCased or UPPER_CASED variable names.noarg
disallows access to arguments.callee
.sub
disallows object access via string literals.forin
enforces a for ... in
statement to be filtered with an if
statement.debug
disallows debugger
statements.curly
enforces braces for if
/for
/do
/while
statements.indent
enforces consistent indentation levels for the whole file.
Development
Setup
git clone git@github.com:palantir/tslint.git
cd tslint
git submodule init
git submodule update
Build
npm install
grunt
TODO
- Disallow variables referenced outside of their scope definition
- Disallow unused variables