ESLint plugin that wraps a TSLint configuration and lints the whole source using TSLint
npx @tessl/cli install tessl/npm-typescript-eslint--eslint-plugin-tslint@7.0.00
# TypeScript ESLint Plugin TSLint
1
2
TypeScript ESLint Plugin TSLint is an ESLint plugin that provides a bridge between ESLint and TSLint by wrapping TSLint configurations and enabling the use of TSLint rules within an ESLint workflow. It allows developers to gradually migrate from TSLint to ESLint while maintaining existing TSLint rules and configurations.
3
4
## Package Information
5
6
- **Package Name**: @typescript-eslint/eslint-plugin-tslint
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @typescript-eslint/eslint-plugin-tslint`
10
11
## Core Imports
12
13
```typescript
14
// When using the plugin programmatically
15
import { meta, rules } from "@typescript-eslint/eslint-plugin-tslint";
16
// or
17
import * as eslintPluginTslint from "@typescript-eslint/eslint-plugin-tslint";
18
```
19
20
For CommonJS:
21
22
```javascript
23
const { meta, rules } = require("@typescript-eslint/eslint-plugin-tslint");
24
// or
25
const eslintPluginTslint = require("@typescript-eslint/eslint-plugin-tslint");
26
```
27
28
## Basic Usage
29
30
```typescript
31
// ESLint configuration (.eslintrc.js)
32
module.exports = {
33
parser: "@typescript-eslint/parser",
34
parserOptions: {
35
project: "./tsconfig.json"
36
},
37
plugins: ["@typescript-eslint/tslint"],
38
rules: {
39
"@typescript-eslint/tslint/config": [
40
"error",
41
{
42
rules: {
43
semicolon: [true, "always"],
44
"no-string-throw": true
45
}
46
}
47
]
48
}
49
};
50
```
51
52
## Architecture
53
54
The plugin is built around these key components:
55
56
- **Plugin Metadata**: Contains name and version information exported as `meta`
57
- **Single Rule System**: Exposes only one rule called "config" that wraps TSLint functionality
58
- **Custom Linter**: Extends TSLint's Linter class to integrate with TypeScript compiler program
59
- **Configuration Bridge**: Converts ESLint configuration to TSLint configuration format
60
- **Result Transformation**: Converts TSLint failures to ESLint report format with auto-fixing support
61
62
## Capabilities
63
64
### Plugin Metadata
65
66
The plugin exports metadata information containing the plugin name and version.
67
68
```typescript { .api }
69
import type { Linter } from '@typescript-eslint/utils/ts-eslint';
70
71
export const meta: Linter.PluginMeta;
72
73
// Linter.PluginMeta is defined in @typescript-eslint/utils/ts-eslint
74
// and contains:
75
// {
76
// name: string;
77
// version: string;
78
// }
79
```
80
81
### Plugin Rules
82
83
The plugin exports a rules object containing the single "config" rule.
84
85
```typescript { .api }
86
import type { Linter } from '@typescript-eslint/utils/ts-eslint';
87
88
export const rules: Linter.PluginRules;
89
90
// The rules object contains a single rule:
91
// {
92
// config: ESLintRule
93
// }
94
```
95
96
### TSLint Configuration Rule
97
98
The main rule that wraps TSLint configuration and applies TSLint rules through ESLint.
99
100
```typescript { .api }
101
// Rule name: "config"
102
// Usage: "@typescript-eslint/tslint/config"
103
104
interface RuleOptions {
105
/** TSLint rules configuration */
106
rules?: RawRulesConfig;
107
/** Custom TSLint rules directories */
108
rulesDirectory?: string[];
109
/** Path to TSLint configuration file */
110
lintFile?: string;
111
}
112
113
type RawRulesConfig = Record<
114
string,
115
| unknown[]
116
| boolean
117
| {
118
severity?: RuleSeverity | 'default' | 'none' | 'warn';
119
options?: unknown;
120
}
121
| null
122
| undefined
123
>;
124
125
type RuleSeverity = 'error' | 'warning' | 'info' | 'hint';
126
```
127
128
**Usage Examples:**
129
130
```typescript
131
// Inline rules configuration
132
{
133
"@typescript-eslint/tslint/config": [
134
"error",
135
{
136
rules: {
137
semicolon: [true, "always"],
138
"no-string-throw": true,
139
"restrict-plus-operands": true
140
}
141
}
142
]
143
}
144
145
// External TSLint file configuration
146
{
147
"@typescript-eslint/tslint/config": [
148
"error",
149
{
150
lintFile: "./tslint.json"
151
}
152
]
153
}
154
155
// Custom rules directory
156
{
157
"@typescript-eslint/tslint/config": [
158
"error",
159
{
160
rulesDirectory: ["./custom-tslint-rules"],
161
rules: {
162
"custom-rule": {
163
severity: "error",
164
options: { customOption: true }
165
}
166
}
167
}
168
]
169
}
170
```
171
172
### Custom Linter Class
173
174
Extended TSLint Linter class that provides access to TypeScript compiler program.
175
176
```typescript { .api }
177
import type { ILinterOptions, LintResult } from 'tslint';
178
import { Linter } from 'tslint';
179
import type { Program, SourceFile } from 'typescript';
180
181
class CustomLinter extends Linter {
182
constructor(
183
options: ILinterOptions,
184
private readonly program: Program
185
);
186
187
/** Get the linting results from TSLint */
188
getResult(): LintResult;
189
190
/** Get a TypeScript source file from the program */
191
getSourceFile(fileName: string): SourceFile | undefined;
192
}
193
194
// ILinterOptions and LintResult are imported from 'tslint'
195
// Program and SourceFile are imported from 'typescript'
196
```
197
198
## Types
199
200
```typescript { .api }
201
type MessageIds = 'failure';
202
203
type Options = [
204
{
205
rules?: RawRulesConfig;
206
rulesDirectory?: string[];
207
lintFile?: string;
208
}
209
];
210
```
211
212
## Error Handling
213
214
The plugin handles errors by:
215
216
- Converting TSLint rule failures to ESLint diagnostic messages
217
- Preserving original TSLint error messages and rule names
218
- Maintaining source location information for precise error reporting
219
- Supporting auto-fix suggestions from TSLint rules when available
220
221
Common error scenarios:
222
- Missing `parserOptions.project` configuration (required for TypeScript parsing)
223
- TSLint configuration file not found when using `lintFile` option
224
- Custom rules directory path not found when using `rulesDirectory`
225
- TSLint rule configuration errors (invalid rule names or options)
226
227
## Dependencies
228
229
### Peer Dependencies
230
- **eslint**: `^8.56.0` - ESLint linting framework
231
- **tslint**: `^5.0.0 || ^6.0.0` - TSLint linting library
232
- **typescript**: `*` - TypeScript compiler
233
234
### Internal Dependencies
235
- **@typescript-eslint/utils**: `7.0.2` - TypeScript ESLint utilities
236
237
## Installation and Setup
238
239
```bash
240
npm install @typescript-eslint/eslint-plugin-tslint tslint typescript
241
```
242
243
Required ESLint configuration:
244
245
```javascript
246
module.exports = {
247
parser: "@typescript-eslint/parser",
248
parserOptions: {
249
project: "./tsconfig.json", // Required for TypeScript parsing
250
ecmaVersion: 2018,
251
sourceType: "module"
252
},
253
plugins: ["@typescript-eslint/tslint"],
254
rules: {
255
"@typescript-eslint/tslint/config": [
256
"error",
257
{
258
// TSLint configuration options
259
}
260
]
261
}
262
};
263
```