Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add newline-after-import rule #245

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
* Report namespace imports ([`no-namespace`])
* Ensure consistent use of file extension within the import path ([`extensions`])
* Enforce a convention in module import order ([`order`])
* Enforce a newline after import statements ([`newline-after-import`])

[`imports-first`]: ./docs/rules/imports-first.md
[`no-duplicates`]: ./docs/rules/no-duplicates.md
Expand Down
40 changes: 40 additions & 0 deletions docs/rules/newline-after-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# newline-after-import

Reports if there's no new line after last import/require in group.

## Rule Details

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

Valid:

```js
import defaultExport from './foo'

const FOO = 'BAR'
```

```js
import defaultExport from './foo'
import { bar } from 'bar-lib'

const FOO = 'BAR'
```

...whereas here imports will be reported:

```js
import * as foo from 'foo'
const FOO = 'BAR'
```

```js
import * as foo from 'foo'
const FOO = 'BAR'

import { bar } from 'bar-lib'
```

## When Not To Use It

If you like to visually group module imports with its usage, you don't want to use this rule.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const rules = {
'no-extraneous-dependencies': require('./rules/no-extraneous-dependencies'),
'no-nodejs-modules': require('./rules/no-nodejs-modules'),
'order': require('./rules/order'),
'newline-after-import': require('./rules/newline-after-import'),

// metadata-based
'no-deprecated': require('./rules/no-deprecated'),
Expand Down
50 changes: 50 additions & 0 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @fileoverview Rule to enforce new line after import not followed by another import.
* @author Radek Benkel
*/

import isStaticRequire from '../core/staticRequire'

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

function getLineDifference(node, nextToken) {
return nextToken.loc.start.line - node.loc.start.line
}

function ensureNoForbiddenKeyword(context, node, tokenToInspect, tokenValue) {
if (!tokenToInspect) {
return
}

if (getLineDifference(node, tokenToInspect) === 1
&& tokenToInspect.type === 'Keyword' && tokenToInspect.value !== tokenValue)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, but I have a hard time understanding the name ensureNoForbiddenKeyword (never did any rules that dealt with tokens), and how/what exactly it it testing (except the line difference thing, that I got). Do you mind explaining it to me?

Copy link
Contributor Author

@singles singles Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Role of that function is to check line difference AND whether next token is something different that import/require - if it is different, then report an error, because only thing that can be directly in next line is either import or require.

{
context.report({
loc: tokenToInspect.loc.start,
message: 'Expected empty line after ' + tokenValue +
' statement not followed by another ' + tokenValue + '.',
})
}
}

module.exports = function (context) {
return {
ImportDeclaration: function (node) {
const nextToken = context.getSourceCode(node).getTokenAfter(node)

ensureNoForbiddenKeyword(context, node, nextToken, 'import')
},
CallExpression: function(node) {
if (isStaticRequire(node)) {
const nextTokens = context.getSourceCode(node).getTokensAfter(node, 2)
const tokenToInspect = nextTokens.length > 1 && nextTokens[0].type === 'Punctuator'
? nextTokens[1]
: nextTokens[0]

ensureNoForbiddenKeyword(context, node, tokenToInspect, 'require')
}
},
}
}
101 changes: 101 additions & 0 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 ruleTester = new RuleTester()

ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
valid: [
{
code: "import foo from 'foo';\n\nvar foo = 'bar';",
parserOptions: { sourceType: 'module' }
},
{
code: "var foo = require('foo-module');\n\nvar foo = 'bar';",
parserOptions: { sourceType: 'module' }
},
{
code: "require('foo-module');\n\nvar foo = 'bar';",
parserOptions: { sourceType: 'module' }
},
{
code: "import foo from 'foo';\nimport { bar } from './bar-lib';",
parserOptions: { sourceType: 'module' }
},
{
code: "import foo from 'foo';\n\nvar a = 123;\n\nimport { bar } from './bar-lib';",
parserOptions: { sourceType: 'module' }
},
{
code: "var foo = require('foo-module');\n\nvar a = 123;\n\nvar bar = require('bar-lib');",
parserOptions: { sourceType: 'module' }
}
],

invalid: [
{
code: "import foo from 'foo';\nexport default function() {};",
errors: [ {
line: 2,
column: 1,
message: IMPORT_ERROR_MESSAGE
} ],
parserOptions: { sourceType: 'module' }
},
{
code: "var foo = require('foo-module');\nvar something = 123;",
errors: [ {
line: 2,
column: 1,
message: REQUIRE_ERROR_MESSAGE
} ],
parserOptions: { sourceType: 'module' }
},
{
code: "import foo from 'foo';\nvar a = 123;\n\nimport { bar } from './bar-lib';\nvar b=456;",
errors: [
{
line: 2,
column: 1,
message: IMPORT_ERROR_MESSAGE
},
{
line: 5,
column: 1,
message: IMPORT_ERROR_MESSAGE
}],
parserOptions: { sourceType: 'module' }
},
{
code: "var foo = require('foo-module');\nvar a = 123;\n\nvar bar = require('bar-lib');\nvar b=456;",
errors: [
{
line: 2,
column: 1,
message: REQUIRE_ERROR_MESSAGE
},
{
line: 5,
column: 1,
message: REQUIRE_ERROR_MESSAGE
}],
parserOptions: { sourceType: 'module' }
},
{
code: "var foo = require('foo-module');\nvar a = 123;\n\nrequire('bar-lib');\nvar b=456;",
errors: [
{
line: 2,
column: 1,
message: REQUIRE_ERROR_MESSAGE
},
{
line: 5,
column: 1,
message: REQUIRE_ERROR_MESSAGE
}],
parserOptions: { sourceType: 'module' }
},
]
});