From e73696a1e234032282fd58c73a4552a9b024879c Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 21 Dec 2016 22:44:25 +0100 Subject: [PATCH] (#686): Add autofixer for newline-after-import --- src/rules/newline-after-import.js | 9 ++++++++- tests/src/rules/newline-after-import.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/rules/newline-after-import.js b/src/rules/newline-after-import.js index 3198a9579..69bda294f 100644 --- a/src/rules/newline-after-import.js +++ b/src/rules/newline-after-import.js @@ -45,6 +45,7 @@ function isClassWithDecorator(node) { module.exports = { meta: { docs: {}, + fixable: 'whitespace', }, create: function (context) { let level = 0 @@ -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) { @@ -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') + }, }) } } diff --git a/tests/src/rules/newline-after-import.js b/tests/src/rules/newline-after-import.js index afd942ef0..bda2742d8 100644 --- a/tests/src/rules/newline-after-import.js +++ b/tests/src/rules/newline-after-import.js @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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,