-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
radekbenkel
wants to merge
2
commits into
import-js:master
from
radekbenkel:new-rule-newline-after-imports
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
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') | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
}, | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 eitherimport
orrequire
.