Skip to content

Commit

Permalink
Autofixer for newline-after-import. Fixes import-js#686
Browse files Browse the repository at this point in the history
  • Loading branch information
eelyafi committed Dec 21, 2016
1 parent e26e898 commit 3e991bf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ node_modules
# generated output
lib/
**/.nyc_output/

# IDE files
.idea
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 an empty line 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
18 changes: 12 additions & 6 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,29 @@ function isClassWithDecorator(node) {
return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length
}

const EXPECTED_LINE_DIFFERENCE = 2;

module.exports = {
meta: {
docs: {},
fixable: "code",
},
create: function (context) {
let level = 0
const requireCalls = []
let level = 0;
const requireCalls = [];

function checkForNewLine(node, nextNode, type) {
if (isClassWithDecorator(nextNode)) {
nextNode = nextNode.decorators[0]
nextNode = nextNode.decorators[0];
}

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

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

if (node.loc.start.line !== node.loc.end.line) {
column = 0
column = 0;
}

context.report({
Expand All @@ -68,6 +73,7 @@ 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
15 changes: 14 additions & 1 deletion 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 All @@ -282,4 +295,4 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parser: 'babel-eslint',
},
],
})
});

0 comments on commit 3e991bf

Please sign in to comment.