Skip to content

Commit

Permalink
[New] Autofixer for newline-after-import
Browse files Browse the repository at this point in the history
Fixes #686
  • Loading branch information
eelyafi authored and ljharb committed Dec 21, 2016
1 parent 3f9e4bf commit bfdf77a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/rules/newline-after-import.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# newline-after-import

Enforces having one or more empty lines after the last top-level import statement or require call.
+(fixable) The `--fix` option on the [command line] automatically fixes problems reported by this rule.

## Rule Details

Expand Down
10 changes: 9 additions & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
'additionalProperties': false,
},
],
fixable: 'whitespace',
},
create: function (context) {
let level = 0
Expand All @@ -68,7 +69,10 @@ module.exports = {
}

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

if (lineDifference < EXPECTED_LINE_DIFFERENCE) {
let column = node.loc.start.column

if (node.loc.start.line !== node.loc.end.line) {
Expand All @@ -81,6 +85,10 @@ module.exports = {
column,
},
message: `Expected empty line after ${type} statement not followed by another ${type}.`,
fix: fixer => fixer.insertTextAfter(
node,
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference)
),
})
}
}
Expand Down
27 changes: 26 additions & 1 deletion tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
invalid: [
{
code: `import foo from 'foo';\nexport default function() {};`,
output: `import foo from 'foo';\n\nexport default function() {};`,
errors: [ {
line: 1,
column: 1,
Expand All @@ -175,6 +176,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `import foo from 'foo';\n\nexport default function() {};`,
output: `import foo from 'foo';\n\n\nexport default function() {};`,
options: [{ 'count': 2 }],
errors: [ {
line: 1,
Expand All @@ -183,8 +185,19 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
} ],
parserOptions: { sourceType: 'module' },
},
{
code: `var foo = require('foo-module');\nvar something = 123;`,
output: `var foo = require('foo-module');\n\nvar something = 123;`,
errors: [ {
line: 1,
column: 1,
message: REQUIRE_ERROR_MESSAGE,
} ],
parserOptions: { sourceType: 'module' },
},
{
code: `import foo from 'foo';\nexport default function() {};`,
output: `import foo from 'foo';\n\nexport default function() {};`,
options: [{ 'count': 1 }],
errors: [ {
line: 1,
Expand All @@ -195,6 +208,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `var foo = require('foo-module');\nvar something = 123;`,
output: `var foo = require('foo-module');\n\nvar something = 123;`,
errors: [ {
line: 1,
column: 1,
Expand All @@ -204,6 +218,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `import foo from 'foo';\nvar a = 123;\n\nimport { bar } from './bar-lib';\nvar b=456;`,
output: `import foo from 'foo';\n\nvar a = 123;\n\nimport { bar } from './bar-lib';\n\nvar b=456;`,
errors: [
{
line: 1,
Expand All @@ -219,6 +234,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `var foo = require('foo-module');\nvar a = 123;\n\nvar bar = require('bar-lib');\nvar b=456;`,
output: `var foo = require('foo-module');\n\nvar a = 123;\n\nvar bar = require('bar-lib');\n\nvar b=456;`,
errors: [
{
line: 1,
Expand All @@ -234,6 +250,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `var foo = require('foo-module');\nvar a = 123;\n\nrequire('bar-lib');\nvar b=456;`,
output: `var foo = require('foo-module');\n\nvar a = 123;\n\nrequire('bar-lib');\n\nvar b=456;`,
errors: [
{
line: 1,
Expand All @@ -249,6 +266,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `var path = require('path');\nvar foo = require('foo');\nvar bar = 42;`,
output: `var path = require('path');\nvar foo = require('foo');\n\nvar bar = 42;`,
errors: [ {
line: 2,
column: 1,
Expand All @@ -257,6 +275,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `var assign = Object.assign || require('object-assign');\nvar foo = require('foo');\nvar bar = 42;`,
output: `var assign = Object.assign || require('object-assign');\nvar foo = require('foo');\n\nvar bar = 42;`,
errors: [ {
line: 2,
column: 1,
Expand All @@ -265,6 +284,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `require('a');\nfoo(require('b'), require('c'), require('d'));\nrequire('d');\nvar foo = 'bar';`,
output: `require('a');\nfoo(require('b'), require('c'), require('d'));\nrequire('d');\n\nvar foo = 'bar';`,
errors: [
{
line: 3,
Expand All @@ -275,6 +295,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `require('a');\nfoo(\nrequire('b'),\nrequire('c'),\nrequire('d')\n);\nvar foo = 'bar';`,
output: `require('a');\nfoo(\nrequire('b'),\nrequire('c'),\nrequire('d')\n);\n\nvar foo = 'bar';`,
errors: [
{
line: 6,
Expand All @@ -285,6 +306,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `import path from 'path';\nimport foo from 'foo';\nvar bar = 42;`,
output: `import path from 'path';\nimport foo from 'foo';\n\nvar bar = 42;`,
errors: [ {
line: 2,
column: 1,
Expand All @@ -294,6 +316,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `import path from 'path';import foo from 'foo';var bar = 42;`,
output: `import path from 'path';import foo from 'foo';\n\nvar bar = 42;`,
errors: [ {
line: 1,
column: 25,
Expand All @@ -303,6 +326,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `import foo from 'foo';\n@SomeDecorator(foo)\nclass Foo {}`,
output: `import foo from 'foo';\n\n@SomeDecorator(foo)\nclass Foo {}`,
errors: [ {
line: 1,
column: 1,
Expand All @@ -313,6 +337,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
},
{
code: `var foo = require('foo');\n@SomeDecorator(foo)\nclass Foo {}`,
output: `var foo = require('foo');\n\n@SomeDecorator(foo)\nclass Foo {}`,
errors: [ {
line: 1,
column: 1,
Expand All @@ -322,4 +347,4 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parser: 'babel-eslint',
},
],
})
});

0 comments on commit bfdf77a

Please sign in to comment.