/
lint-text.js
364 lines (311 loc) · 13.2 KB
/
lint-text.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import process from 'node:process';
import {promises as fs} from 'node:fs';
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, expectedRuleId) => results[0].messages.some(({ruleId}) => ruleId === expectedRuleId);
test('.lintText()', async t => {
const {results} = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n');
t.true(hasRule(results, 'semi'));
});
test('default `ignores`', async t => {
const result = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {
filePath: 'node_modules/ignored/index.js',
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});
test('`ignores` option', async t => {
const result = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {
filePath: 'ignored/index.js',
ignores: ['ignored/**/*.js'],
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});
test('`ignores` option without cwd', async t => {
const result = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {
filePath: 'ignored/index.js',
ignores: ['ignored/**/*.js'],
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});
test('respect overrides', async t => {
const result = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {
filePath: 'ignored/index.js',
ignores: ['ignored/**/*.js'],
overrides: [
{
files: ['ignored/**/*.js'],
ignores: [],
},
],
rules: {
'unicorn/prefer-module': 'off',
'unicorn/prefer-node-protocol': 'off',
},
});
t.is(result.errorCount, 1);
t.is(result.warningCount, 0);
});
test('overriden ignore', async t => {
const result = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {
filePath: 'unignored.js',
overrides: [
{
files: ['unignored.js'],
ignores: ['unignored.js'],
},
],
});
t.is(result.errorCount, 0);
t.is(result.warningCount, 0);
});
test('`ignores` option without filename', async t => {
await t.throwsAsync(async () => {
await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n', {
ignores: ['ignored/**/*.js'],
});
}, {message: /The `ignores` option requires the `filePath` option to be defined./u});
});
test('JSX support', async t => {
const {results} = await xo.lintText('const app = <div className="appClass">Hello, React!</div>;\n');
t.true(hasRule(results, 'no-unused-vars'));
});
test('plugin support', async t => {
const {results} = await xo.lintText('var React;\nReact.render(<App/>);\n', {
plugins: ['react'],
rules: {'react/jsx-no-undef': 'error'},
});
t.true(hasRule(results, 'react/jsx-no-undef'));
});
test('prevent use of extended native objects', async t => {
const {results} = await xo.lintText('[].unicorn();\n');
t.true(hasRule(results, 'no-use-extend-native/no-use-extend-native'));
});
test('extends support', async t => {
const {results} = await xo.lintText('var React;\nReact.render(<App/>);\n', {
extends: 'xo-react',
});
t.true(hasRule(results, 'react/jsx-no-undef'));
});
test('disable style rules when `prettier` option is enabled', async t => {
const {results: withoutPrettier} = await xo.lintText('(a) => {}\n', {filePath: 'test.js'});
// `arrow-parens` is enabled
t.true(hasRule(withoutPrettier, 'arrow-parens'));
// `prettier/prettier` is disabled
t.false(hasRule(withoutPrettier, 'prettier/prettier'));
const {results: withPrettier} = await xo.lintText('(a) => {}\n', {prettier: true, filePath: 'test.js'});
// `arrow-parens` is disabled by `eslint-config-prettier`
t.false(hasRule(withPrettier, 'arrow-parens'));
// `prettier/prettier` is enabled
t.true(hasRule(withPrettier, 'prettier/prettier'));
});
test('extends `react` support with `prettier` option', async t => {
const {results} = await xo.lintText('<Hello name={ firstname } />;\n', {extends: 'xo-react', prettier: true, filePath: 'test.jsx'});
// `react/jsx-curly-spacing` is disabled by `eslint-config-prettier`
t.false(hasRule(results, 'react/jsx-curly-spacing'));
// `prettier/prettier` is enabled
t.true(hasRule(results, 'prettier/prettier'));
});
test('regression test for #71', async t => {
const {results} = await xo.lintText('const foo = { key: \'value\' };\nconsole.log(foo);\n', {
extends: path.join(__dirname, 'fixtures/extends.js'),
});
t.is(results[0].errorCount, 0);
});
test('lintText() - overrides support', async t => {
const cwd = path.join(__dirname, 'fixtures/overrides');
const bar = path.join(cwd, 'test/bar.js');
const {results: barResults} = await xo.lintText(await fs.readFile(bar, 'utf8'), {filePath: bar, cwd});
t.is(barResults[0].errorCount, 0);
const foo = path.join(cwd, 'test/foo.js');
const {results: fooResults} = await xo.lintText(await fs.readFile(foo, 'utf8'), {filePath: foo, cwd});
t.is(fooResults[0].errorCount, 0);
const index = path.join(cwd, 'test/index.js');
const {results: indexResults} = await xo.lintText(await fs.readFile(bar, 'utf8'), {filePath: index, cwd});
t.is(indexResults[0].errorCount, 0);
});
test('do not lint gitignored files if filename is given', async t => {
const cwd = path.join(__dirname, 'fixtures/gitignore');
const ignoredPath = path.resolve('fixtures/gitignore/test/foo.js');
const ignored = await fs.readFile(ignoredPath, 'utf8');
const {results} = await xo.lintText(ignored, {filePath: ignoredPath, cwd});
t.is(results[0].errorCount, 0);
});
test('lint gitignored files if filename is not given', async t => {
const ignoredPath = path.resolve('fixtures/gitignore/test/foo.js');
const ignored = await fs.readFile(ignoredPath, 'utf8');
const {results} = await xo.lintText(ignored);
t.true(results[0].errorCount > 0);
});
test('do not lint gitignored files in file with negative gitignores', async t => {
const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
const ignoredPath = path.resolve('fixtures/negative-gitignore/bar.js');
const ignored = await fs.readFile(ignoredPath, 'utf8');
const {results} = await xo.lintText(ignored, {filePath: ignoredPath, cwd});
t.is(results[0].errorCount, 0);
});
test('multiple negative patterns should act as positive patterns', async t => {
const cwd = path.join(__dirname, 'fixtures', 'gitignore-multiple-negation');
const filePath = path.join(cwd, '!!!unicorn.js');
const text = await fs.readFile(filePath, 'utf8');
const {results} = await xo.lintText(text, {filePath, cwd});
t.is(results[0].errorCount, 0);
});
test('lint negatively gitignored files', async t => {
const cwd = path.join(__dirname, 'fixtures/negative-gitignore');
const glob = path.posix.join(cwd, '*');
const {results} = await xo.lintFiles(glob, {cwd});
t.true(results[0].errorCount > 0);
});
test('do not lint eslintignored files if filename is given', async t => {
const cwd = path.join(__dirname, 'fixtures/eslintignore');
const ignoredPath = path.resolve('fixtures/eslintignore/bar.js');
const ignored = await fs.readFile(ignoredPath, 'utf8');
const {results} = await xo.lintText(ignored, {filePath: ignoredPath, cwd});
t.is(results[0].errorCount, 0);
});
test('lint eslintignored files if filename is not given', async t => {
const ignoredPath = path.resolve('fixtures/eslintignore/bar.js');
const ignored = await fs.readFile(ignoredPath, 'utf8');
const {results} = await xo.lintText(ignored);
t.true(results[0].errorCount > 0);
});
test('enable rules based on nodeVersion', async t => {
const cwd = path.join(__dirname, 'fixtures', 'engines-overrides');
const filePath = path.join(cwd, 'promise-then.js');
const text = await fs.readFile(filePath, 'utf8');
let {results} = await xo.lintText(text, {nodeVersion: '>=8.0.0'});
t.true(hasRule(results, 'promise/prefer-await-to-then'));
({results} = await xo.lintText(text, {nodeVersion: '>=6.0.0'}));
t.false(hasRule(results, 'promise/prefer-await-to-then'));
});
test('enable rules based on nodeVersion in override', async t => {
const cwd = path.join(__dirname, 'fixtures', 'engines-overrides');
const filePath = path.join(cwd, 'promise-then.js');
const text = await fs.readFile(filePath, 'utf8');
let {results} = await xo.lintText(text, {
nodeVersion: '>=8.0.0',
filePath: 'promise-then.js',
overrides: [
{
files: 'promise-*.js',
nodeVersion: '>=6.0.0',
},
],
});
t.false(hasRule(results, 'promise/prefer-await-to-then'));
({results} = await xo.lintText(text, {
nodeVersion: '>=6.0.0',
filePath: 'promise-then.js',
overrides: [
{
files: 'promise-*.js',
nodeVersion: '>=8.0.0',
},
],
}));
t.true(hasRule(results, 'promise/prefer-await-to-then'));
});
test('allow unassigned stylesheet imports', async t => {
let {results} = await xo.lintText('import \'stylesheet.css\'');
t.false(hasRule(results, 'import/no-unassigned-import'));
({results} = await xo.lintText('import \'stylesheet.scss\''));
t.false(hasRule(results, 'import/no-unassigned-import'));
({results} = await xo.lintText('import \'stylesheet.sass\''));
t.false(hasRule(results, 'import/no-unassigned-import'));
({results} = await xo.lintText('import \'stylesheet.less\''));
t.false(hasRule(results, 'import/no-unassigned-import'));
});
test('find configurations close to linted file', async t => {
let {results} = await xo.lintText('console.log(\'semicolon\');\n', {filePath: 'fixtures/nested-configs/child/semicolon.js'});
t.true(hasRule(results, 'semi'));
({results} = await xo.lintText('console.log(\'semicolon\');\n', {filePath: 'fixtures/nested-configs/child-override/child-prettier-override/semicolon.js'}));
t.true(hasRule(results, 'prettier/prettier'));
({results} = await xo.lintText('console.log(\'no-semicolon\')\n', {filePath: 'fixtures/nested-configs/no-semicolon.js'}));
t.true(hasRule(results, 'semi'));
({results} = await xo.lintText(`console.log([
2
]);\n`, {filePath: 'fixtures/nested-configs/child-override/two-spaces.js'}));
t.true(hasRule(results, 'indent'));
});
test('typescript files: two spaces fails', async t => {
const twoSpacesCwd = path.resolve('fixtures', 'typescript');
const twoSpacesfilePath = path.resolve(twoSpacesCwd, 'two-spaces.tsx');
const twoSpacesText = (await fs.readFile(twoSpacesfilePath)).toString();
const {results} = await xo.lintText(twoSpacesText, {
filePath: twoSpacesfilePath,
});
t.true(hasRule(results, '@typescript-eslint/indent'));
});
test('typescript files: two spaces pass', async t => {
const twoSpacesCwd = path.resolve('fixtures', 'typescript');
const twoSpacesfilePath = path.resolve(twoSpacesCwd, 'two-spaces.tsx');
const twoSpacesText = (await fs.readFile(twoSpacesfilePath)).toString();
const {results} = await xo.lintText(twoSpacesText, {
filePath: twoSpacesfilePath,
space: 2,
});
t.is(results[0].errorCount, 0);
});
test('typescript files: extra semi fail', async t => {
const extraSemiCwd = path.resolve('fixtures', 'typescript', 'child');
const extraSemiFilePath = path.resolve(extraSemiCwd, 'extra-semicolon.ts');
const extraSemiText = (await fs.readFile(extraSemiFilePath)).toString();
const {results} = await xo.lintText(extraSemiText, {
filePath: extraSemiFilePath,
});
t.true(hasRule(results, '@typescript-eslint/no-extra-semi'));
});
test('typescript files: extra semi pass', async t => {
const noSemiCwd = path.resolve('fixtures', 'typescript', 'child');
const noSemiFilePath = path.resolve(noSemiCwd, 'no-semicolon.ts');
const noSemiText = (await fs.readFile(noSemiFilePath)).toString();
const {results} = await xo.lintText(noSemiText, {
filePath: noSemiFilePath,
semicolon: false,
});
t.is(results[0].errorCount, 0);
});
test('typescript files: four space fail', async t => {
const fourSpacesCwd = path.resolve('fixtures', 'typescript', 'child', 'sub-child');
const fourSpacesFilePath = path.resolve(fourSpacesCwd, 'four-spaces.ts');
const fourSpacesText = (await fs.readFile(fourSpacesFilePath)).toString();
const {results} = await xo.lintText(fourSpacesText, {
filePath: fourSpacesFilePath,
});
t.true(hasRule(results, '@typescript-eslint/indent'));
});
test('typescript files: four space pass', async t => {
const fourSpacesCwd = path.resolve('fixtures', 'typescript', 'child', 'sub-child');
const fourSpacesFilePath = path.resolve(fourSpacesCwd, 'four-spaces.ts');
const fourSpacesText = (await fs.readFile(fourSpacesFilePath)).toString();
const {results} = await xo.lintText(fourSpacesText, {
filePath: fourSpacesFilePath,
space: 4,
});
t.is(results[0].errorCount, 0);
});
test('deprecated rules', async t => {
const {usedDeprecatedRules} = await xo.lintText('\'use strict\'\nconsole.log(\'unicorn\');\n');
for (const {ruleId, replacedBy} of usedDeprecatedRules) {
t.is(typeof ruleId, 'string');
t.true(Array.isArray(replacedBy));
}
});
async function configType(t, {dir}) {
const {results} = await xo.lintText('var obj = { a: 1 };\n', {cwd: path.resolve('fixtures', 'config-files', dir), filePath: 'file.js'});
t.true(hasRule(results, 'no-var'));
}
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'});