GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Enforce rules based on "engines" field in package.json (#281) · xojs/xo@0d18368

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • config
    • plugins.js
  • lib
    • options-manager.js
  • main.js
  • package.json
  • readme.md
  • test
    • fixtures/engines
      • package.json
    • main.js
    • options-manager.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎config/plugins.js‎

Copy file name to clipboardExpand all lines: config/plugins.js
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ module.exports = {
3131
rules: {
3232
'no-use-extend-native/no-use-extend-native': 'error',
3333
'promise/param-names': 'error',
34-
// Enable this sometime in the future when Node.js has async/await support
35-
// 'promise/prefer-await-to-then': 'error',
3634
'promise/no-return-wrap': ['error', {allowReject: true}],
3735
'promise/no-return-in-finally': 'error',
3836
'import/default': 'error',
@@ -81,6 +79,9 @@ module.exports = {
8179
// 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
8280
// 'node/shebang': 'error',
8381
'node/no-deprecated-api': 'error',
84-
'node/exports-style': ['error', 'module.exports']
82+
'node/exports-style': ['error', 'module.exports'],
83+
// Disabled by default (overrides `plugin:unicorn/recommended`), will be enabled if supported by the Node.js version
84+
'unicorn/prefer-spread': 'off',
85+
'unicorn/no-new-buffer': 'off'
8586
}
8687
};
Collapse file

‎lib/options-manager.js‎

Copy file name to clipboardExpand all lines: lib/options-manager.js
+54-1Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const pathExists = require('path-exists');
88
const pkgConf = require('pkg-conf');
99
const resolveFrom = require('resolve-from');
1010
const prettier = require('prettier');
11+
const semver = require('semver');
1112

1213
const DEFAULT_IGNORE = [
1314
'**/node_modules/**',
@@ -42,6 +43,46 @@ const DEFAULT_CONFIG = {
4243
}
4344
};
4445

46+
/**
47+
* Define the rules that are enabled only for specific version of Node, based on `engines.node` in package.json or the `node-version` option.
48+
*
49+
* The keys are rule names and the values are an Object with a valid semver (`4.0.0` is valid `4` is not) as keys and the rule configuration as values.
50+
* Each entry define the rule configuration and the minimum Node version for which to set it.
51+
* The entry with the highest version that is compliant with the `engines.node`/`node-version` range will be used.
52+
*
53+
* @type {Object}
54+
*
55+
* @example
56+
* ```javascript
57+
* {
58+
* 'plugin/rule': {
59+
* '6.0.0': ['error', {prop: 'node-6-conf'}],
60+
* '8.0.0': ['error', {prop: 'node-8-conf'}]
61+
* }
62+
* }
63+
*```
64+
* With `engines.node` set to `>=4` the rule `plugin/rule` will not be used.
65+
* With `engines.node` set to `>=6` the rule `plugin/rule` will be used with the config `{prop: 'node-6-conf'}`.
66+
* With `engines.node` set to `>=8` the rule `plugin/rule` will be used with the config `{prop: 'node-8-conf'}`.
67+
*/
68+
const ENGINE_RULES = {
69+
'promise/prefer-await-to-then': {
70+
'7.6.0': 'error'
71+
},
72+
'prefer-rest-params': {
73+
'6.0.0': 'error'
74+
},
75+
'unicorn/prefer-spread': {
76+
'5.0.0': 'error'
77+
},
78+
'prefer-destructuring': {
79+
'6.0.0': ['error', {array: true, object: true}, {enforceForRenamedProperties: true}]
80+
},
81+
'unicorn/no-new-buffer': {
82+
'5.10.0': 'error'
83+
}
84+
};
85+
4586
// Keep the same behaviour in mergeWith as deepAssign
4687
const mergeFn = (prev, val) => {
4788
if (Array.isArray(prev) && Array.isArray(val)) {
@@ -88,7 +129,8 @@ const mergeWithPkgConf = opts => {
88129
opts = Object.assign({cwd: process.cwd()}, opts);
89130
opts.cwd = path.resolve(opts.cwd);
90131
const conf = pkgConf.sync('xo', {cwd: opts.cwd, skipOnFalse: true});
91-
return Object.assign({}, conf, opts);
132+
const engines = pkgConf.sync('engines', {cwd: opts.cwd});
133+
return Object.assign({}, conf, {engines}, opts);
92134
};
93135

94136
const normalizeSpaces = opts => {
@@ -129,6 +171,17 @@ const buildConfig = opts => {
129171
);
130172
const spaces = normalizeSpaces(opts);
131173

174+
if (opts.engines && opts.engines.node && semver.validRange(opts.engines.node)) {
175+
for (const rule of Object.keys(ENGINE_RULES)) {
176+
// Use the rule value for the highest version that is lower or equal to the oldest version of Node supported
177+
for (const minVersion of Object.keys(ENGINE_RULES[rule]).sort(semver.compare)) {
178+
if (!semver.intersects(opts.engines.node, `<${minVersion}`)) {
179+
config.rules[rule] = ENGINE_RULES[rule][minVersion];
180+
}
181+
}
182+
}
183+
}
184+
132185
if (opts.space) {
133186
config.rules.indent = ['error', spaces, {SwitchCase: 1}];
134187

Collapse file

‎main.js‎

Copy file name to clipboardExpand all lines: main.js
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const updateNotifier = require('update-notifier');
44
const getStdin = require('get-stdin');
55
const meow = require('meow');
66
const formatterPretty = require('eslint-formatter-pretty');
7+
const semver = require('semver');
78
const openReport = require('./lib/open-report');
89
const xo = require('.');
910

@@ -21,6 +22,7 @@ const cli = meow(`
2122
--space Use space indent instead of tabs [Default: 2]
2223
--no-semicolon Prevent use of semicolons
2324
--prettier Conform to Prettier code style
25+
--node-version Range of Node.js version to support
2426
--plugin Include third-party plugins [Can be set multiple times]
2527
--extend Extend defaults with a custom config [Can be set multiple times]
2628
--open Open files with issues in your editor
@@ -76,6 +78,9 @@ const cli = meow(`
7678
prettier: {
7779
type: 'boolean'
7880
},
81+
nodeVersion: {
82+
type: 'string'
83+
},
7984
plugin: {
8085
type: 'string'
8186
},
@@ -136,6 +141,17 @@ if (input[0] === '-') {
136141
input.shift();
137142
}
138143

144+
if (opts.nodeVersion) {
145+
if (opts.nodeVersion === 'false') {
146+
opts.engines = false;
147+
} else if (semver.validRange(opts.nodeVersion)) {
148+
opts.engines = {node: opts.nodeVersion};
149+
} else {
150+
console.error('The `node-engine` option must be a valid semver range (for example `>=4`)');
151+
process.exit(1);
152+
}
153+
}
154+
139155
if (opts.init) {
140156
require('xo-init')();
141157
} else if (opts.stdin) {
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"prettier": "~1.10.2",
9898
"resolve-cwd": "^2.0.0",
9999
"resolve-from": "^4.0.0",
100+
"semver": "^5.4.1",
100101
"slash": "^1.0.0",
101102
"update-notifier": "^2.1.0",
102103
"xo-init": "^0.6.0"
Collapse file

‎readme.md‎

Copy file name to clipboardExpand all lines: readme.md
+29Lines changed: 29 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Uses [ESLint](http://eslint.org) underneath, so issues regarding rules should be
2727
- No need to specify file paths to lint as it lints all JS files except for [commonly ignored paths](#ignores).
2828
- [Config overrides per files/globs.](#config-overrides)
2929
- Includes many useful ESLint plugins, like [`unicorn`](https://github.com/sindresorhus/eslint-plugin-unicorn), [`import`](https://github.com/benmosher/eslint-plugin-import), [`ava`](https://github.com/avajs/eslint-plugin-ava), [`node`](https://github.com/mysticatea/eslint-plugin-node) and more.
30+
- Automatically enables rules based on the [`engines`](https://docs.npmjs.com/files/package.json#engines) field in your `package.json`.
3031
- Caches results between runs for much better performance.
3132
- Super simple to add XO to a project with `$ xo --init`.
3233
- Fix many issues automagically with `$ xo --fix`.
@@ -61,6 +62,7 @@ $ xo --help
6162
--space Use space indent instead of tabs [Default: 2]
6263
--no-semicolon Prevent use of semicolons
6364
--prettier Conform to Prettier code style
65+
--node-version Range of Node.js version to support
6466
--plugin Include third-party plugins [Can be set multiple times]
6567
--extend Extend defaults with a custom config [Can be set multiple times]
6668
--open Open files with issues in your editor
@@ -206,6 +208,14 @@ Default: `false`
206208

207209
Format code with [Prettier](https://github.com/prettier/prettier). The [Prettier options](https://prettier.io/docs/en/options.html) will be read from the [Prettier config](https://prettier.io/docs/en/configuration.html)
208210

211+
### nodeVersion
212+
213+
Type: `string`, `boolean`<br>
214+
Default: Value of the `engines.node` key in the project `package.json`
215+
216+
Enable rules specific to the Node.js versions within the configured range.
217+
If set to `false`, no rules specific to a Node.js version will be enabled.
218+
209219
### plugins
210220

211221
Type: `Array`
@@ -303,6 +313,25 @@ If you have a directory structure with nested `package.json` files and you want
303313

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

316+
### Transpilation
317+
318+
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.
319+
320+
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:
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+
306335

307336
## FAQ
308337

Collapse file

‎test/fixtures/engines/package.json‎

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "application-name",
3+
"version": "0.0.1",
4+
"engines": {
5+
"node": ">=6"
6+
}
7+
}
Collapse file

‎test/main.js‎

Copy file name to clipboardExpand all lines: test/main.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,9 @@ test('init option', async t => {
9494
const packageJson = fs.readFileSync(filepath, 'utf8');
9595
t.deepEqual(JSON.parse(packageJson).scripts, {test: 'xo'});
9696
});
97+
98+
test('invalid node-engine option', async t => {
99+
const filepath = await tempWrite('console.log()\n', 'x.js');
100+
const err = await t.throws(main(['--node-version', 'v', filepath]));
101+
t.is(err.code, 1);
102+
});
Collapse file

‎test/options-manager.js‎

Copy file name to clipboardExpand all lines: test/options-manager.js
+108-4Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import proxyquire from 'proxyquire';
44
import parentConfig from './fixtures/nested/package';
55
import childConfig from './fixtures/nested/child/package';
66
import prettierConfig from './fixtures/prettier/package';
7+
import enginesConfig from './fixtures/engines/package';
78

89
process.chdir(__dirname);
910

@@ -138,6 +139,88 @@ test('buildConfig: prettier: true, esnext: false', t => {
138139
}]);
139140
});
140141

142+
test('buildConfig: engines: undefined', t => {
143+
const config = manager.buildConfig({});
144+
145+
// Do not include any Node.js version specific rules
146+
t.is(config.rules['prefer-spread'], undefined);
147+
t.is(config.rules['prefer-rest-params'], undefined);
148+
t.is(config.rules['prefer-destructuring'], undefined);
149+
t.is(config.rules['promise/prefer-await-to-then'], undefined);
150+
});
151+
152+
test('buildConfig: engines: false', t => {
153+
const config = manager.buildConfig({engines: false});
154+
155+
// Do not include any Node.js version specific rules
156+
t.is(config.rules['prefer-spread'], undefined);
157+
t.is(config.rules['prefer-rest-params'], undefined);
158+
t.is(config.rules['prefer-destructuring'], undefined);
159+
t.is(config.rules['promise/prefer-await-to-then'], undefined);
160+
});
161+
162+
test('buildConfig: engines: invalid range', t => {
163+
const config = manager.buildConfig({engines: {node: '4'}});
164+
165+
// Do not include any Node.js version specific rules
166+
t.is(config.rules['prefer-spread'], undefined);
167+
t.is(config.rules['prefer-rest-params'], undefined);
168+
t.is(config.rules['prefer-destructuring'], undefined);
169+
t.is(config.rules['promise/prefer-await-to-then'], undefined);
170+
});
171+
172+
test('buildConfig: engines: >=4', t => {
173+
const config = manager.buildConfig({engines: {node: '>=4'}});
174+
175+
// Do not include rules for Node.js 5 and above
176+
t.is(config.rules['unicorn/prefer-spread'], undefined);
177+
// Do not include rules for Node.js 6 and above
178+
t.is(config.rules['prefer-rest-params'], undefined);
179+
t.is(config.rules['prefer-destructuring'], undefined);
180+
// Do not include rules for Node.js 8 and above
181+
t.is(config.rules['promise/prefer-await-to-then'], undefined);
182+
});
183+
184+
test('buildConfig: engines: >=4.1', t => {
185+
const config = manager.buildConfig({engines: {node: '>=5.1'}});
186+
187+
// Do not include rules for Node.js 5 and above
188+
t.is(config.rules['unicorn/prefer-spread'], 'error');
189+
// Do not include rules for Node.js 6 and above
190+
t.is(config.rules['prefer-rest-params'], undefined);
191+
t.is(config.rules['prefer-destructuring'], undefined);
192+
// Do not include rules for Node.js 8 and above
193+
t.is(config.rules['promise/prefer-await-to-then'], undefined);
194+
});
195+
196+
test('buildConfig: engines: >=6', t => {
197+
const config = manager.buildConfig({engines: {node: '>=6'}});
198+
199+
// Include rules for Node.js 5 and above
200+
t.is(config.rules['unicorn/prefer-spread'], 'error');
201+
// Include rules for Node.js 6 and above
202+
t.is(config.rules['prefer-rest-params'], 'error');
203+
t.deepEqual(config.rules['prefer-destructuring'], [
204+
'error', {array: true, object: true}, {enforceForRenamedProperties: true}
205+
]);
206+
// Do not include rules for Node.js 8 and above
207+
t.is(config.rules['promise/prefer-await-to-then'], undefined);
208+
});
209+
210+
test('buildConfig: engines: >=8', t => {
211+
const config = manager.buildConfig({engines: {node: '>=8'}});
212+
213+
// Include rules for Node.js 5 and above
214+
t.is(config.rules['unicorn/prefer-spread'], 'error');
215+
// Include rules for Node.js 6 and above
216+
t.is(config.rules['prefer-rest-params'], 'error');
217+
t.deepEqual(config.rules['prefer-destructuring'], [
218+
'error', {array: true, object: true}, {enforceForRenamedProperties: true}
219+
]);
220+
// Include rules for Node.js 8 and above
221+
t.is(config.rules['promise/prefer-await-to-then'], 'error');
222+
});
223+
141224
test('mergeWithPrettierConf: use `singleQuote`, `trailingComma`, `bracketSpacing` and `jsxBracketSameLine` from `prettier` config if defined', t => {
142225
const cwd = path.resolve('fixtures', 'prettier');
143226
const result = manager.mergeWithPrettierConf({cwd});
@@ -255,26 +338,47 @@ test('groupConfigs', t => {
255338
test('mergeWithPkgConf: use child if closest', t => {
256339
const cwd = path.resolve('fixtures', 'nested', 'child');
257340
const result = manager.mergeWithPkgConf({cwd});
258-
const expected = Object.assign({}, childConfig.xo, {cwd});
341+
const expected = Object.assign({}, childConfig.xo, {cwd}, {engines: {}});
259342
t.deepEqual(result, expected);
260343
});
261344

262345
test('mergeWithPkgConf: use parent if closest', t => {
263346
const cwd = path.resolve('fixtures', 'nested');
264347
const result = manager.mergeWithPkgConf({cwd});
265-
const expected = Object.assign({}, parentConfig.xo, {cwd});
348+
const expected = Object.assign({}, parentConfig.xo, {cwd}, {engines: {}});
266349
t.deepEqual(result, expected);
267350
});
268351

269352
test('mergeWithPkgConf: use parent if child is ignored', t => {
270353
const cwd = path.resolve('fixtures', 'nested', 'child-ignore');
271354
const result = manager.mergeWithPkgConf({cwd});
272-
const expected = Object.assign({}, parentConfig.xo, {cwd});
355+
const expected = Object.assign({}, parentConfig.xo, {cwd}, {engines: {}});
273356
t.deepEqual(result, expected);
274357
});
275358

276359
test('mergeWithPkgConf: use child if child is empty', t => {
277360
const cwd = path.resolve('fixtures', 'nested', 'child-empty');
278361
const result = manager.mergeWithPkgConf({cwd});
279-
t.deepEqual(result, {cwd});
362+
t.deepEqual(result, {cwd, engines: {}});
363+
});
364+
365+
test('mergeWithPkgConf: read engines from package.json', t => {
366+
const cwd = path.resolve('fixtures', 'engines');
367+
const result = manager.mergeWithPkgConf({cwd});
368+
const expected = Object.assign({}, {engines: enginesConfig.engines}, {cwd});
369+
t.deepEqual(result, expected);
370+
});
371+
372+
test('mergeWithPkgConf: XO engine options supersede package.json\'s', t => {
373+
const cwd = path.resolve('fixtures', 'engines');
374+
const result = manager.mergeWithPkgConf({cwd, engines: {node: '>=8'}});
375+
const expected = Object.assign({}, {engines: {node: '>=8'}}, {cwd});
376+
t.deepEqual(result, expected);
377+
});
378+
379+
test('mergeWithPkgConf: XO engine options false supersede package.json\'s', t => {
380+
const cwd = path.resolve('fixtures', 'engines');
381+
const result = manager.mergeWithPkgConf({cwd, engines: false});
382+
const expected = Object.assign({}, {engines: false}, {cwd});
383+
t.deepEqual(result, expected);
280384
});

0 commit comments

Comments
0 (0)
You are visitor 01402809 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.