GeezerVison is showing github.com — Enforce rules based on "engines" field in package.json (#281) · xojs/xo@0d18368
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: config/plugins.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,8 +31,6 @@ module.exports = { | 31 | 31 | rules: { | 32 | 32 | 'no-use-extend-native/no-use-extend-native': 'error', | 33 | 33 | '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', | 36 | 34 | 'promise/no-return-wrap': ['error', {allowReject: true}], | 37 | 35 | 'promise/no-return-in-finally': 'error', | 38 | 36 | 'import/default': 'error', | @@ -81,6 +79,9 @@ module.exports = { | 81 | 79 | // 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 | 82 | 80 | // 'node/shebang': 'error', | 83 | 81 | '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' | 85 | 86 | } | 86 | 87 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/options-manager.js +54-1Lines changed: 54 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ const pathExists = require('path-exists'); | 8 | 8 | const pkgConf = require('pkg-conf'); | 9 | 9 | const resolveFrom = require('resolve-from'); | 10 | 10 | const prettier = require('prettier'); | | 11 | +const semver = require('semver'); | 11 | 12 |
| 12 | 13 | const DEFAULT_IGNORE = [ | 13 | 14 | '**/node_modules/**', | @@ -42,6 +43,46 @@ const DEFAULT_CONFIG = { | 42 | 43 | } | 43 | 44 | }; | 44 | 45 |
| | 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 | + | 45 | 86 | // Keep the same behaviour in mergeWith as deepAssign | 46 | 87 | const mergeFn = (prev, val) => { | 47 | 88 | if (Array.isArray(prev) && Array.isArray(val)) { | @@ -88,7 +129,8 @@ const mergeWithPkgConf = opts => { | 88 | 129 | opts = Object.assign({cwd: process.cwd()}, opts); | 89 | 130 | opts.cwd = path.resolve(opts.cwd); | 90 | 131 | 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); | 92 | 134 | }; | 93 | 135 |
| 94 | 136 | const normalizeSpaces = opts => { | @@ -129,6 +171,17 @@ const buildConfig = opts => { | 129 | 171 | ); | 130 | 172 | const spaces = normalizeSpaces(opts); | 131 | 173 |
| | 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 | + | 132 | 185 | if (opts.space) { | 133 | 186 | config.rules.indent = ['error', spaces, {SwitchCase: 1}]; | 134 | 187 |
| |
Collapse file Copy file name to clipboardExpand all lines: main.js +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ const updateNotifier = require('update-notifier'); | 4 | 4 | const getStdin = require('get-stdin'); | 5 | 5 | const meow = require('meow'); | 6 | 6 | const formatterPretty = require('eslint-formatter-pretty'); | | 7 | +const semver = require('semver'); | 7 | 8 | const openReport = require('./lib/open-report'); | 8 | 9 | const xo = require('.'); | 9 | 10 |
| @@ -21,6 +22,7 @@ const cli = meow(` | 21 | 22 | --space Use space indent instead of tabs [Default: 2] | 22 | 23 | --no-semicolon Prevent use of semicolons | 23 | 24 | --prettier Conform to Prettier code style | | 25 | + --node-version Range of Node.js version to support | 24 | 26 | --plugin Include third-party plugins [Can be set multiple times] | 25 | 27 | --extend Extend defaults with a custom config [Can be set multiple times] | 26 | 28 | --open Open files with issues in your editor | @@ -76,6 +78,9 @@ const cli = meow(` | 76 | 78 | prettier: { | 77 | 79 | type: 'boolean' | 78 | 80 | }, | | 81 | + nodeVersion: { | | 82 | + type: 'string' | | 83 | + }, | 79 | 84 | plugin: { | 80 | 85 | type: 'string' | 81 | 86 | }, | @@ -136,6 +141,17 @@ if (input[0] === '-') { | 136 | 141 | input.shift(); | 137 | 142 | } | 138 | 143 |
| | 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 | + | 139 | 155 | if (opts.init) { | 140 | 156 | require('xo-init')(); | 141 | 157 | } else if (opts.stdin) { | |
Collapse file Copy file name to clipboardExpand all lines: package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 97 | 97 | "prettier": "~1.10.2", | 98 | 98 | "resolve-cwd": "^2.0.0", | 99 | 99 | "resolve-from": "^4.0.0", | | 100 | + "semver": "^5.4.1", | 100 | 101 | "slash": "^1.0.0", | 101 | 102 | "update-notifier": "^2.1.0", | 102 | 103 | "xo-init": "^0.6.0" | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ Uses [ESLint](http://eslint.org) underneath, so issues regarding rules should be | 27 | 27 | - No need to specify file paths to lint as it lints all JS files except for [commonly ignored paths](#ignores). | 28 | 28 | - [Config overrides per files/globs.](#config-overrides) | 29 | 29 | - 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`. | 30 | 31 | - Caches results between runs for much better performance. | 31 | 32 | - Super simple to add XO to a project with `$ xo --init`. | 32 | 33 | - Fix many issues automagically with `$ xo --fix`. | @@ -61,6 +62,7 @@ $ xo --help | 61 | 62 | --space Use space indent instead of tabs [Default: 2] | 62 | 63 | --no-semicolon Prevent use of semicolons | 63 | 64 | --prettier Conform to Prettier code style | | 65 | + --node-version Range of Node.js version to support | 64 | 66 | --plugin Include third-party plugins [Can be set multiple times] | 65 | 67 | --extend Extend defaults with a custom config [Can be set multiple times] | 66 | 68 | --open Open files with issues in your editor | @@ -206,6 +208,14 @@ Default: `false` | 206 | 208 |
| 207 | 209 | 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) | 208 | 210 |
| | 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 | + | 209 | 219 | ### plugins | 210 | 220 |
| 211 | 221 | Type: `Array` | @@ -303,6 +313,25 @@ If you have a directory structure with nested `package.json` files and you want | 303 | 313 |
| 304 | 314 | Put a `package.json` with your config at the root and add `"xo": false` to the `package.json` in your bundled packages. | 305 | 315 |
| | 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 | + | 306 | 335 |
| 307 | 336 | ## FAQ | 308 | 337 |
| |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "name": "application-name", | | 3 | + "version": "0.0.1", | | 4 | + "engines": { | | 5 | + "node": ">=6" | | 6 | + } | | 7 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/main.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,3 +94,9 @@ test('init option', async t => { | 94 | 94 | const packageJson = fs.readFileSync(filepath, 'utf8'); | 95 | 95 | t.deepEqual(JSON.parse(packageJson).scripts, {test: 'xo'}); | 96 | 96 | }); | | 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 Copy file name to clipboardExpand all lines: test/options-manager.js +108-4Lines changed: 108 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import proxyquire from 'proxyquire'; | 4 | 4 | import parentConfig from './fixtures/nested/package'; | 5 | 5 | import childConfig from './fixtures/nested/child/package'; | 6 | 6 | import prettierConfig from './fixtures/prettier/package'; | | 7 | +import enginesConfig from './fixtures/engines/package'; | 7 | 8 |
| 8 | 9 | process.chdir(__dirname); | 9 | 10 |
| @@ -138,6 +139,88 @@ test('buildConfig: prettier: true, esnext: false', t => { | 138 | 139 | }]); | 139 | 140 | }); | 140 | 141 |
| | 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 | + | 141 | 224 | test('mergeWithPrettierConf: use `singleQuote`, `trailingComma`, `bracketSpacing` and `jsxBracketSameLine` from `prettier` config if defined', t => { | 142 | 225 | const cwd = path.resolve('fixtures', 'prettier'); | 143 | 226 | const result = manager.mergeWithPrettierConf({cwd}); | @@ -255,26 +338,47 @@ test('groupConfigs', t => { | 255 | 338 | test('mergeWithPkgConf: use child if closest', t => { | 256 | 339 | const cwd = path.resolve('fixtures', 'nested', 'child'); | 257 | 340 | const result = manager.mergeWithPkgConf({cwd}); | 258 | | - const expected = Object.assign({}, childConfig.xo, {cwd}); | | 341 | + const expected = Object.assign({}, childConfig.xo, {cwd}, {engines: {}}); | 259 | 342 | t.deepEqual(result, expected); | 260 | 343 | }); | 261 | 344 |
| 262 | 345 | test('mergeWithPkgConf: use parent if closest', t => { | 263 | 346 | const cwd = path.resolve('fixtures', 'nested'); | 264 | 347 | const result = manager.mergeWithPkgConf({cwd}); | 265 | | - const expected = Object.assign({}, parentConfig.xo, {cwd}); | | 348 | + const expected = Object.assign({}, parentConfig.xo, {cwd}, {engines: {}}); | 266 | 349 | t.deepEqual(result, expected); | 267 | 350 | }); | 268 | 351 |
| 269 | 352 | test('mergeWithPkgConf: use parent if child is ignored', t => { | 270 | 353 | const cwd = path.resolve('fixtures', 'nested', 'child-ignore'); | 271 | 354 | const result = manager.mergeWithPkgConf({cwd}); | 272 | | - const expected = Object.assign({}, parentConfig.xo, {cwd}); | | 355 | + const expected = Object.assign({}, parentConfig.xo, {cwd}, {engines: {}}); | 273 | 356 | t.deepEqual(result, expected); | 274 | 357 | }); | 275 | 358 |
| 276 | 359 | test('mergeWithPkgConf: use child if child is empty', t => { | 277 | 360 | const cwd = path.resolve('fixtures', 'nested', 'child-empty'); | 278 | 361 | 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); | 280 | 384 | }); |
|