Skip to content

Commit 5ff9700

Browse files
committed
Optimized canReorderItems
1 parent 0e0d34a commit 5ff9700

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/rules/order-imports.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,13 @@ function canCrossNodeWhileReorder(node: NodeOrToken): boolean {
191191
function canReorderItems(firstNode: NodeOrToken, secondNode: NodeOrToken): boolean {
192192
const parent = firstNode.parent;
193193
const firstIndex = parent.body.indexOf(firstNode);
194-
const secondIndex = parent.body.indexOf(secondNode);
195-
const nodesBetween = parent.body.slice(firstIndex, secondIndex + 1);
196-
for (const nodeBetween of nodesBetween) {
197-
if (!canCrossNodeWhileReorder(nodeBetween)) {
194+
const secondIndex = parent.body.indexOf(secondNode, firstIndex);
195+
if (firstIndex === -1 || secondIndex === -1) {
196+
return false;
197+
}
198+
199+
for (let i = firstIndex; i <= secondIndex; i++) {
200+
if (!canCrossNodeWhileReorder(parent.body[i])) {
198201
return false;
199202
}
200203
}

0 commit comments

Comments
 (0)