Skip to content

Commit

Permalink
Merge 3f05cdf into 13aa29c
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Oct 27, 2020
2 parents 13aa29c + 3f05cdf commit 847066c
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 9 deletions.
53 changes: 52 additions & 1 deletion docs/rules/newline-after-import.md
Expand Up @@ -5,7 +5,12 @@ 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 accepts two options,

1. `count` which sets the number of newlines that are enforced after the last top-level import statement or require call. The number of newlines should be greater than or equal to `count`. This option defaults to `1`.

2. `exactCount` which enforce the exact numbers of newlines that is mentioned in `count`. This option defaults to `false`.


Valid:

Expand Down Expand Up @@ -55,6 +60,14 @@ With `count` set to `2` this will be considered valid:
import defaultExport from './foo'


const FOO = 'BAR'
```

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



const FOO = 'BAR'
```

Expand All @@ -71,6 +84,44 @@ import defaultExport from './foo'
const FOO = 'BAR'
```

With `count` set to `2` and `exactCount` set to `true` this will be considered valid:

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


const FOO = 'BAR'
```

With `count` set to `2` and `exactCount` set to `true` these will be considered invalid:

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

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

const FOO = 'BAR'
```

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



const FOO = 'BAR'
```

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




const FOO = 'BAR'
```

## Example options usage
```json
Expand Down
26 changes: 18 additions & 8 deletions src/rules/newline-after-import.js
Expand Up @@ -62,6 +62,10 @@ module.exports = {
'type': 'integer',
'minimum': 1,
},
'exactCount': {
'type': 'boolean',
'default': false,
},
},
'additionalProperties': false,
},
Expand All @@ -82,11 +86,12 @@ module.exports = {
nextNode = nextNode.decorators[0]
}

const options = context.options[0] || { count: 1 }
const options = context.options[0] || { count: 1, exactCount: false }
const lineDifference = getLineDifference(node, nextNode)
const EXPECTED_LINE_DIFFERENCE = options.count + 1

if (lineDifference < EXPECTED_LINE_DIFFERENCE) {
if (lineDifference < EXPECTED_LINE_DIFFERENCE ||
options.exactCount && lineDifference !== EXPECTED_LINE_DIFFERENCE) {
let column = node.loc.start.column

if (node.loc.start.line !== node.loc.end.line) {
Expand All @@ -100,10 +105,15 @@ module.exports = {
},
message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} \
after ${type} statement not followed by another ${type}.`,
fix: fixer => fixer.insertTextAfter(
node,
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference)
),
fix: fixer => {
if (options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference) {
return null
}
return fixer.insertTextAfter(
node,
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference)
)
},
})
}
}
Expand All @@ -119,7 +129,7 @@ after ${type} statement not followed by another ${type}.`,
const { parent } = node
const nodePosition = parent.body.indexOf(node)
const nextNode = parent.body[nodePosition + 1]

// skip "export import"s
if (node.type === 'TSImportEqualsDeclaration' && node.isExport) {
return
Expand Down Expand Up @@ -158,7 +168,7 @@ after ${type} statement not followed by another ${type}.`,
if (nextStatement &&
(!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) {

checkForNewLine(statementWithRequireCall, nextStatement, 'require')
checkForNewLine(statementWithRequireCall, nextStatement, 'require')
}
})
},
Expand Down
75 changes: 75 additions & 0 deletions tests/src/rules/newline-after-import.js
Expand Up @@ -91,6 +91,21 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ 'count': 2 }],
},
{
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ 'count': 2, 'exactCount': true }],
},
{
code: `import foo from 'foo';\n\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ 'count': 1, 'exactCount': true }],
},
{
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ 'count': 1 }],
},
{
code: `import foo from 'foo';\n\n\n\n\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
Expand All @@ -110,6 +125,11 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ 'count': 4 }],
},
{
code: `var foo = require('foo-module');\n\n\n\n\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ 'count': 4, 'exactCount' : true }],
},
{
code: `require('foo-module');\n\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
Expand Down Expand Up @@ -429,5 +449,60 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
{
code: `import foo from 'foo';\n\nexport default function() {};`,
output: `import foo from 'foo';\n\n\nexport default function() {};`,
options: [{ 'count': 2, exactCount: true }],
errors: [ {
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
} ],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n\n\nexport default function() {};`,
output: null,
options: [{ 'count': 2, exactCount: true }],
errors: [ {
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
} ],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n\n\n\nexport default function() {};`,
output: null,
options: [{ 'count': 2, exactCount: true }],
errors: [ {
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
} ],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';export default function() {};`,
output: `import foo from 'foo';\n\nexport default function() {};`,
options: [{ 'count': 1, exactCount: true }],
errors: [ {
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE,
} ],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `const foo = require('foo');\n\n\n\nconst bar = function() {};`,
output: null,
options: [{ 'count': 2, exactCount: true }],
errors: [ {
line: 1,
column: 1,
message: 'Expected 2 empty lines after require statement not followed by another require.',
} ],
parserOptions: { ecmaVersion: 2015},
},
],
})

0 comments on commit 847066c

Please sign in to comment.