Skip to content

Commit

Permalink
add considerComments option
Browse files Browse the repository at this point in the history
  • Loading branch information
pri1311 committed Mar 22, 2022
1 parent a7cd131 commit cc3fbcb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
29 changes: 28 additions & 1 deletion docs/rules/newline-after-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ Enforces having one or more empty lines after the last top-level import statemen

## Rule Details

This rule has one option, `count` which sets the number of newlines that are enforced after the last top-level import statement or require call. This option defaults to `1`.
This rule supports the following options:
- `count` which sets the number of newlines that are enforced after the last top-level import statement or require call. This option defaults to `1`.

- `considerComments` which enforces the rule on comments after the last import-statement as well when set to true. This option defaults to `false`.

Valid:

Expand Down Expand Up @@ -71,6 +74,30 @@ import defaultExport from './foo'
const FOO = 'BAR'
```

With `considerComments` set to `false` this will be considered valid:

```js
import defaultExport from './foo'
// some comment here.
const FOO = 'BAR'
```

With `considerComments` set to `true` this will be considered valid:

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

// some comment here.
const FOO = 'BAR'
```

With `considerComments` set to `true` this will be considered invalid:

```js
import defaultExport from './foo'
// some comment here.
const FOO = 'BAR'
```

## Example options usage
```json
Expand Down
8 changes: 5 additions & 3 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module.exports = {
'type': 'integer',
'minimum': 1,
},
'considerComments': { 'type': 'boolean' },
},
'additionalProperties': false,
},
Expand All @@ -87,7 +88,7 @@ module.exports = {
nextNode = nextNode.decorators[0];
}

const options = context.options[0] || { count: 1 };
const options = context.options[0] && 'count' in context.options[0]? context.options[0] : { count: 1 };
const lineDifference = getLineDifference(node, nextNode);
const EXPECTED_LINE_DIFFERENCE = options.count + 1;

Expand All @@ -114,7 +115,7 @@ after ${type} statement not followed by another ${type}.`,
}

function commentAfterImport(node, nextComment) {
const options = context.options[0] || { count: 1 };
const options = context.options[0] && 'count' in context.options[0] ? context.options[0] : { count: 1 };
const lineDifference = getLineDifference(node, nextComment);
const EXPECTED_LINE_DIFFERENCE = options.count + 1;

Expand Down Expand Up @@ -148,13 +149,14 @@ after import statement not followed by another import.`,
}

function checkImport(node) {
const options = context.options[0] && 'considerComments' in context.options[0] ? context.options[0] : { considerComments: false };
const { parent } = node;
const nodePosition = parent.body.indexOf(node);
const nextNode = parent.body[nodePosition + 1];
const endLine = node.loc.end.line;
let nextComment;

if (typeof parent.comments !== 'undefined') {
if (typeof parent.comments !== 'undefined' && options.considerComments) {
nextComment = parent.comments.find(o => o.loc.start.line === endLine + 1);
}

Expand Down
8 changes: 5 additions & 3 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
{
code: `
const x = () => require('baz') && require('bar')
// Some random single line comment
var bar = 42;
`,
parserOptions: { ecmaVersion: 6 } ,
options: [{ 'considerComments': true }],
},
{
code: `
const x = () => require('baz') && require('bar')
/**
* some multiline comment here
* another line of comment
Expand Down Expand Up @@ -280,7 +280,6 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
code: `
import path from 'path';
import foo from 'foo';
/**
* some multiline comment here
* another line of comment
Expand All @@ -300,6 +299,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
var bar = 42;
`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' } ,
options: [{ 'considerComments': true }],
},
{
code: `
Expand Down Expand Up @@ -339,6 +339,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
message: IMPORT_ERROR_MESSAGE,
} ],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' } ,
options: [{ 'considerComments': true }],
},
{
code: `
Expand All @@ -359,6 +360,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
message: IMPORT_ERROR_MESSAGE,
} ],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' } ,
options: [{ 'considerComments': true, 'count': 1 }],
},
{
code: `import foo from 'foo';\nexport default function() {};`,
Expand Down

0 comments on commit cc3fbcb

Please sign in to comment.