Skip to content

Commit

Permalink
Breaking - newline-after-import: Do not require empty line after inli…
Browse files Browse the repository at this point in the history
…ne require (#570)
  • Loading branch information
jfmengels committed Sep 23, 2016
1 parent c8dcf4d commit b33a66f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
17 changes: 14 additions & 3 deletions docs/rules/newline-after-import.md
@@ -1,11 +1,9 @@
# newline-after-import

Reports if there's no new line after last import/require in group.
Enforces having an empty line after the last top-level import statement or require call.

## Rule Details

**NOTE**: In each of those examples you can replace `import` call with `require`.

Valid:

```js
Expand All @@ -21,6 +19,13 @@ import { bar } from 'bar-lib'
const FOO = 'BAR'
```

```js
const FOO = require('./foo')
const BAR = require('./bar')

const BAZ = 1
```

...whereas here imports will be reported:

```js
Expand All @@ -35,6 +40,12 @@ const FOO = 'BAR'
import { bar } from 'bar-lib'
```

```js
const FOO = require('./foo')
const BAZ = 1
const BAR = require('./bar')
```

## When Not To Use It

If you like to visually group module imports with its usage, you don't want to use this rule.
20 changes: 19 additions & 1 deletion src/rules/newline-after-import.js
Expand Up @@ -63,6 +63,14 @@ module.exports = function (context) {
}
}

let level = 0
function incrementLevel() {
level++
}
function decrementLevel() {
level--
}

return {
ImportDeclaration: function (node) {
const { parent } = node
Expand All @@ -78,7 +86,7 @@ module.exports = function (context) {
},
CallExpression: function(node) {
const scope = context.getScope()
if (isStaticRequire(node)) {
if (isStaticRequire(node) && level === 0) {
const currentScope = scopes[scopeIndex]

if (scope === currentScope.scope) {
Expand Down Expand Up @@ -122,5 +130,15 @@ module.exports = function (context) {
})
})
},
FunctionDeclaration: incrementLevel,
FunctionExpression: incrementLevel,
ArrowFunctionExpression: incrementLevel,
BlockStatement: incrementLevel,
ObjectExpression: incrementLevel,
'FunctionDeclaration:exit': decrementLevel,
'FunctionExpression:exit': decrementLevel,
'ArrowFunctionExpression:exit': decrementLevel,
'BlockStatement:exit': decrementLevel,
'ObjectExpression:exit': decrementLevel,
}
}
43 changes: 31 additions & 12 deletions tests/src/rules/newline-after-import.js
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint'

const IMPORT_ERROR_MESSAGE = 'Expected empty line after import statement not followed by another import.';
const REQUIRE_ERROR_MESSAGE = 'Expected empty line after require statement not followed by another require.';
const IMPORT_ERROR_MESSAGE = 'Expected empty line after import statement not followed by another import.'
const REQUIRE_ERROR_MESSAGE = 'Expected empty line after require statement not followed by another require.'

const ruleTester = new RuleTester()

Expand All @@ -21,7 +21,6 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 6 } ,
},
"function x(){ require('baz'); }",

"a(require('b'), require('c'), require('d'));",
`function foo() {
switch (renderData.modalViewKey) {
Expand Down Expand Up @@ -100,7 +99,35 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
{
code: "var foo = require('foo-module');\n\nvar a = 123;\n\nvar bar = require('bar-lib');",
parserOptions: { sourceType: 'module' }
}
},
{
code: `
function foo() {
var foo = require('foo');
foo();
}
`,
parserOptions: { sourceType: 'module' },
},
{
code: `
if (true) {
var foo = require('foo');
foo();
}
`,
parserOptions: { sourceType: 'module' },
},
{
code: `
function a() {
var assign = Object.assign || require('object-assign');
var foo = require('foo');
var bar = 42;
}
`,
parserOptions: { sourceType: 'module' },
},
],

invalid: [
Expand Down Expand Up @@ -183,14 +210,6 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
message: REQUIRE_ERROR_MESSAGE,
} ]
},
{
code: "function a() {\nvar assign = Object.assign || require('object-assign');\nvar foo = require('foo');\nvar bar = 42; }",
errors: [ {
line: 3,
column: 1,
message: REQUIRE_ERROR_MESSAGE,
} ]
},
{
code: "require('a');\nfoo(require('b'), require('c'), require('d'));\nrequire('d');\nvar foo = 'bar';",
errors: [
Expand Down

0 comments on commit b33a66f

Please sign in to comment.