Shared TSLint config to enforce a consistent code style for LoopBack development
npx @tessl/cli install tessl/npm-loopback--tslint-config@2.1.00
# @loopback/tslint-config
1
2
@loopback/tslint-config provides shared TSLint configurations to enforce consistent code style and quality standards for LoopBack TypeScript development. It offers two main configuration files: a core common configuration for essential development-time linting, and an extended build configuration that includes type-checking rules for comprehensive build-time validation.
3
4
## Package Information
5
6
- **Package Name**: @loopback/tslint-config
7
- **Package Type**: npm
8
- **Language**: TypeScript (TSLint configuration)
9
- **Installation**: `npm install @loopback/tslint-config`
10
11
## Core Imports
12
13
This package doesn't provide programmatic imports. Instead, configurations are consumed through TSLint's `extends` mechanism:
14
15
**Basic tslint.json:**
16
```json
17
{
18
"$schema": "http://json.schemastore.org/tslint",
19
"extends": ["@loopback/tslint-config/tslint.common.json"]
20
}
21
```
22
23
**Build-time tslint.build.json:**
24
```json
25
{
26
"$schema": "http://json.schemastore.org/tslint",
27
"extends": ["@loopback/tslint-config/tslint.build.json"]
28
}
29
```
30
31
## Basic Usage
32
33
After installing the package, configure TSLint to extend the LoopBack configurations:
34
35
**For development-time linting (editor integration):**
36
```json
37
{
38
"$schema": "http://json.schemastore.org/tslint",
39
"extends": ["@loopback/tslint-config/tslint.common.json"],
40
"rules": {
41
// Add any project-specific rule overrides here
42
}
43
}
44
```
45
46
**For comprehensive build-time linting:**
47
```json
48
{
49
"$schema": "http://json.schemastore.org/tslint",
50
"extends": ["@loopback/tslint-config/tslint.build.json"],
51
"rules": {
52
// Add any project-specific rule overrides here
53
}
54
}
55
```
56
57
## Capabilities
58
59
### Common Configuration (tslint.common.json)
60
61
Core TSLint configuration with essential rules for TypeScript development that work without type checking.
62
63
```json { .api }
64
{
65
"$schema": "http://json.schemastore.org/tslint",
66
"rulesDirectory": ["tslint-consistent-codestyle"],
67
"rules": {
68
"adjacent-overload-signatures": true,
69
"prefer-for-of": true,
70
"unified-signatures": true,
71
"no-any": true,
72
"label-position": true,
73
"no-arg": true,
74
"no-construct": true,
75
"no-duplicate-variable": true,
76
"no-invalid-this": true,
77
"no-misused-new": true,
78
"no-shadowed-variable": true,
79
"no-string-throw": true,
80
"no-unused": [true, "ignore-parameters"],
81
"no-unused-expression": true,
82
"no-var-keyword": true,
83
"triple-equals": [true, "allow-null-check", "allow-undefined-check"]
84
}
85
}
86
```
87
88
**Configuration Path:** `@loopback/tslint-config/tslint.common.json`
89
90
**Rule Categories:**
91
- **TypeScript Features**: `adjacent-overload-signatures`, `prefer-for-of`, `unified-signatures`, `no-any`
92
- **Error Prevention**: `label-position`, `no-arg`, `no-construct`, `no-duplicate-variable`, `no-invalid-this`, `no-misused-new`, `no-shadowed-variable`, `no-string-throw`
93
- **Code Quality**: `no-unused`, `no-unused-expression`, `no-var-keyword`, `triple-equals`
94
95
**Dependencies Required:** `tslint-consistent-codestyle` plugin for enhanced code style consistency.
96
97
### Build Configuration (tslint.build.json)
98
99
Extended TSLint configuration that includes all common rules plus additional type-checking rules for build-time validation.
100
101
```json { .api }
102
{
103
"$schema": "http://json.schemastore.org/tslint",
104
"extends": ["./tslint.common.json"],
105
"rules": {
106
"await-promise": [true, "PromiseLike", "RequestPromise"],
107
"no-floating-promises": [true, "PromiseLike", "RequestPromise"],
108
"no-unused-variable": false,
109
"no-void-expression": [true, "ignore-arrow-function-shorthand"]
110
}
111
}
112
```
113
114
**Configuration Path:** `@loopback/tslint-config/tslint.build.json`
115
116
**Inherits:** All rules from `tslint.common.json`
117
118
**Additional Rules:**
119
- **Promise Handling**: `await-promise`, `no-floating-promises` (supports PromiseLike and RequestPromise types)
120
- **Expression Rules**: `no-void-expression` with arrow function shorthand support
121
- **Rule Overrides**: Explicitly disables `no-unused-variable` in favor of the `no-unused` rule from common config
122
123
**Use Case:** Build pipelines and comprehensive type-checking validation where full TypeScript compilation context is available.
124
125
## Configuration Details
126
127
### Rule Descriptions
128
129
**TypeScript-specific rules:**
130
- `adjacent-overload-signatures`: Ensures overloaded functions are grouped together
131
- `prefer-for-of`: Recommends for-of loops over traditional for loops when possible
132
- `unified-signatures`: Warns for overloads that could be unified into a single signature
133
- `no-any`: Prohibits usage of the `any` type
134
135
**Error prevention rules:**
136
- `no-arg`: Disallows access to `arguments.callee`
137
- `no-construct`: Disallows calling constructor of `String`, `Number`, and `Boolean`
138
- `no-duplicate-variable`: Disallows duplicate variable declarations
139
- `no-invalid-this`: Disallows using `this` in classes incorrectly
140
- `no-misused-new`: Warns on apparent attempts to define constructors for interfaces
141
- `no-shadowed-variable`: Disallows shadowing variable declarations
142
- `no-string-throw`: Flags throwing plain strings or concatenations of strings
143
- `no-unused-expression`: Disallows unused expression statements
144
- `triple-equals`: Requires `===` and `!==` over `==` and `!=`
145
146
**Build-time specific rules:**
147
- `await-promise`: Warns for awaiting a value that is not a Promise
148
- `no-floating-promises`: Requires Promise-returning functions to be handled appropriately
149
- `no-void-expression`: Requires expressions of type void to be used in statement position
150
151
### Dependencies
152
153
**Runtime Dependencies:**
154
```json { .api }
155
{
156
"dependencies": {
157
"tslint-consistent-codestyle": "^1.14.1"
158
}
159
}
160
```
161
162
**Peer Dependencies:**
163
```json { .api }
164
{
165
"peerDependencies": {
166
"tslint": ">=5.11.0"
167
}
168
}
169
```
170
171
**Environment Requirements:**
172
```json { .api }
173
{
174
"engines": {
175
"node": ">=8.9"
176
}
177
}
178
```
179
180
## Usage Examples
181
182
### Basic Project Setup
183
184
Create a `tslint.json` file in your project root:
185
186
```json
187
{
188
"$schema": "http://json.schemastore.org/tslint",
189
"extends": ["@loopback/tslint-config/tslint.common.json"],
190
"rules": {
191
// Override specific rules if needed
192
"no-console": true
193
}
194
}
195
```
196
197
### Build Pipeline Integration
198
199
Create a separate `tslint.build.json` for your build process:
200
201
```json
202
{
203
"$schema": "http://json.schemastore.org/tslint",
204
"extends": ["@loopback/tslint-config/tslint.build.json"],
205
"rules": {
206
// Build-specific overrides
207
}
208
}
209
```
210
211
Then run TSLint with type checking:
212
213
```bash
214
tslint --project tsconfig.json --config tslint.build.json 'src/**/*.ts'
215
```
216
217
### IDE Integration
218
219
Most TypeScript-enabled editors will automatically use `tslint.json` for real-time linting. The common configuration is optimized for this use case as it doesn't require type information and provides fast feedback.
220
221
### Custom Rule Extensions
222
223
You can extend the LoopBack configuration with your own rules:
224
225
```json
226
{
227
"$schema": "http://json.schemastore.org/tslint",
228
"extends": ["@loopback/tslint-config/tslint.common.json"],
229
"rules": {
230
// Disable a rule from the base config
231
"no-any": false,
232
// Add additional rules
233
"max-line-length": [true, 120],
234
"prefer-const": true
235
}
236
}
237
```