From eabec089180c78ed6a8af3cc161d9cbbabdeb49c Mon Sep 17 00:00:00 2001 From: Gio d'Amelio Date: Sun, 16 Oct 2016 14:18:32 -0700 Subject: [PATCH] Add 'always-with-newlines' value to order's newlines-between option to allow newlines inside import groups Closes #627 --- CHANGELOG.md | 5 +++++ docs/rules/order.md | 23 ++++++++++++++++++++++- src/rules/order.js | 19 +++++++++++++------ tests/src/rules/order.js | 26 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ce7ac7734..672b4f589e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Fixed - [`prefer-default-export`] handles re-exported default exports ([#609]) +### Added +- Add new value to `order`'s `newlines-between` option to allow newlines inside import groups ([#627], [#628]) + ## [2.0.1] - 2016-10-06 ### Fixed - Fixed code that relied on removed dependencies. ([#604]) @@ -400,6 +403,8 @@ for info on changes for earlier releases. [#157]: https://github.com/benmosher/eslint-plugin-import/pull/157 [#314]: https://github.com/benmosher/eslint-plugin-import/pull/314 +[#628]: https://github.com/benmosher/eslint-plugin-import/issues/628 +[#627]: https://github.com/benmosher/eslint-plugin-import/issues/627 [#609]: https://github.com/benmosher/eslint-plugin-import/issues/609 [#604]: https://github.com/benmosher/eslint-plugin-import/issues/604 [#577]: https://github.com/benmosher/eslint-plugin-import/issues/577 diff --git a/docs/rules/order.md b/docs/rules/order.md index 46a1dbd7f8..9333f2b6a9 100644 --- a/docs/rules/order.md +++ b/docs/rules/order.md @@ -92,13 +92,14 @@ You can set the options like this: "import/order": ["error", {"groups": ["index", "sibling", "parent", "internal", "external", "builtin"]}] ``` -### `newlines-between: [ignore|always|never]`: +### `newlines-between: [ignore|always|always-with-newlines|never]`: Enforces or forbids new lines between import groups: - If set to `ignore`, no errors related to new lines between import groups will be reported (default). - If set to `always`, at least one new line between each group will be enforced, and new lines inside a group will be forbidden. To prevent multiple lines between imports, core `no-multiple-empty-lines` rule can be used. +- If set to `always-with-newlines`, it will act like `always` except newlines are allowed inside import groups. - If set to `never`, no new lines are allowed in the entire import section. With the default group setting, the following will be invalid: @@ -111,6 +112,15 @@ import index from './'; import sibling from './foo'; ``` +```js +/* eslint import/order: ["error", {"newlines-between": "always-with-newlines"}] */ +import fs from 'fs'; + +import path from 'path'; +import index from './'; +import sibling from './foo'; +``` + ```js /* eslint import/order: ["error", {"newlines-between": "never"}] */ import fs from 'fs'; @@ -133,6 +143,17 @@ import index from './'; import sibling from './foo'; ``` +```js +/* eslint import/order: ["error", {"newlines-between": "always-with-newlines"}] */ +import fs from 'fs'; + +import path from 'path'; + +import index from './'; + +import sibling from './foo'; +``` + ```js /* eslint import/order: ["error", {"newlines-between": "never"}] */ import fs from 'fs'; diff --git a/src/rules/order.js b/src/rules/order.js index da4cae1aeb..132f019c7c 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -120,22 +120,24 @@ function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) { let previousImport = imported[0] imported.slice(1).forEach(function(currentImport) { - if (newlinesBetweenImports === 'always') { - if (currentImport.rank !== previousImport.rank - && getNumberOfEmptyLinesBetween(currentImport, previousImport) === 0) + const emptyLinesBetween = getNumberOfEmptyLinesBetween(currentImport, previousImport) + + if (newlinesBetweenImports === 'always' || newlinesBetweenImports === 'always-with-newlines') { + if (currentImport.rank !== previousImport.rank && emptyLinesBetween === 0) { context.report( previousImport.node, 'There should be at least one empty line between import groups' ) } else if (currentImport.rank === previousImport.rank - && getNumberOfEmptyLinesBetween(currentImport, previousImport) > 0) + && emptyLinesBetween > 0 + && newlinesBetweenImports !== 'always-with-newlines') { context.report( previousImport.node, 'There should be no empty line within import group' ) } } else { - if (getNumberOfEmptyLinesBetween(currentImport, previousImport) > 0) { + if (emptyLinesBetween > 0) { context.report(previousImport.node, 'There should be no empty line between import groups') } } @@ -156,7 +158,12 @@ module.exports = { type: 'array', }, 'newlines-between': { - enum: [ 'ignore', 'always', 'never' ], + enum: [ + 'ignore', + 'always', + 'always-with-newlines', + 'never', + ], }, }, additionalProperties: false, diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 68f06c05fc..09a7bd76a8 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -376,6 +376,32 @@ ruleTester.run('order', rule, { `, options: [{ 'newlines-between': 'always' }] }), + // Option: newlines-between: 'always-with-newlines' + test({ + code: ` + var fs = require('fs'); + var path = require('path'); + + var util = require('util'); + + var async = require('async'); + + var relParent1 = require('../foo'); + var relParent2 = require('../'); + + var relParent3 = require('../bar'); + + var sibling = require('./foo'); + var sibling2 = require('./bar'); + + var sibling3 = require('./foobar'); + `, + options: [ + { + 'newlines-between': 'always-with-newlines', + }, + ], + }), ], invalid: [ // builtin before external module (require)