-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
options-manager.js
380 lines (327 loc) · 11.6 KB
/
options-manager.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import path from 'path';
import test from 'ava';
import proxyquire from 'proxyquire';
import parentConfig from './fixtures/nested/package';
import childConfig from './fixtures/nested/child/package';
import prettierConfig from './fixtures/prettier/package';
import enginesConfig from './fixtures/engines/package';
process.chdir(__dirname);
const manager = proxyquire('../lib/options-manager', {
'resolve-from': (cwd, path) => `cwd/${path}`
});
test('normalizeOpts: makes all the opts plural and arrays', t => {
const opts = manager.normalizeOpts({
env: 'node',
global: 'foo',
ignore: 'test.js',
plugin: 'my-plugin',
rule: {'my-rule': 'foo'},
setting: {'my-rule': 'bar'},
extend: 'foo',
extension: 'html'
});
t.deepEqual(opts, {
envs: ['node'],
globals: ['foo'],
ignores: ['test.js'],
plugins: ['my-plugin'],
rules: {'my-rule': 'foo'},
settings: {'my-rule': 'bar'},
extends: ['foo'],
extensions: ['html']
});
});
test('normalizeOpts: falsie values stay falsie', t => {
t.deepEqual(manager.normalizeOpts({}), {});
});
test('buildConfig: defaults', t => {
const config = manager.buildConfig({});
t.true(/[\\/]\.xo-cache[\\/]?$/.test(config.cacheLocation));
t.is(config.useEslintrc, false);
t.is(config.cache, true);
t.is(config.baseConfig.extends[0], 'xo/esnext');
});
test('buildConfig: esnext', t => {
const config = manager.buildConfig({esnext: false});
t.is(config.baseConfig.extends[0], 'xo');
});
test('buildConfig: space: true', t => {
const config = manager.buildConfig({space: true});
t.deepEqual(config.rules.indent, ['error', 2, {SwitchCase: 1}]);
});
test('buildConfig: space: 4', t => {
const config = manager.buildConfig({space: 4});
t.deepEqual(config.rules.indent, ['error', 4, {SwitchCase: 1}]);
});
test('buildConfig: semicolon', t => {
const config = manager.buildConfig({semicolon: false});
t.deepEqual(config.rules, {
semi: ['error', 'never'],
'semi-spacing': ['error', {
before: false,
after: true
}]
});
});
test('buildConfig: prettier: true', t => {
const config = manager.buildConfig({prettier: true, extends: ['xo-react']});
t.deepEqual(config.plugins, ['prettier']);
// Sets the `semi`, `useTabs` and `tabWidth` options in `prettier/prettier` based on the XO `space` and `semicolon` options
// Sets `singleQuote`, `trailingComma`, `bracketSpacing` and `jsxBracketSameLine` with XO defaults
t.deepEqual(config.rules, {
'prettier/prettier': ['error', {
useTabs: true,
bracketSpacing: false,
jsxBracketSameLine: false,
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'es5'
}]});
// eslint-prettier-config must always be last
t.deepEqual(config.baseConfig.extends.slice(-1), ['prettier']);
});
test('buildConfig: prettier: true, semicolon: false', t => {
const config = manager.buildConfig({prettier: true, semicolon: false});
// Sets the `semi` options in `prettier/prettier` based on the XO `semicolon` option
t.deepEqual(config.rules['prettier/prettier'], ['error', {
useTabs: true,
bracketSpacing: false,
jsxBracketSameLine: false,
semi: false,
singleQuote: true,
tabWidth: 2,
trailingComma: 'es5'
}]);
});
test('buildConfig: prettier: true, space: 4', t => {
const config = manager.buildConfig({prettier: true, space: 4});
// Sets `useTabs` and `tabWidth` options in `prettier/prettier` rule based on the XO `space` options
t.deepEqual(config.rules['prettier/prettier'], ['error', {
useTabs: false,
bracketSpacing: false,
jsxBracketSameLine: false,
semi: true,
singleQuote: true,
tabWidth: 4,
trailingComma: 'es5'
}]);
});
test('buildConfig: prettier: true, esnext: false', t => {
const config = manager.buildConfig({prettier: true, esnext: false});
// Sets `useTabs` and `tabWidth` options in `prettier/prettier` rule based on the XO `space` options
t.deepEqual(config.rules['prettier/prettier'], ['error', {
useTabs: true,
bracketSpacing: false,
jsxBracketSameLine: false,
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'none'
}]);
});
test('buildConfig: engines: undefined', t => {
const config = manager.buildConfig({});
// Do not include any Node.js version specific rules
t.is(config.rules['prefer-spread'], undefined);
t.is(config.rules['prefer-rest-params'], undefined);
t.is(config.rules['prefer-destructuring'], undefined);
t.is(config.rules['promise/prefer-await-to-then'], undefined);
});
test('buildConfig: engines: false', t => {
const config = manager.buildConfig({engines: false});
// Do not include any Node.js version specific rules
t.is(config.rules['prefer-spread'], undefined);
t.is(config.rules['prefer-rest-params'], undefined);
t.is(config.rules['prefer-destructuring'], undefined);
t.is(config.rules['promise/prefer-await-to-then'], undefined);
});
test('buildConfig: engines: invalid range', t => {
const config = manager.buildConfig({engines: {node: '4'}});
// Do not include any Node.js version specific rules
t.is(config.rules['prefer-spread'], undefined);
t.is(config.rules['prefer-rest-params'], undefined);
t.is(config.rules['prefer-destructuring'], undefined);
t.is(config.rules['promise/prefer-await-to-then'], undefined);
});
test('buildConfig: engines: >=4', t => {
const config = manager.buildConfig({engines: {node: '>=4'}});
// Do not include rules for Node.js 5 and above
t.is(config.rules['unicorn/prefer-spread'], undefined);
// Do not include rules for Node.js 6 and above
t.is(config.rules['prefer-rest-params'], undefined);
t.is(config.rules['prefer-destructuring'], undefined);
// Do not include rules for Node.js 8 and above
t.is(config.rules['promise/prefer-await-to-then'], undefined);
});
test('buildConfig: engines: >=4.1', t => {
const config = manager.buildConfig({engines: {node: '>=5.1'}});
// Do not include rules for Node.js 5 and above
t.is(config.rules['unicorn/prefer-spread'], 'error');
// Do not include rules for Node.js 6 and above
t.is(config.rules['prefer-rest-params'], undefined);
t.is(config.rules['prefer-destructuring'], undefined);
// Do not include rules for Node.js 8 and above
t.is(config.rules['promise/prefer-await-to-then'], undefined);
});
test('buildConfig: engines: >=6', t => {
const config = manager.buildConfig({engines: {node: '>=6'}});
// Include rules for Node.js 5 and above
t.is(config.rules['unicorn/prefer-spread'], 'error');
// Include rules for Node.js 6 and above
t.is(config.rules['prefer-rest-params'], 'error');
t.deepEqual(config.rules['prefer-destructuring'], ['error', {array: true, object: true}]);
// Do not include rules for Node.js 8 and above
t.is(config.rules['promise/prefer-await-to-then'], undefined);
});
test('buildConfig: engines: >=8', t => {
const config = manager.buildConfig({engines: {node: '>=8'}});
// Include rules for Node.js 5 and above
t.is(config.rules['unicorn/prefer-spread'], 'error');
// Include rules for Node.js 6 and above
t.is(config.rules['prefer-rest-params'], 'error');
t.deepEqual(config.rules['prefer-destructuring'], ['error', {array: true, object: true}]);
// Include rules for Node.js 8 and above
t.is(config.rules['promise/prefer-await-to-then'], 'error');
});
test('mergeWithPrettierConf: use `singleQuote`, `trailingComma`, `bracketSpacing` and `jsxBracketSameLine` from `prettier` config if defined', t => {
const cwd = path.resolve('fixtures', 'prettier');
const result = manager.mergeWithPrettierConf({cwd});
const expected = Object.assign({}, prettierConfig.prettier, {tabWidth: 2, useTabs: true, semi: true});
t.deepEqual(result, expected);
});
test('mergeWithPrettierConf: determine `tabWidth`, `useTabs`, `semi` from xo config', t => {
const cwd = path.resolve('fixtures', 'prettier');
const result = manager.mergeWithPrettierConf({cwd, space: 4, semicolon: false});
const expected = Object.assign({}, prettierConfig.prettier, {tabWidth: 4, useTabs: false, semi: false});
t.deepEqual(result, expected);
});
test('buildConfig: rules', t => {
const rules = {'object-curly-spacing': ['error', 'always']};
const config = manager.buildConfig({rules});
t.deepEqual(config.rules, rules);
});
test('buildConfig: parser', t => {
const parser = 'babel-eslint';
const config = manager.buildConfig({parser});
t.deepEqual(config.baseConfig.parser, parser);
});
test('buildConfig: settings', t => {
const settings = {'import/resolver': 'webpack'};
const config = manager.buildConfig({settings});
t.deepEqual(config.baseConfig.settings, settings);
});
test('buildConfig: extends', t => {
const config = manager.buildConfig({extends: [
'plugin:foo/bar',
'eslint-config-foo-bar',
'foo-bar-two'
]});
t.deepEqual(config.baseConfig.extends.slice(-3), [
'plugin:foo/bar',
'cwd/eslint-config-foo-bar',
'cwd/eslint-config-foo-bar-two'
]);
});
test('findApplicableOverrides', t => {
const result = manager.findApplicableOverrides('/user/dir/foo.js', [
{files: '**/f*.js'},
{files: '**/bar.js'},
{files: '**/*oo.js'},
{files: '**/*.txt'}
]);
t.is(result.hash, 0b1010);
t.deepEqual(result.applicable, [
{files: '**/f*.js'},
{files: '**/*oo.js'}
]);
});
test('groupConfigs', t => {
const paths = [
'/user/foo/hello.js',
'/user/foo/goodbye.js',
'/user/foo/howdy.js',
'/user/bar/hello.js'
];
const opts = {
esnext: false
};
const overrides = [
{
files: '**/foo/*',
esnext: true
},
{
files: '**/foo/howdy.js',
space: 3,
env: 'mocha'
}
];
const result = manager.groupConfigs(paths, opts, overrides);
t.deepEqual(result, [
{
opts: {
esnext: true
},
paths: ['/user/foo/hello.js', '/user/foo/goodbye.js']
},
{
opts: {
esnext: true,
space: 3,
envs: ['mocha']
},
paths: ['/user/foo/howdy.js']
},
{
opts: {
esnext: false
},
paths: ['/user/bar/hello.js']
}
].map(obj => {
obj.opts = Object.assign(manager.emptyOptions(), obj.opts);
return obj;
}));
});
test('mergeWithPkgConf: use child if closest', t => {
const cwd = path.resolve('fixtures', 'nested', 'child');
const result = manager.mergeWithPkgConf({cwd});
const expected = Object.assign({}, childConfig.xo, {cwd}, {engines: {}});
t.deepEqual(result, expected);
});
test('mergeWithPkgConf: use parent if closest', t => {
const cwd = path.resolve('fixtures', 'nested');
const result = manager.mergeWithPkgConf({cwd});
const expected = Object.assign({}, parentConfig.xo, {cwd}, {engines: {}});
t.deepEqual(result, expected);
});
test('mergeWithPkgConf: use parent if child is ignored', t => {
const cwd = path.resolve('fixtures', 'nested', 'child-ignore');
const result = manager.mergeWithPkgConf({cwd});
const expected = Object.assign({}, parentConfig.xo, {cwd}, {engines: {}});
t.deepEqual(result, expected);
});
test('mergeWithPkgConf: use child if child is empty', t => {
const cwd = path.resolve('fixtures', 'nested', 'child-empty');
const result = manager.mergeWithPkgConf({cwd});
t.deepEqual(result, {cwd, engines: {}});
});
test('mergeWithPkgConf: read engines from package.json', t => {
const cwd = path.resolve('fixtures', 'engines');
const result = manager.mergeWithPkgConf({cwd});
const expected = Object.assign({}, {engines: enginesConfig.engines}, {cwd});
t.deepEqual(result, expected);
});
test('mergeWithPkgConf: XO engine options supersede package.json\'s', t => {
const cwd = path.resolve('fixtures', 'engines');
const result = manager.mergeWithPkgConf({cwd, engines: {node: '>=8'}});
const expected = Object.assign({}, {engines: {node: '>=8'}}, {cwd});
t.deepEqual(result, expected);
});
test('mergeWithPkgConf: XO engine options false supersede package.json\'s', t => {
const cwd = path.resolve('fixtures', 'engines');
const result = manager.mergeWithPkgConf({cwd, engines: false});
const expected = Object.assign({}, {engines: false}, {cwd});
t.deepEqual(result, expected);
});