Skip to content

Commit

Permalink
Set target engines to 8 for default AVA files
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jan 14, 2018
1 parent 51a96d4 commit a78e02e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const runEslint = (paths, opts) => {
module.exports.lintText = (str, opts) => {
opts = optionsManager.preprocess(opts);

if (opts.overrides && opts.overrides.length > 0) {
if (opts.overrides && opts.overrides.length > 0 && opts.filename) {
const overrides = opts.overrides;
delete opts.overrides;

Expand Down
23 changes: 23 additions & 0 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ const DEFAULT_EXTENSION = [
'jsx'
];

const AVA_NODE_TARGET = 8;

const AVA_DEFAULT_FILES = [
'test.js',
'test-*.js',
'test',
'**/__tests__',
'**/*.test.js'
];

const DEFAULT_CONFIG = {
useEslintrc: false,
cache: true,
Expand Down Expand Up @@ -325,17 +335,29 @@ const getIgnores = opts => {
return opts;
};

const getDefaultOverrides = opts => {
// Only apply if the user uses AVA, and the `engines.node` supoort Node version lower than AVA target
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}`))) {
opts.overrides = arrify(opts.overrides);
opts.overrides.push(mergeWith({}, {files: AVA_DEFAULT_FILES}, {engines: {nodes: `>=${AVA_NODE_TARGET}`}}));
}

return opts;
};

const preprocess = opts => {
opts = mergeWithPkgConf(opts);
opts = normalizeOpts(opts);
opts = getIgnores(opts);
opts = getDefaultOverrides(opts);
opts.extensions = DEFAULT_EXTENSION.concat(opts.extensions || []);

return opts;
};

module.exports.DEFAULT_IGNORE = DEFAULT_IGNORE;
module.exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
module.exports.AVA_DEFAULT_FILES = AVA_DEFAULT_FILES;
module.exports.mergeWithPkgConf = mergeWithPkgConf;
module.exports.mergeWithPrettierConf = mergeWithPrettierConf;
module.exports.normalizeOpts = normalizeOpts;
Expand All @@ -346,3 +368,4 @@ module.exports.groupConfigs = groupConfigs;
module.exports.preprocess = preprocess;
module.exports.emptyOptions = emptyOptions;
module.exports.getIgnores = getIgnores;
module.exports.getDefaultOverrides = getDefaultOverrides;
19 changes: 0 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,25 +313,6 @@ If you have a directory structure with nested `package.json` files and you want

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

### Transpilation

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.

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:

```json
{
"xo": {
"overrides": [
{
"files": "{test,tests,spec,__tests__}/**/*.js",
"nodeVersion": ">=9"
}
]
}
}
```


## FAQ

Expand Down
21 changes: 21 additions & 0 deletions test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,24 @@ test('mergeWithPkgConf: XO engine options false supersede package.json\'s', t =>
const expected = Object.assign({}, {engines: false}, {cwd});
t.deepEqual(result, expected);
});

test('getDefaultOverrides: add AVA override if node engine <=8', t => {
const cwd = path.resolve('fixtures', 'engines');
const config = manager.getDefaultOverrides({cwd, engines: {node: '>=4'}});

t.deepEqual(config.overrides, [{engines: {nodes: '>=8'}, files: manager.AVA_DEFAULT_FILES}]);
});

test('getDefaultOverrides: add AVA override is node engine undefined', t => {
const cwd = path.resolve('fixtures', 'engines');
const config = manager.getDefaultOverrides({cwd});

t.deepEqual(config.overrides, [{engines: {nodes: '>=8'}, files: manager.AVA_DEFAULT_FILES}]);
});

test('getDefaultOverrides: Ado not add AVA override if node engine >=8', t => {
const cwd = path.resolve('fixtures', 'engines');
const config = manager.getDefaultOverrides({cwd, engines: {node: '>=8'}});

t.is(config.overrides, undefined);
});

0 comments on commit a78e02e

Please sign in to comment.