From 9507de9e20cec85ab268590dfed4d263d0a22f03 Mon Sep 17 00:00:00 2001 From: Gio d'Amelio Date: Tue, 15 Nov 2016 15:06:11 -0800 Subject: [PATCH] Add 'always-and-inside-groups' value to order's newlines-between option to allow newlines inside import groups Closes #627 --- CHANGELOG.md | 3 +++ docs/rules/order.md | 23 ++++++++++++++++++++++- src/rules/order.js | 20 ++++++++++++++------ tests/src/rules/order.js | 26 ++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7131e79b..444924aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## [Unreleased] ### Added - [`no-anonymous-default-export`] rule: report anonymous default exports ([#712], thanks [@duncanbeevers]). +- Add new value to `order`'s `newlines-between` option to allow newlines inside import groups ([#627], [#628]) ### Changed - [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg]) @@ -436,6 +437,8 @@ for info on changes for earlier releases. [#660]: https://github.com/benmosher/eslint-plugin-import/issues/660 [#653]: https://github.com/benmosher/eslint-plugin-import/issues/653 +[#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 [#602]: https://github.com/benmosher/eslint-plugin-import/issues/602 diff --git a/docs/rules/order.md b/docs/rules/order.md index fd2c16c15..e0c4887dc 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-and-inside-groups|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-and-inside-groups`, 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-and-inside-groups"}] */ +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-and-inside-groups"}] */ +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 da4cae1ae..a3be499a4 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -120,22 +120,25 @@ 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-and-inside-groups') { + 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-and-inside-groups') { 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 +159,12 @@ module.exports = { type: 'array', }, 'newlines-between': { - enum: [ 'ignore', 'always', 'never' ], + enum: [ + 'ignore', + 'always', + 'always-and-inside-groups', + 'never', + ], }, }, additionalProperties: false, diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 68f06c05f..c87508573 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-and-inside-groups' + 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-and-inside-groups', + }, + ], + }), ], invalid: [ // builtin before external module (require)