-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
lint-files.js
320 lines (269 loc) · 9.45 KB
/
lint-files.js
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import process from 'node:process';
import path from 'node:path';
import test from 'ava';
import createEsmUtils from 'esm-utils';
import xo from '../index.js';
const {__dirname} = createEsmUtils(import.meta);
process.chdir(__dirname);
const hasRule = (results, filePath, ruleId, rulesMeta) => {
const result = results.find(x => x.filePath === filePath);
const hasRuleInResults = result ? result.messages.some(x => x.ruleId === ruleId) : false;
const hasRuleInResultsMeta = rulesMeta ? typeof rulesMeta[ruleId] === 'object' : true;
return hasRuleInResults && hasRuleInResultsMeta;
};
test('only accepts allowed extensions', async t => {
// Markdown files will always produce linter errors and will not be going away
const cwd = path.join(__dirname, 'fixtures/custom-extension');
const mdGlob = '*.md';
// No files should be linted = no errors
const noOptionsResults = await xo.lintFiles(mdGlob, {cwd});
t.is(noOptionsResults.errorCount, 0);
// Markdown files linted (with no plugin for it) = errors
const moreExtensionsResults = await xo.lintFiles(mdGlob, {extensions: ['md'], cwd});
t.true(moreExtensionsResults.errorCount > 0);
});
test('ignores dirs for empty extensions', async t => {
{
const cwd = path.join(__dirname, 'fixtures/nodir');
const glob = '*';
const results = await xo.lintFiles(glob, {extensions: ['', 'js'], cwd});
const {results: [fileResult]} = results;
// Only `fixtures/nodir/noextension` should be linted
const expected = 'fixtures/nodir/noextension'.split('/').join(path.sep);
const actual = path.relative(__dirname, fileResult.filePath);
t.is(actual, expected);
t.is(results.errorCount, 1);
}
{
const cwd = path.join(__dirname, 'fixtures/nodir');
const glob = 'nested/*';
const results = await xo.lintFiles(glob, {cwd});
const {results: [fileResult]} = results;
// Ensure `nodir/nested` **would** report if globbed
const expected = 'fixtures/nodir/nested/index.js'.split('/').join(path.sep);
const actual = path.relative(__dirname, fileResult.filePath);
t.is(actual, expected);
t.is(results.errorCount, 1);
}
{
const cwd = path.join(__dirname, 'fixtures/nodir');
// Check Windows-style paths are working
const glob = 'nested\\*';
const results = await xo.lintFiles(glob, {cwd});
const {results: [fileResult]} = results;
const expected = 'fixtures/nodir/nested/index.js'.split('/').join(path.sep);
const actual = path.relative(__dirname, fileResult.filePath);
t.is(actual, expected);
t.is(results.errorCount, 1);
}
});
test.serial('cwd option', async t => {
const {results} = await xo.lintFiles('**/*', {cwd: 'fixtures/cwd'});
const paths = results.map(r => path.relative(__dirname, r.filePath));
paths.sort();
t.deepEqual(paths, [path.join('fixtures', 'cwd', 'unicorn.js')]);
});
test('do not lint gitignored files', async t => {
const cwd = path.join(__dirname, 'fixtures/gitignore');
const glob = '**/*';
const ignored = path.resolve('fixtures/gitignore/test/foo.js');
const {results} = await xo.lintFiles(glob, {cwd});
t.is(results.some(r => r.filePath === ignored), false);
});
test('do not lint gitignored files in file with negative gitignores', async t => {
const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
const glob = '*';
const ignored = path.resolve('fixtures/negative-gitignore/bar.js');
const {results} = await xo.lintFiles(glob, {cwd});
t.is(results.some(r => r.filePath === ignored), false);
});
test('lint negatively gitignored files', async t => {
const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
const glob = '*';
const negative = path.resolve('fixtures/negative-gitignore/foo.js');
const {results} = await xo.lintFiles(glob, {cwd});
t.is(results.some(r => r.filePath === negative), true);
});
test('do not lint inapplicable negatively gitignored files', async t => {
const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
const glob = 'bar.js';
const negative = path.resolve('fixtures/negative-gitignore/foo.js');
const {results} = await xo.lintFiles(glob, {cwd});
t.is(results.some(r => r.filePath === negative), false);
});
test('multiple negative patterns should act as positive patterns', async t => {
const cwd = path.join(__dirname, 'fixtures', 'gitignore-multiple-negation');
const {results} = await xo.lintFiles('**/*', {cwd});
const paths = results.map(r => path.basename(r.filePath));
paths.sort();
t.deepEqual(paths, ['!!unicorn.js', '!unicorn.js']);
});
test.failing('enable rules based on nodeVersion', async t => {
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: 'fixtures/engines-overrides'});
// The transpiled file (as specified in `overrides`) should use `await`
t.true(
hasRule(
results,
path.resolve('fixtures/engines-overrides/promise-then-transpile.js'),
'promise/prefer-await-to-then',
rulesMeta,
),
);
// The non transpiled files can use `.then`
t.false(
hasRule(
results,
path.resolve('fixtures/engines-overrides/promise-then.js'),
'promise/prefer-await-to-then',
rulesMeta,
),
);
});
test('do not lint eslintignored files', async t => {
const cwd = path.join(__dirname, 'fixtures/eslintignore');
const glob = '*';
const positive = path.resolve('fixtures/eslintignore/foo.js');
const negative = path.resolve('fixtures/eslintignore/bar.js');
const {results} = await xo.lintFiles(glob, {cwd});
t.is(results.some(r => r.filePath === positive), true);
t.is(results.some(r => r.filePath === negative), false);
});
test('find configurations close to linted file', async t => {
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: 'fixtures/nested-configs'});
t.true(
hasRule(
results,
path.resolve('fixtures/nested-configs/child/semicolon.js'),
'semi',
rulesMeta,
),
);
t.true(
hasRule(
results,
path.resolve('fixtures/nested-configs/child-override/child-prettier-override/semicolon.js'),
'prettier/prettier',
rulesMeta,
),
);
t.true(
hasRule(
results,
path.resolve('fixtures/nested-configs/no-semicolon.js'),
'semi',
rulesMeta,
),
);
t.true(
hasRule(
results,
path.resolve('fixtures/nested-configs/child-override/two-spaces.js'),
'indent',
rulesMeta,
),
);
});
test.serial('typescript files', async t => {
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: 'fixtures/typescript'});
t.true(
hasRule(
results,
path.resolve('fixtures/typescript/two-spaces.tsx'),
'@typescript-eslint/indent',
rulesMeta,
),
);
t.true(
hasRule(
results,
path.resolve('fixtures/typescript/child/extra-semicolon.ts'),
'@typescript-eslint/no-extra-semi',
rulesMeta,
),
);
t.true(
hasRule(
results,
path.resolve('fixtures/typescript/child/sub-child/four-spaces.ts'),
'@typescript-eslint/indent',
rulesMeta,
),
);
});
test.serial('typescript 2 space option', async t => {
const {errorCount, results} = await xo.lintFiles('two-spaces.tsx', {cwd: 'fixtures/typescript', space: 2});
// eslint-disable-next-line ava/assertion-arguments
t.is(errorCount, 0, JSON.stringify(results[0].messages));
});
test.serial('typescript 4 space option', async t => {
const {errorCount} = await xo.lintFiles('child/sub-child/four-spaces.ts', {cwd: 'fixtures/typescript', space: 4});
t.is(errorCount, 0);
});
test.serial('typescript no semicolon option', async t => {
const {errorCount} = await xo.lintFiles('child/no-semicolon.ts', {cwd: 'fixtures/typescript', semicolon: false});
t.is(errorCount, 0);
});
test('webpack import resolver is used if webpack.config.js is found', async t => {
const cwd = 'fixtures/webpack/with-config/';
const {results} = await xo.lintFiles('file1.js', {
cwd,
rules: {
'import/no-unresolved': 2,
},
});
// eslint-disable-next-line ava/assertion-arguments
t.is(results[0].errorCount, 1, JSON.stringify(results[0].messages));
const errorMessage = results[0].messages[0].message;
t.truthy(/Unable to resolve path to module 'inexistent'/.exec(errorMessage));
});
test('webpack import resolver config can be passed through webpack option', async t => {
const cwd = 'fixtures/webpack/no-config/';
const {results} = await xo.lintFiles('file1.js', {
cwd,
webpack: {
config: {
resolve: {
alias: {
file2alias: path.resolve(__dirname, cwd, './file2.js'),
},
},
},
},
rules: {
'import/no-unresolved': 2,
},
});
// eslint-disable-next-line ava/assertion-arguments
t.is(results[0].errorCount, 1, JSON.stringify(results[0].messages));
});
test('webpack import resolver is used if {webpack: true}', async t => {
const cwd = 'fixtures/webpack/no-config/';
const {results} = await xo.lintFiles('file3.js', {
cwd,
webpack: true,
rules: {
'import/no-unresolved': 2,
'import/no-webpack-loader-syntax': 0,
},
});
// eslint-disable-next-line ava/assertion-arguments
t.is(results[0].errorCount, 0, JSON.stringify(results[0]));
});
async function configType(t, {dir}) {
const {results, rulesMeta} = await xo.lintFiles('**/*', {cwd: path.resolve('fixtures', 'config-files', dir)});
t.true(
hasRule(
results,
path.resolve('fixtures', 'config-files', dir, 'file.js'),
'indent',
rulesMeta,
),
);
}
configType.title = (_, {type}) => `load config from ${type}`.trim();
test(configType, {type: 'xo.config.js', dir: 'xo-config_js'});
test(configType, {type: 'xo.config.cjs', dir: 'xo-config_cjs'});
test(configType, {type: '.xo-config.js', dir: 'xo-config_js'});
test(configType, {type: '.xo-config.cjs', dir: 'xo-config_cjs'});
test(configType, {type: '.xo-config.json', dir: 'xo-config_json'});
test(configType, {type: '.xo-config', dir: 'xo-config'});