This project includes a variety of tslint rules we have found useful for our projects.
Install from npm to your devDependencies (https://www.npmjs.com/package/vrsource-tslint-rules)
npm install --save-dev vrsource-tslint-rules
Configure tslint to use the vrsource-tslint-rules folder:
Add "vrsource-tslint-rules"
to the extends
array of your tslint.json
file:
{
"rulesDirectory": ["vrsource-tslint-rules/rules"]
"rules": {
...
}
}
Now configure some of the new rules.
Rule to enforce the use of parentheses each clause of a conditional when they are not simple expressions of a single indentifier or calling expression.
"conditional-expression-parens": true
This rule provides extensive support for customizing allowable variable names for a wide variety of variable tags. The rule is configured by setting up a list of sub-rules that specify the tags of variables to check and the checks to perform on the variable's name. The sub-rules are checked in order and the first one that matches the tags of variable being checked is the only one that is used.
An example set of sub-rules for an example coding standard is shown below.
"ext-variable-name": [
true,
["class", "pascal"],
["interface", "pascal", {"regex": "^I.*$"}],
["parameter", "camel"],
["property", "static", "camel", {"regex": "^s.*$"}],
["property", "private", "camel", "require-leading-underscore"],
["property", "protected", "camel", "allow-leading-underscore"],
["variable", "local", "snake"],
["variable", "const", "upper"],
["variable", "camel", {"regex": "^g.*$"}],
["method", "private", "camel", "require-leading-underscore"],
["method", "protected", "camel", "allow-leading-underscore"],
["function", "camel"],
["default", "camel"]
]
Allowed tags for variables:
- primary (choose one):
- class, interface, parameter, property, method, function, variable
- modifiers (choose zero or more):
- local, const, static, public, protected, private
note: If any tags is added to a sub-rule then all must match the variable.
Checks allowed:
- One of:
- "camel": Require variables to use camelCaseVariables
- "snake": Require variables to use snake_case_variables
- "pascal": Require variables to use PascalCaseVariables
- "upper": Require variables to use UPPER_CASE_VARIABLES
- "allow-leading-underscore": permits the variable to have a leading underscore
- "allow-trailing-underscore": permits the variable to have a trailing underscore
- "require-leading-underscore": requires the variable to have a leading underscore
- "require-trailing-underscore": requires the variable to have a trailing underscore
- "ban-keywords": bans a list of language keywords from being used
- {"regex": "^.*$"}: checks the variable name against the given regex
Rule to enforce consistent spacing inside array and object literals.
See: eslint: object-curly-spacing and array-bracket-spacing
"literal-spacing": [
true,
{
"array": ["always"],
"object": ["never"],
"import": ["always"]
}
]
Rule to enforce a maximum number of parameters for functions and methods.
"max-params": [
true,
3
]
Rule to enforce various checks arrow functions that span multiple lines.
"multiline-arrow": [
true,
"require-parens",
"require-block"
]
This rule checks to make sure that all case clauses use a block around the statements of the case. This helps to protect against issues with lexcical declarations that would become visible to the entire switch statement.
To maintain consistency, the rule requires a block in all cases.
"prefer-case-blocks": true
Flags locations where code calls "new Object()", "new Array()", "new Function()""
"prefer-literal": [
true,
"object",
"function",
"array"
]
- 6.0.0
- Breaking change: older checks removed in 5.8.0 did not get fully removed. Now they are. Note: 5.8.2 and 5.8.3 also suffer from this breaking change.
- 5.8.3
- Bump release
- 5.8.2
- move tslint and typescript to peerDependencies
- improve method to use as rulesDirectory
- 5.8.1
- Fix bug in spacing rule
- 5.8.0
- Update to tslint 5.8.0
- Remove dot-notation (use no-string-literal instead)
- Remove no-jasmine-focus (use ban instead)
- Remove no-param-reassign (use no-parameter-reassignment instead)
- Remove no-duplicate-import (now in tslint)
- 5.1.1
- Move chokidar-cli to devDependencies
- 5.1.0
- Updated to work with tslint 5.1.0
- Update package.json to include build and test scripts
- Cleaned up the tslint and tsconfig options used
- 4.0.1
- Add "main" entry to package.json to allow importing
- 4.0.0
- Update versioning to track major version of tslint
- Update to tslint 4.x and TS 2.x
- Remove
prefer-const
rule as tslint now supports it
- 0.15.0
- ajaff: Refactor and extend ext-variable-name options
- simplify RuleWalker visitor methods
- add support for validating property accessors
- improve failure messages
- add require-leading/training-underscore options
- validate parameter properties as properties
- ajaff: Refactor and extend ext-variable-name options
- 0.14.1
- Add import options to literal-spacing rule
- 0.13.1
- Fix bugs in ext-variable-name rule.
- variables in constructors didn't get the 'local' tag
- variables could end up with 'public' tag that were not properties or methods
- Fix bugs in ext-variable-name rule.
- 0.13.0
- Add no-jasmine-focus rule.
- 0.12.1
- Fix bug in dot-notation where invalid identifiers were not allowed.
- 0.12.0
- Added literal-spacing rule
- 0.11.0
- Added no-param-reassign rule
- 0.10.0
- Added prefer-case-blocks rule
- 0.9.0
- Add dot-notation rule
- 0.8.0
- Add conditional-expression-parens rule
- 0.7.0
- Add prefer-literal rule
- 0.6.0
- Add max-params rule
- 0.5.0
- Add no-duplicate-imports
- 0.4.0
- Add multiline-arrow rule
- 0.3.0
- Add prefer-const rule
- 0.2.0
- Remove demonstration rule
- 0.1.0
- First version
Contributions are greatly appreciated. Please fork the repository and submit a pull request.
- ajafff
- abdulmoizeng
- timocov
- jayrylan
- ethanresnick
The command npm run test
will run tests locally.
To add a new rule:
- Add the rule's .ts file to the /rules directory
- Add one or more tests in the test/rules directory
- Use run_tests.sh to build and run the tests
- (optional) The .vscode directory includes settings to run and debug rules in vscode.
- Just modify the "args" entry to point to the rule test case to run
Notes for how to build new rules and tests:
- http://palantir.github.io/tslint/develop/custom-rules/
- http://palantir.github.io/tslint/develop/testing-rules/
- AST explorer: https://astexplorer.net/
- Typescript Compiler API: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
- AST Dive: https://basarat.gitbooks.io/typescript/content/docs/compiler/ast.html
- Typescript project rules: microsoft/TypeScript#4458
- https://github.com/mgechev/codelyzer
- https://github.com/Microsoft/tslint-microsoft-contrib
- https://github.com/buzinas/tslint-eslint-rules
- Update version in package.json
- Update Changelog
- git commit
- git tag -a vX.Y.Z -m "Tag Release"
- git push --tags
- npm publish