Skip to content

Commit a78e02e

Browse files
committed
Set target engines to 8 for default AVA files
1 parent 51a96d4 commit a78e02e

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const runEslint = (paths, opts) => {
4242
module.exports.lintText = (str, opts) => {
4343
opts = optionsManager.preprocess(opts);
4444

45-
if (opts.overrides && opts.overrides.length > 0) {
45+
if (opts.overrides && opts.overrides.length > 0 && opts.filename) {
4646
const overrides = opts.overrides;
4747
delete opts.overrides;
4848

lib/options-manager.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const DEFAULT_EXTENSION = [
3030
'jsx'
3131
];
3232

33+
const AVA_NODE_TARGET = 8;
34+
35+
const AVA_DEFAULT_FILES = [
36+
'test.js',
37+
'test-*.js',
38+
'test',
39+
'**/__tests__',
40+
'**/*.test.js'
41+
];
42+
3343
const DEFAULT_CONFIG = {
3444
useEslintrc: false,
3545
cache: true,
@@ -325,17 +335,29 @@ const getIgnores = opts => {
325335
return opts;
326336
};
327337

338+
const getDefaultOverrides = opts => {
339+
// Only apply if the user uses AVA, and the `engines.node` supoort Node version lower than AVA target
340+
if (opts.cwd && resolveFrom.silent(opts.cwd, 'ava') && (!opts.engines || !opts.engines.node || !semver.validRange(opts.engines.node) || semver.intersects(opts.engines.node, `<${AVA_NODE_TARGET}`))) {
341+
opts.overrides = arrify(opts.overrides);
342+
opts.overrides.push(mergeWith({}, {files: AVA_DEFAULT_FILES}, {engines: {nodes: `>=${AVA_NODE_TARGET}`}}));
343+
}
344+
345+
return opts;
346+
};
347+
328348
const preprocess = opts => {
329349
opts = mergeWithPkgConf(opts);
330350
opts = normalizeOpts(opts);
331351
opts = getIgnores(opts);
352+
opts = getDefaultOverrides(opts);
332353
opts.extensions = DEFAULT_EXTENSION.concat(opts.extensions || []);
333354

334355
return opts;
335356
};
336357

337358
module.exports.DEFAULT_IGNORE = DEFAULT_IGNORE;
338359
module.exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
360+
module.exports.AVA_DEFAULT_FILES = AVA_DEFAULT_FILES;
339361
module.exports.mergeWithPkgConf = mergeWithPkgConf;
340362
module.exports.mergeWithPrettierConf = mergeWithPrettierConf;
341363
module.exports.normalizeOpts = normalizeOpts;
@@ -346,3 +368,4 @@ module.exports.groupConfigs = groupConfigs;
346368
module.exports.preprocess = preprocess;
347369
module.exports.emptyOptions = emptyOptions;
348370
module.exports.getIgnores = getIgnores;
371+
module.exports.getDefaultOverrides = getDefaultOverrides;

readme.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,25 +313,6 @@ If you have a directory structure with nested `package.json` files and you want
313313

314314
Put a `package.json` with your config at the root and add `"xo": false` to the `package.json` in your bundled packages.
315315

316-
### Transpilation
317-
318-
If some files in your project are transpiled in order to support an older Node version you can use the [Config Overrides](#config-overrides) option to set a specific [nodeVersion](#nodeversion) target to these files.
319-
320-
For example, if your project targets Node 4 (your `package.json` is configured with `engines.node` to `>=4`) and you are using [AVA](https://github.com/avajs/ava), then your test files are automatically transpiled. You can override `nodeVersion` for the tests files:
321-
322-
```json
323-
{
324-
"xo": {
325-
"overrides": [
326-
{
327-
"files": "{test,tests,spec,__tests__}/**/*.js",
328-
"nodeVersion": ">=9"
329-
}
330-
]
331-
}
332-
}
333-
```
334-
335316

336317
## FAQ
337318

test/options-manager.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,24 @@ test('mergeWithPkgConf: XO engine options false supersede package.json\'s', t =>
382382
const expected = Object.assign({}, {engines: false}, {cwd});
383383
t.deepEqual(result, expected);
384384
});
385+
386+
test('getDefaultOverrides: add AVA override if node engine <=8', t => {
387+
const cwd = path.resolve('fixtures', 'engines');
388+
const config = manager.getDefaultOverrides({cwd, engines: {node: '>=4'}});
389+
390+
t.deepEqual(config.overrides, [{engines: {nodes: '>=8'}, files: manager.AVA_DEFAULT_FILES}]);
391+
});
392+
393+
test('getDefaultOverrides: add AVA override is node engine undefined', t => {
394+
const cwd = path.resolve('fixtures', 'engines');
395+
const config = manager.getDefaultOverrides({cwd});
396+
397+
t.deepEqual(config.overrides, [{engines: {nodes: '>=8'}, files: manager.AVA_DEFAULT_FILES}]);
398+
});
399+
400+
test('getDefaultOverrides: Ado not add AVA override if node engine >=8', t => {
401+
const cwd = path.resolve('fixtures', 'engines');
402+
const config = manager.getDefaultOverrides({cwd, engines: {node: '>=8'}});
403+
404+
t.is(config.overrides, undefined);
405+
});

0 commit comments

Comments
 (0)