-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
lint.impl.ts
267 lines (233 loc) · 8.05 KB
/
lint.impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { joinPathFragments, type ExecutorContext } from '@nx/devkit';
import type { ESLint } from 'eslint';
import { mkdirSync, writeFileSync } from 'fs';
import { interpolate } from 'nx/src/tasks-runner/utils';
import { dirname, posix, resolve } from 'path';
import { findFlatConfigFile, findOldConfigFile } from '../../utils/config-file';
import type { Schema } from './schema';
import { resolveAndInstantiateESLint } from './utility/eslint-utils';
export default async function run(
options: Schema,
context: ExecutorContext
): Promise<{ success: boolean }> {
// this is only used for the hasher
delete options.hasTypeAwareRules;
const systemRoot = context.root;
// eslint resolves files relative to the current working directory.
// We want these paths to always be resolved relative to the workspace
// root to be able to run the lint executor from any subfolder.
process.chdir(systemRoot);
const projectName = context.projectName || '<???>';
const projectRoot =
context.projectsConfigurations.projects[context.projectName].root;
const printInfo = options.format && !options.silent;
if (printInfo) {
console.info(`\nLinting ${JSON.stringify(projectName)}...`);
}
options.cacheLocation = options.cacheLocation
? joinPathFragments(options.cacheLocation, projectName)
: undefined;
const { printConfig, errorOnUnmatchedPattern, ...normalizedOptions } =
options;
// locate the flat config file if it exists starting from the project root
const flatConfigFilePath = findFlatConfigFile(projectRoot, context.root);
const hasFlatConfig = flatConfigFilePath !== null;
// while standard eslint uses by default closest config to the file, if otherwise not specified,
// the flat config would be resolved starting from the cwd, which we changed to the workspace root
// so we explicitly set the config path to the flat config file path we previously found
if (hasFlatConfig && !normalizedOptions.eslintConfig) {
normalizedOptions.eslintConfig = posix.relative(
systemRoot,
flatConfigFilePath
);
}
/**
* We want users to have the option of not specifying the config path, and let
* eslint automatically resolve the `.eslintrc.json` files in each folder.
*/
let eslintConfigPath = normalizedOptions.eslintConfig
? resolve(systemRoot, normalizedOptions.eslintConfig)
: undefined;
const { eslint, ESLint } = await resolveAndInstantiateESLint(
eslintConfigPath,
normalizedOptions,
hasFlatConfig
);
const version = ESLint.version?.split('.');
if (
!version ||
version.length < 2 ||
Number(version[0]) < 7 ||
(Number(version[0]) === 7 && Number(version[1]) < 6)
) {
throw new Error('ESLint must be version 7.6 or higher.');
}
if (printConfig) {
try {
const fileConfig = await eslint.calculateConfigForFile(printConfig);
console.log(JSON.stringify(fileConfig, null, ' '));
return {
success: true,
};
} catch (err) {
console.error(err);
return {
success: false,
};
}
}
let lintResults: ESLint.LintResult[] = [];
const normalizedLintFilePatterns = normalizedOptions.lintFilePatterns.map(
(pattern) => {
return interpolate(pattern, {
workspaceRoot: '',
projectRoot,
projectName: context.projectName,
});
}
);
try {
lintResults = await eslint.lintFiles(normalizedLintFilePatterns);
} catch (err) {
if (
err.message.includes(
'You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser'
)
) {
const ruleName = err.message.match(/rule '([^']+)':/)?.[1];
const reportedFile = err.message.match(
/Occurred while linting (.+)$/
)?.[1];
let eslintConfigPathForError = `for the project "${projectName}"`;
if (eslintConfigPath) {
eslintConfigPathForError = `"${posix.relative(
context.root,
eslintConfigPath
)}"`;
} else {
const configPathForfile = hasFlatConfig
? findFlatConfigFile(projectRoot, context.root)
: findOldConfigFile(reportedFile ?? projectRoot, context.root);
if (configPathForfile) {
eslintConfigPathForError = `"${posix.relative(
context.root,
configPathForfile
)}"`;
}
}
console.error(`
Error: You have attempted to use ${
ruleName ? `the lint rule "${ruleName}"` : 'a lint rule'
} which requires the full TypeScript type-checker to be available, but you do not have "parserOptions.project" configured to point at your project tsconfig.json files in the relevant TypeScript file "overrides" block of your ESLint config ${eslintConfigPathForError}
${reportedFile ? `Occurred while linting ${reportedFile}` : ''}
Please see https://nx.dev/recipes/tips-n-tricks/eslint for full guidance on how to resolve this issue.
`);
return {
success: false,
};
}
// If some unexpected error, rethrow
throw err;
}
if (lintResults.length === 0 && errorOnUnmatchedPattern) {
const ignoredPatterns = (
await Promise.all(
normalizedLintFilePatterns.map(async (pattern) =>
(await eslint.isPathIgnored(pattern)) ? pattern : null
)
)
)
.filter((pattern) => !!pattern)
.map((pattern) => `- '${pattern}'`);
if (ignoredPatterns.length) {
const ignoreSection = hasFlatConfig
? `'ignores' configuration`
: `'.eslintignore' file`;
throw new Error(
`All files matching the following patterns are ignored:\n${ignoredPatterns.join(
'\n'
)}\n\nPlease check your ${ignoreSection}.`
);
}
throw new Error(
'Invalid lint configuration. Nothing to lint. Please check your lint target pattern(s).'
);
}
// output fixes to disk, if applicable based on the options
await ESLint.outputFixes(lintResults);
// if quiet, only show errors
if (normalizedOptions.quiet) {
console.debug('Quiet mode enabled - filtering out warnings\n');
lintResults = ESLint.getErrorResults(lintResults);
}
const formatter = await eslint.loadFormatter(normalizedOptions.format);
const formattedResults = await formatter.format(lintResults);
if (normalizedOptions.outputFile) {
const pathToOutputFile = joinPathFragments(
context.root,
normalizedOptions.outputFile
);
mkdirSync(dirname(pathToOutputFile), { recursive: true });
writeFileSync(pathToOutputFile, formattedResults);
} else {
console.info(formattedResults);
}
const totals = getTotals(lintResults);
if (printInfo) {
outputPrintInfo(totals);
}
return {
success:
normalizedOptions.force ||
(totals.errors === 0 &&
(normalizedOptions.maxWarnings === -1 ||
totals.warnings <= normalizedOptions.maxWarnings)),
};
}
function getTotals(lintResults: ESLint.LintResult[]) {
let errors = 0;
let warnings = 0;
let fixableErrors = 0;
let fixableWarnings = 0;
for (const result of lintResults) {
errors += result.errorCount || 0;
warnings += result.warningCount || 0;
fixableErrors += result.fixableErrorCount || 0;
fixableWarnings += result.fixableWarningCount || 0;
}
return {
errors,
warnings,
fixableErrors,
fixableWarnings,
};
}
function pluralizedOutput(word: string, count: number) {
return `${count} ${word}${count === 1 ? '' : 's'}`;
}
function outputPrintInfo({
errors,
warnings,
fixableErrors,
fixableWarnings,
}: ReturnType<typeof getTotals>) {
const total = warnings + errors;
const totalFixable = fixableErrors + fixableWarnings;
if (total <= 0) {
console.info('\u2714 All files pass linting\n');
return;
}
console.info(
`\u2716 ${pluralizedOutput('problem', total)} (${pluralizedOutput(
'error',
errors
)}, ${pluralizedOutput('warning', warnings)})\n`
);
if (totalFixable <= 0) return;
console.info(
` ${pluralizedOutput('error', fixableErrors)} and ${pluralizedOutput(
'warning',
fixableWarnings
)} are potentially fixable with the \`--fix\` option.\n`
);
}