Shared TSLint & codelyzer rules to enforce a consistent code style for Angular development
Please support this project by simply putting a Github star. Share this library with friends on Twitter and everywhere else you can.
The value of the software produced is directly affected by the quality of the codebase, and not every developer might
- be aware of the potential pitfalls of certain constructions in TypeScript,
- be introduced into certain conventions when using the Angular framework,
- know that not every developer is as capable in understanding an elegant (but abstract) solution as the original developer.
For that purpose, we need to use static code analysis tools such as TSLint and codelyzer to check readability, maintainability, and functionality errors.
Although complying with these tools may seem to appear as undesired overhead or may limit creativity, it becomes easier for any new developers to read, preventing a lot of time/frustration spent figuring out the structure and characteristics of the code.
Containing a set of TSLint and codelyzer rules, angular-tslint-rules has been compiled using many contributions
from colleagues, commercial/open-source projects and some other sources from the Internet, as well as years of development
using the Angular framework.
If you have questions, comments or suggestions, just create an issue on this repository. I'll try to revise and republish these rules with new insights, experiences and remarks in alignment with the updates on TSLint and codelyzer.
Note: The following set of rules depend on:
- Getting started
- Usage
- Rules
- Contributing
- License
You can install angular-tslint-rules using npm
npm install angular-tslint-rules --save
Note: You should have already installed TSLint and codelyzer.
To use these TSLint rules, use configuration inheritance via the extends keyword.
A sample configuration is shown below, where tslint.json lives adjacent to your node_modules folder:
{
"rulesDirectory": [
"node_modules/codelyzer"
],
"extends": ["angular-tslint-rules"],
"rules": {
// override tslint rules here
...
}
}- Do not use classes unless required.
"no-unnecessary-class": [
true,
"allow-constructor-only",
"allow-static-only"
]- Do not specify the
publickeyword (this is the default accessibility level). privateandprivate staticmembers in classes should be denoted with theprivatekeyword.
"member-access": [
true,
"no-public"
]- Member ordering should be in the following way:
- Public members should be ordered before private members.
- Static members should be ordered before instance members.
- Variables should be ordered before functions.
"member-ordering": [
true,
"public-before-private",
"static-before-instance",
"variables-before-functions"
]- Member overloads should be consecutive.
"adjacent-overload-signatures": true- Always prefer unifying any two overloads into one, by using a union or an optional/rest parameter.
"unified-signatures": true- Always prefer functions over
privatemembers that do not usethis.
"prefer-function-over-method": [
true,
"allow-public",
"allow-protected"
]- Do not use the
thiskeyword outside class context (including functions in methods).
"no-invalid-this": [
true,
"check-function-in-method"
]- Do not invoke the
supermethod twice in a constructor (except in branched statements or nested class constructors).
"no-duplicate-super": true- Always prefer parentheses
()when invoking a constructor via the new keyword.
"new-parens": true- Do not define
newfor classes.
"no-misused-new": true- Do not use the constructors of
String,Number, andBoolean.
"no-construct": true- Do not define constructors for interfaces.
- Do not use empty interfaces.
"no-empty-interface": true- Always prefer
foo(): voidoverfoo: () => voidin interfaces and types.
"prefer-method-signature": true- Always prefer an interface declaration over a type literal (
type T = { ... }).
"interface-over-type-literal": true- Functions should be defined right after the variable declarations.
- Do not invoke
arguments.calleewithin a function, as it makes impossible various performance optimizations.
"no-arg": true- Always prefer defining anonymous functions as fat-arrow/lambda
() => { }functions (unless it is absolutely necessary to preserve the context in the function body).
"only-arrow-functions": [
true,
"allow-declarations",
"allow-named-functions"
]- fat-arrow/lambda functions should have parenthesis
()around the function parameters (except if removing them is allowed by TypeScript).
"arrow-parens": [
true,
"ban-single-arg-parens"
]- Always prefer
() => xover() => { return x; }.
"arrow-return-shorthand": true- Do not use the unnecessary
return await.
"no-return-await": true- Always prefer
constkeyword where appropriate, for values that should never change. - Then prefer
leteverywhere else. - Do not use the
varkeyword.
"prefer-const": true- Do not use implied global variables.
- Do not define a variable on the global scope from within a smaller scope.
"no-shadowed-variable": true- Declare one variable at a time (except in loops).
"one-variable-per-declaration": [
true,
"ignore-for-loop"
]- Do not use duplicate variable names in the same block scope.
"no-duplicate-variable": [
true,
"check-parameters"
]- Do not use a
var/letstatement or destructuring initializer to be initialized toundefined.
"no-unnecessary-initializer": true- Do not import modules that are not listed as a dependency in the project's
package.json.
"no-implicit-dependencies": true- Always use the
importstatement keywords in alphabetical order.
"ordered-imports": [
true,
{
"import-sources-order": "any",
"named-imports-order": "case-insensitive",
"grouped-imports": true
}
]- Always use a single
importstatement per module.
"no-duplicate-imports": true- Always
importsubmodules fromrxjs.
"import-blacklist": [
true,
"rxjs"
]- Do not use
requirestatements at all (use ES6-styleimportstatement instead).
"no-require-imports": true- Do not use default exports in ES6-style modules.
"no-default-export": true- Do not use
/// <reference path=> importsstatements at all (use ES6-styleimportstatement instead).
"no-reference": true- Always prefer defining the return type of functions, methods and property declarations explicitly.
- Types should be used whenever necessary (no implicit
any).
"typedef": [
true,
"call-signature",
"property-declaration"
]- Always prefer type inference over explicit type declaration (except for function return types).
"no-inferrable-types": true- Always prefer the use of
as Typefor type assertions over<Type>.
"no-angle-bracket-type-assertion": true- Always prefer writing an interface or literal type with just a call signature as a function type.
"callable-types": true- Do not use the
nullkeyword, always returnundefinedinstead of anullreference.
"no-null-keyword": true- Do not use non-null assertions.
"no-non-null-assertion": true- Arrays should be defined as
Array<type>instead oftype[].
"array-type": [
true,
"generic"
]- Always prefer ES6 object spread operator whenever possible.
"prefer-object-spread": true- Always prefer ES6 object literal shorthand whenever possible.
"object-literal-shorthand": true- Always prefer object property names using literals whenever possible.
"object-literal-key-quotes": [
true,
"as-needed"
]- Always prefer single-quotes
''for all strings, and use double-quotes""for strings within strings.
"quotemark": [
true,
"single",
"avoid-template",
"avoid-escape"
]- Always prefer template expressions over string literal concatenation.
"prefer-template": true- Do not use string templates outside template strings (``).
"no-invalid-template-strings": true- Always prefer using
===and!==operators whenever possible.
==and!=operators do type coercion, which can lead to headaches when debugging code.
"triple-equals": [
true,
"allow-null-check"
]- In a binary expression, a literal should always be on the right-hand side.
"binary-expression-operand-order": true- Do not use bitwise operators.
"no-bitwise": true- Always prefer using
isNanfunction to checkNaNreferences.
"use-isnan": true- Assignment expressions inside of the condition block of
if,while, anddo whilestatements should be avoided.
"no-conditional-assignment": true- Always prefer conditional expression over a standard
ifstatement.
"prefer-conditional-expression": [
true,
"check-else-if"
]- Always prefer
for-ofloop over a standardforloop.
"prefer-for-of": true- Filter
for-instatements with anifstatement (this prevents accidental iteration over properties inherited from an object???s prototype).
"forin": true- Each switch statement should have a default case.
"switch-default": true- Each switch case except default should end with
break,return, orthrow.
"no-switch-case-fall-through": true- Do not use duplicate switch cases.
"no-duplicate-switch-case": true- Do not use control flow statements, such as
return,continue,breakandthrowsinfinallyblocks.
"no-unsafe-finally": true- Use UTF-8 encoding.
"encoding": true- Limit the level of complexity (cyclomatic complexity) in a function/method by 20 branches.
"cyclomatic-complexity": [
true,
20
]- Files should not exceed 1500 lines of code.
"max-file-line-count": [true, 1500]- Keep the length of each line under 140 characters.
"max-line-length": [
true,
140
]- Use 2 spaces for indentation.
"indent": [
true,
"spaces",
2
]- All files should end in a new line.
"eofline": trueUse curly braces as needed.
"curly": [
true,
"as-needed"
]Whitespaces should be used in the following circumstances:
- All branching statements (
if/else/for/while) should be followed by one space. - Variable declarations should be separated by one space around the type specification and equals token.
- There should be one space between the typecast and its target.
- All operators except the period
., left parenthesis(, and left bracket[should be separated from their operands by one space. - There should be no space between the unary/incremental operators
!x, -x, +x, ~x, ++x, --xand its operand. - There should be one space between the type operators
|,&and its operand. - There should be one space after the left curly brace
{and before the right curly brace}containingimportstatement keywords. - Each separator (
,,;) in the control part of aforstatement should be followed with one space. - There should be no space after the rest/spread operator
.... - The left curly brace
{followed by a right parenthesis)should always separated by one space.
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-separator",
"check-rest-spread",
"check-type",
"check-typecast",
"check-type-operator",
"check-preblock"
]- For each call signature, index signature, parameter, property declaration and variable declaration;
- There should be no space between the parameter and the colon
:indicating the type declaration. - There should be one space between the colon
:and the type declaration.
- There should be no space between the parameter and the colon
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
},
{
"call-signature": "onespace",
"index-signature": "onespace",
"parameter": "onespace",
"property-declaration": "onespace",
"variable-declaration": "onespace"
}
]- For each function, class member and constructor;
- There should be no space between the name of the function/member and the left parenthesis
(of its parameter list.
- There should be no space between the name of the function/member and the left parenthesis
- For each fat-arrow/lambda function;
- There should be one space between the right parenthesis
)and the=>.
- There should be one space between the right parenthesis
"space-before-function-paren": [
true,
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always",
"method": "never",
"constructor": "never"
}
]- There should be no space within parenthesis.
"space-within-parens": 0- Put one space between the
importstatement keywords.
"import-spacing": true- Do not use trailing whitespaces at the end of a line
"no-trailing-whitespace": trueEmpty lines improve code readability by allowing the developer to logically group code blocks.
- There should be an empty line before the return statement.
"newline-before-return": true- For each function, anonymous function, class member, constructor,
else,catchandfinallystatements;- There should be one space between the right parenthesis
)and the left curly{brace that begins the statement body.
- There should be one space between the right parenthesis
- For each fat-arrow/lambda function;
- There should be one space between the
=>and the left curly brace{that begins the statement body.
- There should be one space between the
elsestatements should indented to align with the line containing the closing brace for theifstatement.catchandfinallystatements should indented to align with the line containing the closing brace for thetrystatement.
"one-line": [
true,
"check-open-brace",
"check-whitespace",
"check-else",
"check-catch",
"check-finally"
]- Do not use more than one empty line in a row.
"no-consecutive-blank-lines": [
true,
1
]- A semicolon should be placed at the end of every simple statement (except at the end of bound class methods).
"semicolon": [
true,
"always",
"strict-bound-class-methods"
]- Vertically align parameters and statements (helps maintain a readable, consistent style in your codebase).
"align": [
true,
"parameters",
"statements"
]- Always prefer trailing commas in array and object literals, destructuring assignments, function typings, named imports/exports and function parameters.
"trailing-comma": [
true,
{
"multiline": "never",
"singleline": "never",
"esSpecCompliant": true
}
]- Class and interface names should be in PascalCase.
"class-name": true- All variable, and function names should be in camelCase.
- Do not use trailing underscore
_characters.
"variable-name": [
true,
"check-format",
"allow-leading-underscore",
"ban-keywords"
]- Always prefer
//for all inline comments. - There should be one space before the comment.
"comment-format": [
true,
"check-space"
]- JSDoc comments should start with
/**and end with*/.
"jsdoc-format": [
true,
"check-multiline-start"
]- Do not use redundant JSDoc comments.
"no-redundant-jsdoc": true- Do not use the
consolemethod (such messages are considered to be for debugging purposes and therefore might ship to the production environment).
"no-console": [
true,
"log",
"debug",
"info",
"time",
"timeEnd",
"trace"
]- Do not use the
debuggerstatement (this might cause the environment to stop execution and start up a debugger, if not omitted on the production code).
"no-debugger": true- Do not use the
evalfunction (usingevalon untrusted code might open a program up to several different injection attacks).
"no-eval": true- Do not throw plain strings or concatenations of strings (because only
Errors produce proper stack traces).
"no-string-throw": true- Do not use namespaces (using
namespace {}is outdated).
"no-namespace": true- Do not use internal modules (using
module {}is outdated).
"no-internal-module": true- Always prefer the
radixparameter to be specified when callingparseInt.
"radix": true- Do not leave unused expressions in the code.
"no-unused-expression": [
true,
"allow-fast-null-checks"
],- Do not use empty blocks
{}in the code.
"no-empty": true- Do not use missing elements in arrays.
"no-sparse-arrays": true"angular-whitespace": [
true,
"check-interpolation",
"check-semicolon"
],
"banana-in-box": true,
"templates-no-negated-async": true,
"directive-selector": [
true,
"attribute",
[
"ngx",
"test"
],
"camelCase"
],
"component-selector": [
true,
"element",
[
"ngx",
"test"
],
"kebab-case"
],
"use-input-property-decorator": true,
"use-output-property-decorator": true,
"use-host-property-decorator": true,
"use-view-encapsulation": true,
"no-attribute-parameter-decorator": true,
"no-input-rename": true,
"no-output-rename": true,
"no-output-on-prefix": true,
"no-forward-ref": true,
"use-life-cycle-interface": true,
"contextual-life-cycle": true,
"trackBy-function": true,
"use-pipe-transform-interface": true,
"pipe-naming": [
true,
"camelCase",
"ngx"
],
"component-class-suffix": true,
"directive-class-suffix": true,
"pipe-impure": trueIf you want to file a bug, contribute some code, or improve documentation, please read up on the following contribution guidelines:
The MIT License (MIT)
Copyright (c) 2017 Burak Tasci