Skip to content

Commit

Permalink
[Fix] order: Require with member expression could not be fixed if alp…
Browse files Browse the repository at this point in the history
…habetize.order was used
  • Loading branch information
msvab committed Jul 7, 2022
1 parent 98c0f05 commit 0bf04be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
29 changes: 20 additions & 9 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,35 @@ function findStartOfLineWithComments(sourceCode, node) {
return result;
}

function isPlainRequireModule(node) {
function isRequireExpression(expr) {
return expr != null &&
expr.type === 'CallExpression' &&
expr.callee != null &&
expr.callee.name === 'require' &&
expr.arguments != null &&
expr.arguments.length === 1 &&
expr.arguments[0].type === 'Literal';
}

function isSupportedRequireModule(node) {
if (node.type !== 'VariableDeclaration') {
return false;
}
if (node.declarations.length !== 1) {
return false;
}
const decl = node.declarations[0];
const result = decl.id &&
const isPlainRequire = decl.id &&
(decl.id.type === 'Identifier' || decl.id.type === 'ObjectPattern') &&
isRequireExpression(decl.init);
const isRequireWithMemberExpression = decl.id &&
(decl.id.type === 'Identifier' || decl.id.type === 'ObjectPattern') &&
decl.init != null &&
decl.init.type === 'CallExpression' &&
decl.init.callee != null &&
decl.init.callee.name === 'require' &&
decl.init.arguments != null &&
decl.init.arguments.length === 1 &&
decl.init.arguments[0].type === 'Literal';
return result;
decl.init.callee.type === 'MemberExpression' &&
isRequireExpression(decl.init.callee.object);
return isPlainRequire || isRequireWithMemberExpression;
}

function isPlainImportModule(node) {
Expand All @@ -158,7 +169,7 @@ function isPlainImportEquals(node) {
}

function canCrossNodeWhileReorder(node) {
return isPlainRequireModule(node) || isPlainImportModule(node) || isPlainImportEquals(node);
return isSupportedRequireModule(node) || isPlainImportModule(node) || isPlainImportEquals(node);
}

function canReorderItems(firstNode, secondNode) {
Expand Down Expand Up @@ -276,7 +287,7 @@ function getSorter(ascending) {
result = a < b ? -1 : 1;
}
}

return result * multiplier;
};
}
Expand Down
27 changes: 22 additions & 5 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,13 @@ ruleTester.run('order', rule, {
import express from 'express';
import service from '@/api/service';
import fooParent from '../foo';
import fooSibling from './foo';
import index from './';
import internalDoesNotExistSoIsUnknown from '@/does-not-exist';
`,
options: [
Expand Down Expand Up @@ -2289,7 +2289,7 @@ ruleTester.run('order', rule, {
import b from "foo-bar";
`,
errors: [{
message: '`foo-bar` import should occur after import of `foo/barfoo`',
message: '`foo-bar` import should occur after import of `foo/barfoo`',
}],
}),
// Option alphabetize {order: 'asc': caseInsensitive: true}
Expand Down Expand Up @@ -2336,6 +2336,23 @@ ruleTester.run('order', rule, {
message: '`foo` import should occur before import of `Bar`',
}],
}),
// Option alphabetize {order: 'asc'} and require with member expression
test({
code: `
const b = require('./b').get();
const a = require('./a');
`,
output: `
const a = require('./a');
const b = require('./b').get();
`,
options: [{
alphabetize: { order: 'asc' },
}],
errors: [{
message: '`./a` import should occur before import of `./b`',
}],
}),
// Alphabetize with parent paths
test({
code: `
Expand Down

0 comments on commit 0bf04be

Please sign in to comment.