TSLint config for 5Minds projects
- install with
npm install --save-dev tslint-config-5minds
- create a tslint.json in your project with the following content:
Typescript specific
1.1 adjacent-overload-signatures: Enforces function overloads to be consecutive.
// good
// bad -> [All 'constructor' signatures should be adjacent]
1.2 member-access: - Requires explicit visibility declarations for class members(properties, accessors, class methods).
// good
// bad -> The get property accessor 'one' must be marked either 'private', 'public', or 'protected'
1.3 member-ordering: Enforces member ordering
// good
// bad -> Declaration of protected static field not allowed after declaration of public instance field. Instead, this should come after public static fields.
1.4 no-any: Disallows usages of any
as a type declaration.
// bad -> Type declaration of 'any' is forbidden.;
1.5 no-empty-interface: Forbids empty interfaces.
// good
// bad -> An empty interface is equivalent to '{}'
// bad -> An interface declaring no members is equivalent to its supertype.
1.6 no-internal-module: Disallows internal module. TypeScript introduced the equivalent keyword 'namespace' in order to avoid confusion with ES6 modules.
// bad -> The internal 'module' syntax is deprecated, use the 'namespace' keyword instead.declare
1.7 no-magic-numbers: Disallows the use constant number values outside of variable assignments. When no list of allowed values is specified, -1, 0 and 1 are allowed by default.
// good;console.logleet;
// bad -> 'magic numbers' are not allowed (no-magic-numbers)console.log1337;
1.8 no-namespace: Disallows use of internal modules and namespaces. Use ES6 import/export.
This rule prevents you from describing external modules with declare namespace
but that should be an exception and should never be used in .ts-files anyway.
// bad -> [tslint] 'namespace' and 'module' are disallowed (no-namespace)declare
1.9 no-reference: Disallows /// <reference path="..." /> imports (use ES6-style imports instead).
// bad -> [tslint] <reference> is not allowed, use imports (no-reference)///
1.10 no-var-requires: Disallows the use of require statements except in import statements.
// good;
// bad -> require statement not part of an import statement (no-var-requires);
1.11 only-arrow-functions: Disallows traditional (non-arrow), unnamed function expressions.
// good.map;
// good.map;
// bad -> non-arrow functions are forbidden.map
1.12 prefer-for-of: Recommends a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated.
; // good for of cars
// bad -> Expected a 'for-of' loop instead of a 'for' loop with this simple iteration for ; i < cars.length; i++
1.13 typedef: Requires type definitions to exist.
// good
// bad -> expected parameter: 'bar' to have a typedef (typedef) & expected call-signature: 'foo' to have a typedef (typedef)
1.14 typedef-whitespace: Requires or disallows whitespace for type definitions. You can define whether there is one or no space between the definition's colon and the previous or following symbol.
// good;
// bad -> [tslint] expected nospace before colon in variable-declaration (typedef-whitespace);
// bad -> [tslint] expected onespace after colon in variable-declaration (typedef-whitespace);
1.15 unified-signatures: Warns for any two overloads that could be unified into one by using a union or an optional/rest parameter.
// good;
// bad -> These overloads can be combined into one signature taking `number | string | number`.;;
Functionality
2.1 curly: Enforces braces for if
/for
/do
/while
statements.
// goodif result.type === 'JSON'
// bad -> if statements must be bracedif result.type === 'JSON' return res;
2.2 import-blacklist:
Disallows importing the specified modules directly via import
and require
.
Instead only sub modules may be imported from that module.
Some libraries allow importing their submodules instead of the entire module. This is good practise as it avoids loading unused modules.
// good;
// bad -> This import is blacklisted, import a submodule instead;
2.3 label-position: Only allows labels in sensible locations(like switch-cases).
Labels can be used in combination with break
, continue
and switch
.
// bad -> unexpected label on statement
2.4 no-arg: Disallows use of arguments.callee
.
Usage of arguments.callee
makes many optimizations impossible and almost every case can be expressed without.
// good.map;
// bad -> Access to arguments.callee is forbidden.map;
2.5 no-bitwise: Disallows bitwise operators. Bitwise operations are often typos(& instead of &&) and when used correctly they make code harder to understand.
// bad -> Forbidden bitwise operatoration.;
2.6 no-conditional-assignment: Disallows any type of assignment in conditionals.
That happens mostly when you confuse =
with ==
.
// bad -> Assignments in conditional expressions are forbiddenif i = true
2.7 no-console: Bans the use of specified console
methods.
// bad -> Calls to 'console.log' are not allowed.console.log'veritas numquam perit';
2.8 no-construct: Disallows access to the constructors of String
, Number
, and Boolean
.
The mentioned above classes are wrappers. Strings are not actually objects in JavaScript. Whenever you call a function on a string(for example charAt
) the compiler wraps that string into the String Wrapper beforehand. Using wrappers manually never makes sense.
// bad -> Forbidden constructor, use a literal or simple function call insteadnew String'foo' === 'foo' //sidenote: this expression equals false
2.9 no-debugger: Disallows debugger
statements.
// bad -> Use of debugger statements is forbiddendebugger;
2.10 no-duplicate-variable: Disallows duplicate variable declarations in the same block scope.
// bad -> Cannot redeclare block-scoped variable 'two'.;;
2.11 no-empty: Disallows empty blocks.
// bad -> block is empty
2.12 no-eval: Disallows eval
function invocations.
Eval
is often dangerous, slow and in most cases not required.
// forbidden evaleval"2+2" + 2 === 6
2.13 no-invalid-this: Disallows using the this
keyword outside of classes.
// good
// bad -> the "this" keyword is disallowed outside of a class bodyconsole.logthis === window;
2.14 no-misused-new: Warns on apparent attempts to define constructors for interfaces or new
for classes.
// bad -> Interfaces cannot be constructed, only classes. Did you mean `declare class`?
// bad -> `new` in a class is a method named "new". Did you mean `constructor`?declare
2.15 no-shadowed-variable: Disallows shadowing variable declarations.
// bad -> Shadowed variable: 'a'
2.16 no-string-throw: Flags throwing plain strings or concatenations of strings because only Errors produce proper stack traces.
// good
// bad -> Throwing plain strings (not instances of Error) gives no stack traces
2.17 no-unused-expression: Disallows unused expression statements. Unused expressions are expression statements which are not assignments or function calls.
// bad -> expected an assignment or function calltrue;
2.18 no-use-before-declare: Disallows usage of variables before their declaration.
// bad -> variable 'lorem' used before declarationconsole.loglorem;;
2.19 no-var-keyword: Disallows usage of the var
keyword.
// good;
// good;
// bad -> Forbidden 'var' keyword, use 'let' or 'const' instead;
2.20 switch-default: Require a default
case in all switch
statements.
// goodswitch new Date.getDay { case 0: day = "Monday"; break; case default: day = "Okay Day";}
// bad -> Switch statement should include a 'default' caseswitch new Date.getDay { case 0: day = "Monday"; break;}
2.21 triple-equals: Requires ===
and !==
in place of ==
and !=
.
// goodparseInt'2' === 2;
// bad -> == should be ===`2` == 2;
2.22 typeof-compare: Makes sure result of typeof
is compared to correct string values
// bad -> 'typeof' expression must be compared to one of: "undefined", "string", "boolean", "number", "function", "object", "symbol"typeof new Date === 'date';
2.23 use-isnan: Enforces use of the isNaN()
function to check for NaN references instead of a comparison to the NaN
constant.
// goodisNaN'2' + 2;
// bad -> Found an invalid comparison for NaN: ('2' + 2) === NaN'2' + 2 === NaN;
Maintainability
3.1 cyclomatic-complexity: Enforces a threshold of cyclomatic complexity(McCabe complexity).
Does not refer to the number of instructions but rather to the number of paths.
Influenced by switchcases, catch
, if
and ? :
||
and &&
and any kind of loop.
// bad -> The function numberIsEven has a cyclomatic complexity of 21 which is higher than the threshold of 20
3.2 eofline: Ensures the file ends with a newline. Posix defines a line as a number of characters ending with the newline. Most software will recognize the last line anyways, but you should not rely on that.
3.3 indent: Enforces indentation spaces. As soon as this rule supports setting the number of spaces per indentation it will be set to 4 spaces in the 5Minds-tslint-config.
3.4 linebreak-style: Enforces a consistent linebreak style. You may choose between CRLF
and LF
, referencing the control characters carriage return \r
and `line feed (\n).
Windows uses CRLF; Unix uses LF.
3.5 max-classes-per-file: A file may not contain more than the specified number of classes
// bad -> [tslint] A maximum of 1 class per file is allowed (max-classes-per-file)
3.6 max-file-line-count: Requires files to remain under 3000 lines
3.7 max-line-length: Requires lines to be under a certain max length.
// bad -> Exceeds maximum line length of 150;
3.8 no-default-export: Disallows default exports in ES6-style modules.
// good
// bad -> Use of default exports is forbidden
3.9 no-mergeable-namespace: Disallows mergeable namespaces in the same file.
// bad -> Mergeable namespace RegExpNamespace found. Merge its contents with the namespace on line 0.
3.10 no-require-imports: Disallows invocation of require()`.
// good;
// bad -> require() style import is forbidden;
// bad -> require() style import is forbidden;
3.11 no-trailing-whitespace: Disallows trailing whitespace at the end of a line.
3.12 prefer-const: Requires that variable declarations use const
instead of let
if possible.
3.13 trailing-comma: Requires or disallows trailing commas in array and object literals, destructuring assignments, function and tuple typings, named imports and function parameters.
;
;
Style
4.1 align: Enforces vertical alignment.
// good;
// bad -> parameters are not aligned;
4.2 array-type: Requires using Array<T>
for arrays.
// bad -> Array type using 'T[]' is forbidden. Use 'Array<T>' instead.;
4.3 arrow-parens: Requires parentheses around the parameters of arrow function definitions.
// bad -> Parentheses are required around the parameters of an arrow function definition
4.4 class-name: Enforces PascalCased class and interface names.
// good
// bad -> Class name must be in pascal case
// bad -> Class name must be in pascal case
4.5 comment-format: Enforces formatting rules for single-line comments.
// good// I start with a space
// bad -> comment must start with a space//I start directly
4.6 import-spacing: Ensures proper spacing between import statement keywords
// good;
// bad -> Add space after 'import'// bad -> Add space after '*';
4.7 new-parens: Requires parentheses when invoking a constructor via the new
keyword.
// good; // bad -> Parentheses are required when invoking a constructorfoo = new Foo;
4.8 no-consecutive-blank-lines: Disallows one or more blank lines in a row.
4.9 no-unnecessary-initializer: Forbids a ‘var’/’let’ statement or destructuring initializer to be initialized to ‘undefined’.
// good;
// good;
// bad -> Unnecessary initialization to 'undefined'.;
4.10 object-literal-key-quotes: Enforces consistent object literal property quote style.
;
4.11 one-line: Requires the specified tokens to be on the same line as the expression preceding them.
// goodtry catch error
// bad -> misplaced 'catch'try catch error
4.12 one-variable-per-declaration: Disallows multiple variable definitions in the same declaration statement.
// good;;
// bad -> Multiple variable declarations in the same statement are forbidden;
4.13 ordered-imports: Requires that import statements be alphabetized.
// good;;
// bad -> Import sources within a group must be alphabetized.;;
4.14 prefer-method-signature: Prefer foo(): void
over foo: () => void
in interfaces and types.
// good
// bad -> Use a method signature instead of a property signature of function type.
4.15 quotemark: Requires single or double quotes for string literals.
// good;
// good -> if you want to use singlequotes within a string;
// bad -> " should be ';
// bad -> " should be ';
4.16 semicolon: Enforces consistent semicolon usage at the end of every statement.
// good;
// bad -> lack of semicolon
4.17 space-before-function-paren: Require or disallow a space before function parenthesis
// good
// bad -> Spaces before function parens are disallowed
4.18 variable-name: Checks variable names for various errors.
// good;
// good;
// good;
// bad -> variable name clashes with keyword/type;
// bad -> variable name clashes with keyword/type;
// bad -> variable name must be in camelcase or uppercase;
// bad -> variable name must be in camelcase or uppercase;
4.19 whitespace: Enforces whitespace style conventions.
// goodif true
// bad -> missing whitespaceiffalse