Skip to content

Commit ce037f4

Browse files
msvabljharb
authored andcommitted
[Fix] order: require with member expression could not be fixed if alphabetize.order was used
1 parent 98c0f05 commit ce037f4

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1515
- [`order`]: move nested imports closer to main import entry ([#2396], thanks [@pri1311])
1616
- [`no-restricted-paths`]: fix an error message ([#2466], thanks [@AdriAt360])
1717
- [`no-restricted-paths`]: use `Minimatch.match` instead of `minimatch` to comply with Windows Native paths ([#2466], thanks [@AdriAt360])
18+
- [`order`]: require with member expression could not be fixed if alphabetize.order was used ([#2490], thanks [@msvab])
1819

1920
### Changed
2021
- [Tests] `named`: Run all TypeScript test ([#2427], thanks [@ProdigySim])
@@ -994,6 +995,7 @@ for info on changes for earlier releases.
994995

995996
[`memo-parser`]: ./memo-parser/README.md
996997

998+
[#2490]: https://github.com/import-js/eslint-plugin-import/pull/2490
997999
[#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466
9981000
[#2440]: https://github.com/import-js/eslint-plugin-import/pull/2440
9991001
[#2427]: https://github.com/import-js/eslint-plugin-import/pull/2427
@@ -1638,15 +1640,16 @@ for info on changes for earlier releases.
16381640
[@MikeyBeLike]: https://github.com/MikeyBeLike
16391641
[@mplewis]: https://github.com/mplewis
16401642
[@mrmckeb]: https://github.com/mrmckeb
1643+
[@msvab]: https://github.com/msvab
16411644
[@mx-bernhard]: https://github.com/mx-bernhard
16421645
[@nickofthyme]: https://github.com/nickofthyme
16431646
[@nicolashenry]: https://github.com/nicolashenry
16441647
[@noelebrun]: https://github.com/noelebrun
16451648
[@ntdb]: https://github.com/ntdb
16461649
[@nwalters512]: https://github.com/nwalters512
16471650
[@ombene]: https://github.com/ombene
1648-
[@OutdatedVersion]: https://github.com/OutdatedVersion
16491651
[@ota-meshi]: https://github.com/ota-meshi
1652+
[@OutdatedVersion]: https://github.com/OutdatedVersion
16501653
[@panrafal]: https://github.com/panrafal
16511654
[@paztis]: https://github.com/paztis
16521655
[@pcorpet]: https://github.com/pcorpet

src/rules/order.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,35 @@ function findStartOfLineWithComments(sourceCode, node) {
129129
return result;
130130
}
131131

132-
function isPlainRequireModule(node) {
132+
function isRequireExpression(expr) {
133+
return expr != null &&
134+
expr.type === 'CallExpression' &&
135+
expr.callee != null &&
136+
expr.callee.name === 'require' &&
137+
expr.arguments != null &&
138+
expr.arguments.length === 1 &&
139+
expr.arguments[0].type === 'Literal';
140+
}
141+
142+
function isSupportedRequireModule(node) {
133143
if (node.type !== 'VariableDeclaration') {
134144
return false;
135145
}
136146
if (node.declarations.length !== 1) {
137147
return false;
138148
}
139149
const decl = node.declarations[0];
140-
const result = decl.id &&
150+
const isPlainRequire = decl.id &&
151+
(decl.id.type === 'Identifier' || decl.id.type === 'ObjectPattern') &&
152+
isRequireExpression(decl.init);
153+
const isRequireWithMemberExpression = decl.id &&
141154
(decl.id.type === 'Identifier' || decl.id.type === 'ObjectPattern') &&
142155
decl.init != null &&
143156
decl.init.type === 'CallExpression' &&
144157
decl.init.callee != null &&
145-
decl.init.callee.name === 'require' &&
146-
decl.init.arguments != null &&
147-
decl.init.arguments.length === 1 &&
148-
decl.init.arguments[0].type === 'Literal';
149-
return result;
158+
decl.init.callee.type === 'MemberExpression' &&
159+
isRequireExpression(decl.init.callee.object);
160+
return isPlainRequire || isRequireWithMemberExpression;
150161
}
151162

152163
function isPlainImportModule(node) {
@@ -158,7 +169,7 @@ function isPlainImportEquals(node) {
158169
}
159170

160171
function canCrossNodeWhileReorder(node) {
161-
return isPlainRequireModule(node) || isPlainImportModule(node) || isPlainImportEquals(node);
172+
return isSupportedRequireModule(node) || isPlainImportModule(node) || isPlainImportEquals(node);
162173
}
163174

164175
function canReorderItems(firstNode, secondNode) {
@@ -276,7 +287,7 @@ function getSorter(ascending) {
276287
result = a < b ? -1 : 1;
277288
}
278289
}
279-
290+
280291
return result * multiplier;
281292
};
282293
}

tests/src/rules/order.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,13 +896,13 @@ ruleTester.run('order', rule, {
896896
import express from 'express';
897897
898898
import service from '@/api/service';
899-
899+
900900
import fooParent from '../foo';
901-
901+
902902
import fooSibling from './foo';
903-
903+
904904
import index from './';
905-
905+
906906
import internalDoesNotExistSoIsUnknown from '@/does-not-exist';
907907
`,
908908
options: [
@@ -2289,7 +2289,7 @@ ruleTester.run('order', rule, {
22892289
import b from "foo-bar";
22902290
`,
22912291
errors: [{
2292-
message: '`foo-bar` import should occur after import of `foo/barfoo`',
2292+
message: '`foo-bar` import should occur after import of `foo/barfoo`',
22932293
}],
22942294
}),
22952295
// Option alphabetize {order: 'asc': caseInsensitive: true}
@@ -2336,6 +2336,23 @@ ruleTester.run('order', rule, {
23362336
message: '`foo` import should occur before import of `Bar`',
23372337
}],
23382338
}),
2339+
// Option alphabetize {order: 'asc'} and require with member expression
2340+
test({
2341+
code: `
2342+
const b = require('./b').get();
2343+
const a = require('./a');
2344+
`,
2345+
output: `
2346+
const a = require('./a');
2347+
const b = require('./b').get();
2348+
`,
2349+
options: [{
2350+
alphabetize: { order: 'asc' },
2351+
}],
2352+
errors: [{
2353+
message: '`./a` import should occur before import of `./b`',
2354+
}],
2355+
}),
23392356
// Alphabetize with parent paths
23402357
test({
23412358
code: `

0 commit comments

Comments
 (0)