0
# Linter Integration
1
2
Support for ESLint integration and linter configuration management. These utilities provide standardized linter type definitions for Nx workspace tooling.
3
4
## Capabilities
5
6
### Linter Type Enumeration
7
8
Standardized linter types for workspace configuration (deprecated enum, use type instead).
9
10
```typescript { .api }
11
/**
12
* @deprecated Use LinterType instead
13
* Enumeration of supported linter types
14
*/
15
enum Linter {
16
/** ESLint linter configuration */
17
EsLint = 'eslint',
18
/** No linter configuration */
19
None = 'none'
20
}
21
22
/**
23
* Modern type definition for linter options
24
*/
25
type LinterType = 'eslint' | 'none';
26
```
27
28
**Usage Examples:**
29
30
```typescript
31
import { Linter, LinterType } from "@nx/workspace";
32
33
// Using deprecated enum (still functional)
34
function setupLinterWithEnum(linter: Linter) {
35
switch (linter) {
36
case Linter.EsLint:
37
console.log("Setting up ESLint configuration");
38
break;
39
case Linter.None:
40
console.log("No linter configuration");
41
break;
42
}
43
}
44
45
// Using modern type (recommended)
46
function setupLinterWithType(linter: LinterType) {
47
switch (linter) {
48
case 'eslint':
49
console.log("Setting up ESLint configuration");
50
break;
51
case 'none':
52
console.log("No linter configuration");
53
break;
54
}
55
}
56
57
// Generator usage
58
setupLinterWithEnum(Linter.EsLint);
59
setupLinterWithType('eslint');
60
```
61
62
## Generator Integration
63
64
### Schema Definitions
65
66
Linter types are commonly used in generator schemas for project creation:
67
68
```typescript
69
import { LinterType } from "@nx/workspace";
70
71
interface ProjectGeneratorSchema {
72
name: string;
73
directory?: string;
74
linter: LinterType;
75
skipFormat?: boolean;
76
}
77
78
function generateProject(schema: ProjectGeneratorSchema) {
79
console.log(`Creating project with ${schema.linter} linter`);
80
81
if (schema.linter === 'eslint') {
82
// Setup ESLint configuration
83
setupEslintConfig(schema.name);
84
}
85
}
86
```
87
88
### ESLint Configuration Setup
89
90
```typescript
91
import { Tree } from "@nx/devkit";
92
import { LinterType } from "@nx/workspace";
93
94
function addLinterConfiguration(
95
tree: Tree,
96
projectRoot: string,
97
linter: LinterType
98
): void {
99
if (linter === 'eslint') {
100
// Create .eslintrc.json
101
const eslintConfig = {
102
extends: ["../../.eslintrc.json"],
103
ignorePatterns: ["!**/*"],
104
overrides: [
105
{
106
files: ["*.ts", "*.tsx", "*.js", "*.jsx"],
107
rules: {}
108
}
109
]
110
};
111
112
tree.write(
113
`${projectRoot}/.eslintrc.json`,
114
JSON.stringify(eslintConfig, null, 2)
115
);
116
117
console.log("ESLint configuration created");
118
} else {
119
console.log("No linter configuration requested");
120
}
121
}
122
```
123
124
## Workspace Linter Setup
125
126
### Root ESLint Configuration
127
128
When using ESLint in an Nx workspace, the root configuration typically includes:
129
130
```json
131
{
132
"root": true,
133
"ignorePatterns": ["**/*"],
134
"plugins": ["@nx"],
135
"overrides": [
136
{
137
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
138
"extends": ["@nx/eslint-plugin-nx/recommended"],
139
"rules": {}
140
}
141
]
142
}
143
```
144
145
### Project-Specific Configuration
146
147
Individual projects extend the root configuration:
148
149
```json
150
{
151
"extends": ["../../.eslintrc.json"],
152
"ignorePatterns": ["!**/*"],
153
"overrides": [
154
{
155
"files": ["*.ts", "*.tsx"],
156
"extends": [
157
"@nx/eslint-plugin-nx/typescript"
158
],
159
"rules": {}
160
}
161
]
162
}
163
```
164
165
## Advanced Usage
166
167
### Conditional Linter Setup
168
169
```typescript
170
import { Tree } from "@nx/devkit";
171
import { LinterType } from "@nx/workspace";
172
173
interface LinterOptions {
174
linter: LinterType;
175
strict?: boolean;
176
includePrettier?: boolean;
177
}
178
179
function setupAdvancedLinter(
180
tree: Tree,
181
projectRoot: string,
182
options: LinterOptions
183
): void {
184
if (options.linter === 'none') {
185
console.log("Skipping linter setup");
186
return;
187
}
188
189
const eslintConfig: any = {
190
extends: ["../../.eslintrc.json"],
191
ignorePatterns: ["!**/*"],
192
overrides: [
193
{
194
files: ["*.ts", "*.tsx", "*.js", "*.jsx"],
195
rules: {}
196
}
197
]
198
};
199
200
// Add strict rules if requested
201
if (options.strict) {
202
eslintConfig.overrides[0].extends = [
203
"@nx/eslint-plugin-nx/typescript"
204
];
205
eslintConfig.overrides[0].rules = {
206
"@typescript-eslint/no-unused-vars": "error",
207
"@typescript-eslint/no-explicit-any": "error"
208
};
209
}
210
211
// Add Prettier integration
212
if (options.includePrettier) {
213
eslintConfig.extends.push("prettier");
214
}
215
216
tree.write(
217
`${projectRoot}/.eslintrc.json`,
218
JSON.stringify(eslintConfig, null, 2)
219
);
220
}
221
```
222
223
### Linter Type Validation
224
225
```typescript
226
import { LinterType } from "@nx/workspace";
227
228
function isValidLinterType(value: string): value is LinterType {
229
return value === 'eslint' || value === 'none';
230
}
231
232
function validateLinterOptions(options: any): LinterType {
233
if (!options.linter) {
234
return 'eslint'; // Default to ESLint
235
}
236
237
if (!isValidLinterType(options.linter)) {
238
throw new Error(`Invalid linter type: ${options.linter}. Must be 'eslint' or 'none'.`);
239
}
240
241
return options.linter;
242
}
243
244
// Usage in generators
245
function createProjectWithValidation(options: any) {
246
const linter = validateLinterOptions(options);
247
console.log(`Using linter: ${linter}`);
248
}
249
```
250
251
### Migration from Enum to Type
252
253
```typescript
254
import { Linter, LinterType } from "@nx/workspace";
255
256
// Migration helper function
257
function migrateLinterEnum(oldLinter: Linter): LinterType {
258
switch (oldLinter) {
259
case Linter.EsLint:
260
return 'eslint';
261
case Linter.None:
262
return 'none';
263
default:
264
return 'eslint';
265
}
266
}
267
268
// Usage during migration
269
function migrateOldConfiguration(oldConfig: { linter: Linter }) {
270
const newConfig = {
271
...oldConfig,
272
linter: migrateLinterEnum(oldConfig.linter)
273
};
274
275
return newConfig;
276
}
277
```
278
279
## Integration with Nx Targets
280
281
### ESLint Target Configuration
282
283
When ESLint is selected, projects typically get a lint target:
284
285
```json
286
{
287
"targets": {
288
"lint": {
289
"executor": "@nx/linter:eslint",
290
"outputs": ["{options.outputFile}"],
291
"options": {
292
"lintFilePatterns": ["apps/my-app/**/*.{ts,tsx,js,jsx}"]
293
}
294
}
295
}
296
}
297
```
298
299
### Target Setup Helper
300
301
```typescript
302
import { Tree, updateProjectConfiguration, readProjectConfiguration } from "@nx/devkit";
303
import { LinterType } from "@nx/workspace";
304
305
function addLintTarget(
306
tree: Tree,
307
projectName: string,
308
linter: LinterType,
309
lintFilePatterns: string[]
310
): void {
311
if (linter === 'none') {
312
return; // Skip target creation
313
}
314
315
const projectConfig = readProjectConfiguration(tree, projectName);
316
317
projectConfig.targets = projectConfig.targets || {};
318
projectConfig.targets.lint = {
319
executor: "@nx/linter:eslint",
320
outputs: ["{options.outputFile}"],
321
options: {
322
lintFilePatterns
323
}
324
};
325
326
updateProjectConfiguration(tree, projectName, projectConfig);
327
}
328
```
329
330
## Types
331
332
```typescript { .api }
333
/**
334
* @deprecated Use LinterType instead
335
*/
336
enum Linter {
337
EsLint = 'eslint',
338
None = 'none'
339
}
340
341
/**
342
* Modern linter type definition
343
*/
344
type LinterType = 'eslint' | 'none';
345
346
interface LinterOptions {
347
linter: LinterType;
348
strict?: boolean;
349
includePrettier?: boolean;
350
}
351
```