Skip to content

Commit

Permalink
Fix import/order fixer
Browse files Browse the repository at this point in the history
Reordering import statement to line below ignores uncrossable statements
Fix #1252
  • Loading branch information
tihonove committed Jan 3, 2019
1 parent 5a6b6c4 commit 9426cfb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ function canCrossNodeWhileReorder(node) {

function canReorderItems(firstNode, secondNode) {
const parent = firstNode.parent
const firstIndex = parent.body.indexOf(firstNode)
const secondIndex = parent.body.indexOf(secondNode)
const [firstIndex, secondIndex] = [
parent.body.indexOf(firstNode),
parent.body.indexOf(secondNode),
].sort()
const nodesBetween = parent.body.slice(firstIndex, secondIndex + 1)
for (var nodeBetween of nodesBetween) {
if (!canCrossNodeWhileReorder(nodeBetween)) {
Expand Down
84 changes: 84 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,91 @@ ruleTester.run('order', rule, {
},
],
}),
// reorder fix cannot cross function call on moving below #1
test({
code: `
const local = require('./local');
fn_call();
const global1 = require('global1');
const global2 = require('global2');
fn_call();
`,
output: `
const local = require('./local');
fn_call();
const global1 = require('global1');
const global2 = require('global2');
fn_call();
`,
errors: [{
ruleId: 'order',
message: '`./local` import should occur after import of `global2`',
}],
}),
// reorder fix cannot cross function call on moving below #2
test({
code: `
const local = require('./local');
fn_call();
const global1 = require('global1');
const global2 = require('global2');
fn_call();
`,
output: `
const local = require('./local');
fn_call();
const global1 = require('global1');
const global2 = require('global2');
fn_call();
`,
errors: [{
ruleId: 'order',
message: '`./local` import should occur after import of `global2`',
}],
}),
// reorder fix cannot cross function call on moving below #3
test({
code: `
const local1 = require('./local1');
const local2 = require('./local2');
const local3 = require('./local3');
const local4 = require('./local4');
fn_call();
const global1 = require('global1');
const global2 = require('global2');
const global3 = require('global3');
const global4 = require('global4');
const global5 = require('global5');
fn_call();
`,
output: `
const local1 = require('./local1');
const local2 = require('./local2');
const local3 = require('./local3');
const local4 = require('./local4');
fn_call();
const global1 = require('global1');
const global2 = require('global2');
const global3 = require('global3');
const global4 = require('global4');
const global5 = require('global5');
fn_call();
`,
errors: [
'`./local1` import should occur after import of `global5`',
'`./local2` import should occur after import of `global5`',
'`./local3` import should occur after import of `global5`',
'`./local4` import should occur after import of `global5`',
],
}),
// reorder fix cannot cross non import or require
test(withoutAutofixOutput({
code: `
Expand Down

0 comments on commit 9426cfb

Please sign in to comment.