/
lint.impl.ts
151 lines (125 loc) · 4.54 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
import type { ExecutorContext } from '@nrwl/devkit';
import { ESLint } from 'eslint';
import { writeFileSync, mkdirSync } from 'fs';
import { dirname, join, resolve } from 'path';
import type { Schema } from './schema';
import { lint, loadESLint } 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;
process.chdir(context.cwd);
const projectName = context.projectName || '<???>';
const printInfo = options.format && !options.silent;
if (printInfo) {
console.info(`\nLinting ${JSON.stringify(projectName)}...`);
}
const projectESLint: { ESLint: typeof ESLint } = await loadESLint();
const version = projectESLint.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.');
}
const eslint = new projectESLint.ESLint({});
/**
* 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.
*/
const eslintConfigPath = options.eslintConfig
? resolve(systemRoot, options.eslintConfig)
: undefined;
let lintResults: ESLint.LintResult[] = [];
try {
lintResults = await lint(eslintConfigPath, options);
} catch (err) {
if (
err.message.includes(
'You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser'
)
) {
let eslintConfigPathForError = `for ${projectName}`;
if (context.workspace?.projects?.[projectName]?.root) {
const { root } = context.workspace.projects[projectName];
eslintConfigPathForError = `\`${root}/.eslintrc.json\``;
}
console.error(`
Error: You have attempted to use 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 project ESLint config ${
eslintConfigPath || eslintConfigPathForError
}
Please see https://nx.dev/guides/eslint for full guidance on how to resolve this issue.
`);
return {
success: false,
};
}
// If some unexpected error, rethrow
throw err;
}
if (lintResults.length === 0) {
const ignoredPatterns = (
await Promise.all(
options.lintFilePatterns.map(async (pattern) =>
(await eslint.isPathIgnored(pattern)) ? pattern : null
)
)
)
.filter((pattern) => !!pattern)
.map((pattern) => `- '${pattern}'`);
if (ignoredPatterns.length) {
throw new Error(
`All files matching the following patterns are ignored:\n${ignoredPatterns.join(
'\n'
)}\n\nPlease check your '.eslintignore' file.`
);
}
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 projectESLint.ESLint.outputFixes(lintResults);
// if quiet, only show errors
if (options.quiet) {
console.debug('Quiet mode enabled - filtering out warnings\n');
lintResults = ESLint.getErrorResults(lintResults);
}
const formatter = await eslint.loadFormatter(options.format);
let totalErrors = 0;
let totalWarnings = 0;
for (const result of lintResults) {
if (result.errorCount || result.warningCount) {
totalErrors += result.errorCount;
totalWarnings += result.warningCount;
}
}
const formattedResults = await formatter.format(lintResults);
if (options.outputFile) {
const pathToOutputFile = join(context.root, options.outputFile);
mkdirSync(dirname(pathToOutputFile), { recursive: true });
writeFileSync(pathToOutputFile, formattedResults);
} else {
console.info(formattedResults);
}
if (totalWarnings > 0 && printInfo) {
console.warn('Lint warnings found in the listed files.\n');
}
if (totalErrors > 0 && printInfo) {
console.error('Lint errors found in the listed files.\n');
}
if (totalWarnings === 0 && totalErrors === 0 && printInfo) {
console.info('All files pass linting.\n');
}
return {
success:
options.force ||
(totalErrors === 0 &&
(options.maxWarnings === -1 || totalWarnings <= options.maxWarnings)),
};
}