Skip to content

Commit

Permalink
[New] order: add fixer for destructuring commonjs import
Browse files Browse the repository at this point in the history
fixes #1337
  • Loading branch information
golopot authored and ljharb committed May 29, 2019
1 parent 9a0455e commit 15e5c61
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ function isPlainRequireModule(node) {
return false
}
const decl = node.declarations[0]
const result = (decl.id != null && decl.id.type === 'Identifier') &&
const result = decl.id &&
(decl.id.type === 'Identifier' || decl.id.type === 'ObjectPattern') &&
decl.init != null &&
decl.init.type === 'CallExpression' &&
decl.init.callee != null &&
Expand Down
15 changes: 15 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,21 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur before import of `async`',
}],
}),
// fix destructured commonjs import
test({
code: `
var {b} = require('async');
var {a} = require('fs');
`,
output: `
var {a} = require('fs');
var {b} = require('async');
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
}),
// fix order of multile import
test({
code: `
Expand Down

0 comments on commit 15e5c61

Please sign in to comment.