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

Autofixer for newline-after-import. Fixes #686 #696

Merged
merged 1 commit into from
Jun 20, 2017
Merged
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 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',
},
],
})
});