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

A lot of improvements #2

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"babel-plugin-istanbul": "^2.0.1",
"babel-preset-es2015-argon": "latest",
"babel-register": "6.16.3",
"eslint": "3.x",
"eslint": "4.x",
"eslint-import-resolver-node": "file:./resolvers/node",
"eslint-import-resolver-webpack": "file:./resolvers/webpack",
"eslint-module-utils": "file:./utils",
Expand All @@ -52,12 +52,12 @@
"nyc": "^8.3.0"
},
"peerDependencies": {
"eslint": "2.x - 3.x"
"eslint": "2.x - 4.x"
},
"dependencies": {
"builtin-modules": "^1.1.1",
"eslint-import-resolver-node": "^0.2.0",
"eslint-module-utils": "^2.0.0",
"eslint-import-resolver-node": "^0.3.1",
"eslint-module-utils": "^2.1.1",
"gulp-watch": "^4.3.11",
"jscodeshift": "^0.3.30",
"lodash.cond": "^4.3.0"
Expand Down
86 changes: 34 additions & 52 deletions src/rules/order.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
'use strict'

require('./../core/add-types');

import { EOL } from 'os';
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'
import parser from 'babel-eslint';

import jscodeshift from 'jscodeshift';
const j = jscodeshift.withParser(parser);

jscodeshift.types.Type.def('ExperimentalSpreadProperty').bases('Node');
jscodeshift.types.Type.def('ExperimentalRestProperty').bases('Node');

jscodeshift.types.finalize();

const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index']

// REPORTING
Expand Down Expand Up @@ -44,54 +35,46 @@ function findOutOfOrder(imported) {
}


function findRootNode(j, root, node) {
let result = null;

root
.find(j.Node)
.filter((p) => p.node === node).forEach(p => {
let parent = p;

while (parent.parent != null && parent.parent.value.body == null) {
parent = parent.parent;
}
function findRootNode(node) {
let result = node;

result = parent;
});
while (result.parent != null && result.parent.body == null) {
result = result.parent;
}

return result;
}

function fixOutOfOrder(context, firstNode, secondNode, order) {
const sourceCode = context.getSourceCode();
const root = j(sourceCode.ast);

const firstRoot = findRootNode(j, root, firstNode.node);
const secondRoot = findRootNode(j, root, secondNode.node);
const newCode = sourceCode.getText(secondRoot.node);
const firstRoot = findRootNode(firstNode.node);
const secondRoot = findRootNode(secondNode.node);
const beforeToken = sourceCode.getTokenBefore(secondRoot);
const newCode = sourceCode.getText(secondRoot);

const msg = () => `\`${secondNode.name}\` import should occur ${order}` +
` import \`${firstNode.name}\``;
const msg = () => '`' + secondNode.name + '` import should occur ' + order +
' import of `' + firstNode.name + '`';

if (order === 'before') {
context.report({
node: secondNode.node,
message: msg(),
fix: fixer => fixer.insertTextBefore(firstRoot.node, newCode)
fix: fixer => [
fixer.remove(secondRoot),
fixer.insertTextBefore(firstRoot, newCode),
],
});
} else if (order === 'after') {
context.report({
node: secondNode.node,
message: msg(),
fix: fixer => fixer.insertTextAfter(firstRoot.node, newCode)
fix: fixer => [
fixer.remove(secondRoot),
fixer.insertTextAfter(firstRoot, newCode),
]
});
}

context.report({
node: secondNode.node,
message: msg(),
fix: fixer => fixer.remove(secondRoot.node)
});
}

function reportOutOfOrder(context, imported, outOfOrder, order) {
Expand Down Expand Up @@ -171,20 +154,16 @@ function convertGroupsToRanks(groups) {
}

function fixNewLineAfterImport(context, previousImport) {
const root = j(context.getSourceCode().ast);

const prevRoot = findRootNode(j, root, previousImport.node);
const prevRoot = findRootNode(previousImport.node);

return (fixer) => fixer.insertTextAfter(prevRoot.node, EOL);
return (fixer) => fixer.insertTextAfter(prevRoot, EOL);
}

function removeNewLineAfterImport(context, currentImport, previousImport) {
const root = j(context.getSourceCode().ast);
const prevRoot = findRootNode(previousImport.node);
const currRoot = findRootNode(currentImport.node);

const prevRoot = findRootNode(j, root, previousImport.node);
const currRoot = findRootNode(j, root, currentImport.node);

return (fixer) => fixer.removeRange([prevRoot.node.range[1] + 1, currRoot.node.range[0]]);
return (fixer) => fixer.removeRange([prevRoot.range[1] + 1, currRoot.range[0]]);
}

function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) {
Expand All @@ -196,26 +175,29 @@ function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) {

return linesBetweenImports.filter((line) => !line.trim().length).length
}

let previousImport = imported[0]

imported.slice(1).forEach(function(currentImport) {
const emptyLinesCount = getNumberOfEmptyLinesBetween(currentImport, previousImport);
if (newlinesBetweenImports === 'always') {
if (currentImport.rank !== previousImport.rank && emptyLinesCount === 0) {
const emptyLinesBetween = getNumberOfEmptyLinesBetween(currentImport, previousImport)

if (newlinesBetweenImports === 'always'
|| newlinesBetweenImports === 'always-and-inside-groups') {
if (currentImport.rank !== previousImport.rank && emptyLinesBetween === 0) {
context.report({
node: previousImport.node,
message: 'There should be at least one empty line between import groups',
fix: fixNewLineAfterImport(context, previousImport)
});
} else if (currentImport.rank === previousImport.rank && emptyLinesCount > 0) {
} else if (currentImport.rank === previousImport.rank
&& emptyLinesBetween > 0
&& newlinesBetweenImports !== 'always-and-inside-groups') {
context.report({
node: previousImport.node,
message: 'There should be no empty line within import group',
fix: removeNewLineAfterImport(context, currentImport, previousImport)
});
}
} else if (emptyLinesCount > 0) {
} else if (emptyLinesBetween > 0) {
context.report({
node: previousImport.node,
message: 'There should be no empty line between import groups',
Expand All @@ -239,7 +221,7 @@ module.exports = {
type: 'array',
},
'newlines-between': {
enum: [ 'ignore', 'always', 'never' ],
enum: [ 'ignore', 'always', 'never', 'always-and-inside-groups' ],
},
},
additionalProperties: false,
Expand Down
Loading