From a78e02e8253fcfd6c443eb130b10b91f3bcf7966 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Wed, 10 Jan 2018 15:11:26 -0500 Subject: [PATCH] Set target engines to 8 for default AVA files --- index.js | 2 +- lib/options-manager.js | 23 +++++++++++++++++++++++ readme.md | 19 ------------------- test/options-manager.js | 21 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index b042ed94..a0526303 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/lib/options-manager.js b/lib/options-manager.js index 681d6a97..4fd75fcd 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -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, @@ -325,10 +335,21 @@ 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; @@ -336,6 +357,7 @@ const preprocess = 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; @@ -346,3 +368,4 @@ module.exports.groupConfigs = groupConfigs; module.exports.preprocess = preprocess; module.exports.emptyOptions = emptyOptions; module.exports.getIgnores = getIgnores; +module.exports.getDefaultOverrides = getDefaultOverrides; diff --git a/readme.md b/readme.md index 9d84c802..0d73de7e 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/options-manager.js b/test/options-manager.js index bcd9ff8a..916941c7 100644 --- a/test/options-manager.js +++ b/test/options-manager.js @@ -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); +});