Skip to content

Commit 1fde568

Browse files
committed
Add node/no-unsupported-features rules
1 parent e0f81a7 commit 1fde568

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

config/plugins.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@ module.exports = {
185185
// }
186186
// ],
187187

188-
// Disabled as the rule doesn't allow to exclude compiled sources
189-
// 'node/no-unsupported-features': 'error',
190-
191188
'node/process-exit-as-throw': 'error',
192189

193190
// Disabled as the rule doesn't exclude scripts executed with `node` but not referenced in 'bin'. See https://github.com/mysticatea/eslint-plugin-node/issues/96

lib/options-manager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ const buildXOConfig = options => config => {
195195
}
196196
}
197197

198+
if (options.nodeVersion) {
199+
config.rules['node/no-unsupported-features/es-builtins'] = ['error', {version: options.nodeVersion}];
200+
config.rules['node/no-unsupported-features/es-syntax'] = ['error', {version: options.nodeVersion, ignores: ['modules']}];
201+
config.rules['node/no-unsupported-features/node-builtins'] = ['error', {version: options.nodeVersion}];
202+
}
203+
198204
if (options.space && !options.prettier) {
199205
config.rules.indent = ['error', spaces, {SwitchCase: 1}];
200206

readme.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,37 @@ Put a `package.json` with your config at the root and omit the `xo` property in
336336

337337
### Transpilation
338338

339-
If some files in your project are transpiled in order to support an older Node.js version, you can use the [config overrides](#config-overrides) option to set a specific [`nodeVersion`](#nodeversion) target for these files.
339+
If some files in your project are transpiled in order to support an older Node.js version, you can use the [config overrides](#config-overrides) option to set a specific [`nodeVersion`](#nodeversion) to target your sources files.
340340

341-
For example, if your project targets Node.js 4 (your `package.json` is configured with `engines.node` set 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:
341+
For example, if your project targets Node.js 8 but you want to use the latest JavaScript syntax as supported in Node.js 12:
342+
1. Set the `engines.node` property of your `package.json` to `>=8`
343+
2. Configure [Babel](https://babeljs.io) to transpile your source files (in `src` directory in this example)
344+
3. Make sure to include the transpiled files in your published package with the [`files`](https://docs.npmjs.com/files/package.json#files) and [`main`](https://docs.npmjs.com/files/package.json#main) properties of your `package.json`
345+
4. Configure the XO `overrides` option to set `nodeVersion` to `>=12` for your source files directory
342346

343347
```json
344348
{
349+
"engines": {
350+
"node": ">=8"
351+
},
352+
"scripts": {
353+
"build": "babel src --out-dir dist"
354+
},
355+
"main": "dist/index.js",
356+
"files": ["dist/**/*.js"],
345357
"xo": {
346358
"overrides": [
347359
{
348-
"files": "{test,tests,spec,__tests__}/**/*.js",
349-
"nodeVersion": ">=9"
360+
"files": "{src}/**/*.js",
361+
"nodeVersion": ">=12"
350362
}
351363
]
352364
}
353365
}
354366
```
355367

368+
This way your `package.json` will contain the actual minimum Node.js version supported by your published code, but XO will lint your source code as if it targets Node.js 12.
369+
356370
### Including files ignored by default
357371

358372
To include files that XO [ignores by default](https://github.com/xojs/xo/blob/master/lib/constants.js#L1), add them as negative globs in the `ignores` option:
@@ -367,7 +381,6 @@ To include files that XO [ignores by default](https://github.com/xojs/xo/blob/ma
367381
}
368382
```
369383

370-
371384
## FAQ
372385

373386
#### What does XO mean?

test/options-manager.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ test('buildConfig: space: 4', t => {
6767

6868
test('buildConfig: semicolon', t => {
6969
const config = manager.buildConfig({semicolon: false, nodeVersion: '12'});
70-
t.deepEqual(config.rules, {
71-
semi: ['error', 'never'],
72-
'semi-spacing': ['error', {
73-
before: false,
74-
after: true
75-
}]
76-
});
70+
t.deepEqual(config.rules.semi, ['error', 'never']);
71+
t.deepEqual(config.rules['semi-spacing'], ['error', {before: false, after: true}]);
7772
});
7873

7974
test('buildConfig: prettier: true', t => {
@@ -211,6 +206,9 @@ test('buildConfig: engines: undefined', t => {
211206
t.is(config.rules['unicorn/prefer-flat-map'], 'off');
212207
t.is(config.rules['node/prefer-promises/dns'], 'off');
213208
t.is(config.rules['node/prefer-promises/fs'], 'off');
209+
t.is(config.rules['node/no-unsupported-features/es-builtins'], undefined);
210+
t.is(config.rules['node/no-unsupported-features/es-syntax'], undefined);
211+
t.is(config.rules['node/no-unsupported-features/node-builtins'], undefined);
214212
});
215213

216214
test('buildConfig: nodeVersion: false', t => {
@@ -226,31 +224,32 @@ test('buildConfig: nodeVersion: false', t => {
226224
t.is(config.rules['node/prefer-promises/fs'], 'off');
227225
});
228226

229-
test('buildConfig: nodeVersion: invalid range', t => {
230-
const config = manager.buildConfig({nodeVersion: '4'});
231-
232-
// Override all the rules specific to Node.js version
233-
t.is(config.rules['prefer-object-spread'], 'off');
234-
t.is(config.rules['prefer-rest-params'], 'off');
235-
t.is(config.rules['prefer-destructuring'], 'off');
236-
t.is(config.rules['promise/prefer-await-to-then'], 'off');
237-
t.is(config.rules['unicorn/prefer-flat-map'], 'off');
238-
t.is(config.rules['node/prefer-promises/dns'], 'off');
239-
t.is(config.rules['node/prefer-promises/fs'], 'off');
240-
});
241-
242227
test('buildConfig: nodeVersion: >=6', t => {
243228
const config = manager.buildConfig({nodeVersion: '>=6'});
244229

245230
// Turn off rule if we support Node.js below 7.6.0
246231
t.is(config.rules['promise/prefer-await-to-then'], 'off');
232+
// Set node/no-unsupported-features rules with the nodeVersion
233+
t.deepEqual(config.rules['node/no-unsupported-features/es-builtins'], ['error', {version: '>=6'}]);
234+
t.deepEqual(
235+
config.rules['node/no-unsupported-features/es-syntax'],
236+
['error', {version: '>=6', ignores: ['modules']}]
237+
);
238+
t.deepEqual(config.rules['node/no-unsupported-features/node-builtins'], ['error', {version: '>=6'}]);
247239
});
248240

249241
test('buildConfig: nodeVersion: >=8', t => {
250242
const config = manager.buildConfig({nodeVersion: '>=8'});
251243

252244
// Do not turn off rule if we support only Node.js above 7.6.0
253245
t.is(config.rules['promise/prefer-await-to-then'], undefined);
246+
// Set node/no-unsupported-features rules with the nodeVersion
247+
t.deepEqual(config.rules['node/no-unsupported-features/es-builtins'], ['error', {version: '>=8'}]);
248+
t.deepEqual(
249+
config.rules['node/no-unsupported-features/es-syntax'],
250+
['error', {version: '>=8', ignores: ['modules']}]
251+
);
252+
t.deepEqual(config.rules['node/no-unsupported-features/node-builtins'], ['error', {version: '>=8'}]);
254253
});
255254

256255
test('mergeWithPrettierConfig: use `singleQuote`, `trailingComma`, `bracketSpacing` and `jsxBracketSameLine` from `prettier` config if defined', t => {
@@ -344,7 +343,7 @@ test('mergeWithPrettierConfig: throw error is `space`/`tabWidth` conflicts', t =
344343
test('buildConfig: rules', t => {
345344
const rules = {'object-curly-spacing': ['error', 'always']};
346345
const config = manager.buildConfig({rules, nodeVersion: '12'});
347-
t.deepEqual(config.rules, rules);
346+
t.deepEqual(config.rules['object-curly-spacing'], rules['object-curly-spacing']);
348347
});
349348

350349
test('buildConfig: parser', t => {

0 commit comments

Comments
 (0)