A linter for the TypeScript language.
banbans the use of specific functions. Options are ["object", "function"] pairs that ban the use of object.function()class-nameenforces PascalCased class and interface names.comment-formatenforces rules for single-line comments. Rule options:"check-space"enforces the rule that all single-line comments must begin with a space, as in// comment- note that comments starting with
///are also allowed, for things such as ///
- note that comments starting with
"check-lowercase"enforces the rule that the first non-whitespace character of a comment must be lowercase, if applicable
curlyenforces braces forif/for/do/whilestatements.eoflineenforces the file to end with a newline.forinenforces afor ... instatement to be filtered with anifstatement.*indentenforces consistent indentation levels (currently disabled).interface-nameenforces the rule that interface names must begin with a capital 'I'jsdoc-formatenforces basic format rules for jsdoc comments -- comments starting with/**- each line contains an asterisk and asterisks must be aligned
- each asterisk must be followed by either a space or a newline (except for the first and the last)
- the only characters before the asterisk on each line must be whitepace characters
label-positionenforces labels only on sensible statements.label-undefinedchecks that labels are defined before usage.max-line-lengthsets the maximum length of a line.no-argdisallows access toarguments.callee.no-bitwisedisallows bitwise operators.no-consoledisallows access to the specified functions onconsole. Rule options are functions to ban on the console variable.no-consecutive-blank-linesdisallows having more than one blank line in a row in a fileno-constructdisallows access to the constructors ofString,Number, andBoolean.no-debuggerdisallowsdebuggerstatements.no-duplicate-keydisallows duplicate keys in object literals.no-duplicate-variabledisallows duplicate variable declarations.no-emptydisallows empty blocks.no-evaldisallowsevalfunction invocations.no-string-literaldisallows object access via string literals.no-trailing-whitespacedisallows trailing whitespace at the end of a line.no-unreachabledisallows unreachable code afterbreak,catch,throw, andreturnstatements.one-lineenforces the specified tokens to be on the same line as the expression preceding it. Rule options:"check-catch"checks thatcatchis on the same line as the closing brace fortry"check-else"checks thatelseis on the same line as the closing brace forif"check-open-brace"checks that an open brace falls on the same line as its preceding expression."check-whitespace"checks preceding whitespace for the specified tokens.
quotemarkenforces consistent single or double quoted string literals.radixenforces the radix parameter ofparseIntsemicolonenforces semicolons at the end of every statement.triple-equalsenforces === and !== in favor of == and !=.typedefenforces type definitions to exist. Rule options:"callSignature"checks return type of functions"catchClause"checks type in exception catch blocks"indexSignature"checks index type specifier of indexers"parameter"checks type specifier of parameters"propertySignature"checks return types of interface properties"variableDeclarator"checks variable declarations
typedef-whitespaceenforces spacing whitespace for type definitions. Each rule option requires a value of"space"or"nospace"to require a space or no space before the type specifier's colon. Rule options:"callSignature"checks return type of functions"catchClause"checks type in exception catch blocks"indexSignature"checks index type specifier of indexers
use-strictenforces ECMAScript 5's strict modecheck-modulechecks that all top-level modules are using strict modecheck-functionchecks that all top-level functions are using strict mode
variable-nameallows only camelCased or UPPER_CASED variable names. Rule options:"allow-leading-underscore"allows underscores at the beginnning.
whitespaceenforces spacing whitespace. Rule options:"check-branch"checks branching statements (if/else/for/while) are followed by whitespace"check-decl"checks that variable declarations have whitespace around the equals token"check-operator"checks for whitespace around operator tokens"check-separator"checks for whitespace after separator tokens (,/;)"check-type"checks for whitespace before a variable type specification
You can disable/enable tslint inside a file, or some subset of the tslint rules, with the following comment rule flags:
/* tslint:disable */will disable all rules for the rest of the file/* tslint:enable */will enable all rules for the rest of the file/* tslint:disable:rule1 rule2 rule3... */will disable the listed rules for the rest of the file/* tslint:enable:rule1 rule2 rule3... */will enable the listed rules for the rest of the file
Rules flags will enable and disable rules as they are passed, each directive will turn the rules listed in the flag on or off until a later flag turns the rule on or off. Disabling an already disabled rule and enabling an already enabled rule will have no effect.
For instance: imagine we have a file with /* tslint:disable */ on the first line of a file, /* tslint:enable:ban class-name */ on the tenth line and a /* tslint:enable */ on the twentieth. No rules will be checked between the first line and the tenth, only the ban and class-name rules will be checked between the tenth and twentieth, and all rules will be checked for the remainder of the file.
sudo npm install tslint -g
npm install tslint
Please first ensure that the TypeScript source files compile correctly.
usage: tslint
Options:
-c, --config configuration file
-f, --file file to lint [required]
-o, --out output file
-r, --rules-dir rules directory
-s, --formatters-dir formatters directory
-t, --format output format (prose, json) [default: "prose"]
By default, configuration is loaded from tslint.json, if it exists in the current path.
tslint accepts the following commandline options:
-f, --file:
The location of the TypeScript file that you wish to lint. This
option is required.
-c, --config:
The location of the configuration file that tslint will use to
determine which rules are activated and what options to provide
to the rules. If no option is specified, the config file named
tslint.json is used, so long as it exists in the path.
The format of the file is { rules: { /* rules list */ } },
where /* rules list */ is a key: value comma-seperated list of
rulename: rule-options pairs. Rule-options can be either a
boolean true/false value denoting whether the rule is used or not,
or a list [boolean, ...] where the boolean provides the same role
as in the non-list case, and the rest of the list are options passed
to the rule that will determine what it checks for (such as number
of characters for the max-line-length rule, or what functions to ban
for the ban rule).
-o, --out:
A filename to output the results to. By default, tslint outputs to
stdout, which is usually the console where you're running it from.
-r, --rules-dir:
An additional rules directory, for additional user-created rules.
tslint will always check its default rules directory, in
node_modules/tslint/build/rules, before checking the user-provided
rules directory, so rules in the user-provided rules directory
with the same name as the base rules will not be loaded.
-s, --formatters-dir:
An additional formatters directory, for user-created formatters.
Formatters are files that will format the tslint output, before
writing it to stdout or the file passed in --out. The default
directory, node_modules/tslint/build/formatters, will always be
checked first, so user-created formatters with the same names
as the base formatters will not be loaded.
-t, --format:
The formatter to use to format the results of the linter before
outputting it to stdout or the file passed in --out. The core
formatters are prose (human readable) and json (machine readable),
and prose is the default if this option is not used. Additional
formatters can be added and used if the --formatters-dir option
is set.
--help:
Prints this help message.
var options = {
formatter: "json",
configuration: configuration,
rulesDirectory: "customRules/",
formattersDirectory: "customFormatters/"
};
var Linter = require("tslint");
var ll = new Linter(fileName, contents, options);
var result = ll.lint();
git clone git@github.com:palantir/tslint.git
cd tslint
git submodule init
git submodule update
npm install
grunt
- Add more rules from jshint
- Disallow unused variables