eslint-plugin-sort-imports-es6-autofix
Advanced tools
Comparing version 0.2.2 to 0.3.0
{ | ||
"name": "eslint-plugin-sort-imports-es6-autofix", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "A sort-imports rule that properly distinguishes between ES6 import types.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -16,2 +16,4 @@ # eslint-plugin-sort-imports-es6-autofix | ||
This rule respects whitespace and comments between imports by only looking at the order of (and sorting) consecutive import statements (those without newlines/comments in between them). | ||
This fork also fixes the import order on eslint --fix. | ||
@@ -18,0 +20,0 @@ To avoid problems, it will only switch out the import statements, not comments on the same line, etc. |
@@ -108,2 +108,12 @@ /** | ||
/** | ||
* Gets if there are lines (empty or comments) between two nodes | ||
* @param {ASTNode} firstNode - the ImportDeclaration node. | ||
* @param {ASTNode} secondNode - the ImportDeclaration node. | ||
* @returns {boolean} if there are lines between the nodes. | ||
*/ | ||
function isLineBetween(firstNode, secondNode) { | ||
return firstNode.loc.end.line < secondNode.loc.start.line - 1; | ||
} | ||
function sortAndFixAllNodes(initial, nodes) { | ||
@@ -148,25 +158,44 @@ const rich = nodes.map(node => [node, initial.substring(node.range[0], node.range[1])]); | ||
const sorted = fixed.sort((a, b) => { | ||
const currentMemberSyntaxGroupIndex = getMemberParameterGroupIndex(b[0]), | ||
currentMemberIsType = (b[0].importKind && b[0].importKind === 'type') || false, | ||
previousMemberSyntaxGroupIndex = getMemberParameterGroupIndex(a[0]), | ||
previousMemberIsType = (a[0].importKind && a[0].importKind === 'type') || false; | ||
let currentLocalMemberName = getFirstLocalMemberName(b[0]), | ||
previousLocalMemberName = getFirstLocalMemberName(a[0]); | ||
if (ignoreCase) { | ||
previousLocalMemberName = previousLocalMemberName && previousLocalMemberName.toLowerCase(); | ||
currentLocalMemberName = currentLocalMemberName && currentLocalMemberName.toLowerCase(); | ||
} | ||
if (typeSortStrategy !== "mixed" && currentMemberIsType !== previousMemberIsType) { | ||
return ((currentMemberIsType && typeSortStrategy === "before") || (previousMemberIsType && typeSortStrategy === "after")) ? 1 : -1; | ||
} if (currentMemberSyntaxGroupIndex !== previousMemberSyntaxGroupIndex) { | ||
return (currentMemberSyntaxGroupIndex < previousMemberSyntaxGroupIndex) ? 1 : -1; | ||
} else if(previousLocalMemberName && currentLocalMemberName) { | ||
return (currentLocalMemberName < previousLocalMemberName) ? 1 : -1; | ||
} | ||
return 0; | ||
}); | ||
// Group by ImportDeclarations that are consecutive (no lines inbetween) | ||
const sections = fixed.reduce((sections, current) => { | ||
const lastSection = sections[sections.length - 1]; | ||
if (lastSection.length === 0) { | ||
lastSection.push(current); | ||
} else { | ||
const lastFixed = lastSection[lastSection.length - 1]; | ||
if (isLineBetween(lastFixed[0], current[0])) { | ||
sections.push([ current ]); | ||
} else { | ||
lastSection.push(current); | ||
} | ||
} | ||
return sections; | ||
}, [[]]) | ||
// Sort each grouping | ||
const sorted = sections.map(section => { | ||
return section.sort((a, b) => { | ||
const currentMemberSyntaxGroupIndex = getMemberParameterGroupIndex(b[0]), | ||
currentMemberIsType = (b[0].importKind && b[0].importKind === 'type') || false, | ||
previousMemberSyntaxGroupIndex = getMemberParameterGroupIndex(a[0]), | ||
previousMemberIsType = (a[0].importKind && a[0].importKind === 'type') || false; | ||
let currentLocalMemberName = getFirstLocalMemberName(b[0]), | ||
previousLocalMemberName = getFirstLocalMemberName(a[0]); | ||
if (ignoreCase) { | ||
previousLocalMemberName = previousLocalMemberName && previousLocalMemberName.toLowerCase(); | ||
currentLocalMemberName = currentLocalMemberName && currentLocalMemberName.toLowerCase(); | ||
} | ||
if (typeSortStrategy !== "mixed" && currentMemberIsType !== previousMemberIsType) { | ||
return ((currentMemberIsType && typeSortStrategy === "before") || (previousMemberIsType && typeSortStrategy === "after")) ? 1 : -1; | ||
} if (currentMemberSyntaxGroupIndex !== previousMemberSyntaxGroupIndex) { | ||
return (currentMemberSyntaxGroupIndex < previousMemberSyntaxGroupIndex) ? 1 : -1; | ||
} else if(previousLocalMemberName && currentLocalMemberName) { | ||
return (currentLocalMemberName < previousLocalMemberName) ? 1 : -1; | ||
} | ||
return 0; | ||
}); | ||
}).reduce((a, c) => a.concat(c), []); // Flatten groupings | ||
return sorted.map(n => n[1]).reduce((done, current, i) => (`${done}${i !== 0 ? betweens[i - 1] : ''}${current}`), ''); | ||
} | ||
@@ -180,3 +209,3 @@ | ||
if (previousDeclaration) { | ||
if (previousDeclaration && !isLineBetween(previousDeclaration, node)) { | ||
const currentMemberSyntaxGroupIndex = getMemberParameterGroupIndex(node), | ||
@@ -193,3 +222,2 @@ currentMemberIsType = (node.importKind && node.importKind === 'type') || false, | ||
} | ||
@@ -196,0 +224,0 @@ // When the current declaration uses a different member syntax, |
18345
275
41
5