📚 Technical Documentation
Section: Usage - Linting 🛠️
Overview
Linting is an essential part of maintaining code quality and consistency in any project. In this Angular-based frontend application, TSLint is used for linting TypeScript code. The linting rules are defined in the tslint.json file to enforce coding standards and catch potential errors during development.
Configuration File: tslint.json
This file contains the configuration for TSLint, which extends the tslint:recommended ruleset and customizes additional rules to suit the project's requirements.
{
"extends": "tslint:recommended",
"rules": {
"align": {
"options": ["parameters", "statements"]
},
"array-type": false,
"arrow-return-shorthand": true,
"curly": true,
"deprecation": {
"severity": "warning"
},
"component-class-suffix": true,
"contextual-lifecycle": true,
"directive-class-suffix": true,
"directive-selector": [
true,
"attribute",
"app",
"camelCase"
],
"component-selector": [
true,
"element",
"app",
"kebab-case"
],
"eofline": true,
"import-blacklist": [true, "rxjs/Rx"],
"import-spacing": true,
"indent": {
"options": ["spaces"]
},
"max-classes-per-file": false,
"max-line-length": [true, 140],
"member-ordering": [
true,
{
"order": ["static-field", "instance-field", "static-method", "instance-method"]
}
],
"no-console": [true, "debug", "info", "time", "timeEnd", "trace"],
"no-empty": false,
"no-inferrable-types": [true, "ignore-params"],
"no-non-null-assertion": true,
"no-redundant-jsdoc": true,
"no-switch-case-fall-through": true,
"no-var-requires": false,
"object-literal-key-quotes": [true, "as-needed"],
"quotemark": [true, "single"],
"semicolon": {
"options": ["always"]
},
"space-before-function-paren": {
"options": {
"anonymous": "never",
"asyncArrow": "always",
"constructor": "never",
"method": "never",
"named": "never"
}
},
"typedef": [true, "call-signature"],
"typedef-whitespace": {
"options": [
{
"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"
}
]
},
"variable-name": {
"options": ["ban-keywords", "check-format", "allow-pascal-case"]
},
"whitespace": {
"options": [
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type",
"check-typecast"
]
},
"no-conflicting-lifecycle": true,
"no-host-metadata-property": true,
"no-input-rename": true,
"no-inputs-metadata-property": true,
"no-output-native": true,
"no-output-on-prefix": true,
"no-output-rename": true,
"no-outputs-metadata-property": true,
"template-banana-in-box": true,
"template-no-negated-async": true,
"use-lifecycle-interface": true,
"use-pipe-transform-interface": true
},
"rulesDirectory": ["codelyzer"]
}
How to Run Linting
To check your code for linting errors, use the following command:
npm run lint
This command will execute the linting process based on the rules defined in tslint.json. If any issues are detected, they will be displayed in the terminal for correction.
Key Rules and Their Purpose
| Rule | Purpose |
|---|---|
align |
Ensures proper alignment of parameters and statements for better readability. |
arrow-return-shorthand |
Enforces concise arrow function return syntax. |
curly |
Requires curly braces for all control statements (e.g., if, for). |
deprecation |
Warns about the use of deprecated APIs or features. |
component-class-suffix |
Ensures that component classes have a specific suffix (Component). |
max-line-length |
Limits the maximum length of a line of code to 140 characters. |
no-console |
Disallows the use of certain console methods (e.g., debug, info). |
quotemark |
Enforces the use of single quotes for string literals. |
semicolon |
Requires semicolons at the end of statements. |
variable-name |
Enforces consistent variable naming conventions. |
Why Linting Matters 🌟
- Consistency: Ensures uniform coding style across the team.
- Error Detection: Identifies potential bugs and anti-patterns early.
- Readability: Improves code readability and maintainability.
- Best Practices: Encourages the use of modern and efficient coding practices.
Integration with Build Process
Linting can be integrated into the build process to ensure that only clean and error-free code is deployed. This can be configured in CI/CD pipelines to automate the linting step.
Conclusion
By adhering to the linting rules defined in tslint.json, the project ensures high code quality, consistency, and adherence to best practices. Regular linting helps developers catch issues early and maintain a clean codebase.
File Reference:
tslint.json【4:0†source】