UNPKG

4.62 kBMarkdownView Raw
1tslint-clean-code [![Build Status](https://travis-ci.org/Glavin001/tslint-clean-code.svg?branch=master)](https://travis-ci.org/Glavin001/tslint-clean-code) [![Build status](https://ci.appveyor.com/api/projects/status/8femxsete95is18a/branch/master?svg=true)](https://ci.appveyor.com/project/Glavin001/tslint-clean-code/branch/master)
2======
3
4> A set of [TSLint](https://github.com/palantir/tslint) rules used to enforce [Clean Code](https://www.amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882) practices. Inspired by [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882).
5
6:point_right: Sign up for [**CodePass**, *the Quickest Way To Solve Your Coding Errors*](https://codepass.ca/)! :boom:
7
8## Installation
9
10```bash
11npm install tslint-clean-code
12```
13
14## Configuration
15
16##### Configure `tslint.json`
17
18In your `tslint.json` file, extend this package.
19For example:
20
21```json
22{
23 "extends": [
24 "tslint-clean-code"
25 ],
26 "rules": {
27 "newspaper-order": true
28 }
29}
30```
31
32You can also extend other tslint config packages to combine this plugin with other community custom rules.
33
34##### Configure your Grunt build task
35
36Add the new rulesDirectory to your tslint task:
37
38 grunt.initConfig({
39 tslint: {
40 options: {
41 rulesDirectory: 'node_modules/tslint-clean-code/dist/src',
42 configuration: grunt.file.readJSON("tslint.json")
43 },
44 files: {
45 src: ['src/file1.ts', 'src/file2.ts']
46 }
47 }
48 })
49
50The tslint.json file does not change format when using this package. Just add our rule definitions to your existing tslint.json file.
51
52## Supported Rules
53
54Rule Name | Description | Since
55:---------- | :------------ | -------------
56`id-length` | Enforces a minimum and/or maximum identifier length convention. | 0.1.0
57`try-catch-first` | Try-catch blocks must be first within the scope. Try-catch blocks are transactions and should leave your program in a consistent state, no matter what happens in the try. | 0.1.0
58`max-func-args` | Limit the number of input arguments for a function. The ideal number of arguments for a function is zero (niladic). | 0.1.0
59`min-class-cohesion` | The more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive. We would like cohesion to be high. When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole. | 0.1.0
60`newspaper-order` | We would like a source file to be like a newspaper article. Detail should increase as we move downward, until at the end we find the lowest level functions and details in the source file. | 0.1.0
61`no-flag-args` | Functions should only do one thing, therefore passing a boolean into a function is a bad practice. The function does one thing if the flag is true and another if the flag is false! | 0.1.0
62`no-for-each-push` | Enforce using `Array.prototype.map` instead of `Array.prototype.forEach` and `Array.prototype.push`. | 0.1.0
63`no-feature-envy` | A method accesses the data of another object more than its own data. | 0.1.8
64`no-map-without-usage` | Ensure results of `Array.prototype.map` is either assigned to variable or returned | 0.1.0
65`no-complex-conditionals` | Enforce the maximum complexity of conditional expressions. | 0.1.0
66`prefer-dry-conditionals` | Don't-Repeat-Yourself in if statement conditionals, instead use Switch statements. | 0.1.0
67`no-commented-out-code` | Code must not be commented out. | 0.2.0
68
69## Development
70
71To develop tslint-clean-code simply clone the repository, install dependencies and run grunt:
72
73```
74git clone git@github.com:Glavin001/tslint-clean-code.git --config core.autocrlf=input --config core.eol=lf
75cd tslint-clean-code
76npm install
77grunt all
78grunt create-rule --rule-name=no-something-or-other
79```
80
81## Debug code
82
83If command fails because of file access permissions, prefix it with sudo.
84
85```bash
86npm install -g node-inspector
87```
88
89Then run:
90
91```bash
92node-debug grunt mochaTest
93```
94
95The `node-debug` command will load Node Inspector in your default browser (works in Chrome and Opera only).
96
97Set a breakpoint somewhere in your code and resume execution. Your breakpoint should be hit.
98
99## Thank you
100
101Thank you to maintainers of [tslint-microsoft-contrib](https://github.com/Microsoft/tslint-microsoft-contrib), from which this repository was forked. The initial structure was kept and new rules were added, this would not have been possible without Microsoft's awesome work!