Skip to content

Commit

Permalink
(#686): Add autofixer for newline-after-import
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-vilchik committed Dec 21, 2016
1 parent e26e898 commit e73696a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function isClassWithDecorator(node) {
module.exports = {
meta: {
docs: {},
fixable: 'whitespace',
},
create: function (context) {
let level = 0
Expand All @@ -55,7 +56,8 @@ module.exports = {
nextNode = nextNode.decorators[0]
}

if (getLineDifference(node, nextNode) < 2) {
const lineDiff = getLineDifference(node, nextNode)
if (lineDiff < 2) {
let column = node.loc.start.column

if (node.loc.start.line !== node.loc.end.line) {
Expand All @@ -68,6 +70,11 @@ module.exports = {
column,
},
message: `Expected empty line after ${type} statement not followed by another ${type}.`,
fix(fixer) {
// add 1 blank line,
// or 2, if the nodes are on the same line
return fixer.insertTextAfter(node, lineDiff === 1 ? '\n' : '\n\n')
},
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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 @@ -155,6 +156,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 @@ -164,6 +166,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 @@ -179,6 +182,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 @@ -194,6 +198,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 @@ -209,6 +214,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 @@ -217,6 +223,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 @@ -225,6 +232,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 @@ -235,6 +243,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 @@ -245,6 +254,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 @@ -254,6 +264,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 @@ -263,6 +274,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 @@ -273,6 +285,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 Down

0 comments on commit e73696a

Please sign in to comment.