Skip to content

Commit 957092a

Browse files
golopotljharb
authored andcommitted
[Fix] order: ignore non-module-level requires
Fixes #1936.
1 parent 6cd1fe1 commit 957092a

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88

99
### Fixed
1010
- [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella])
11+
- [`order`]: ignore non-module-level requires ([#1940], thanks [@golopot])
1112

1213
## [2.22.1] - 2020-09-27
1314
### Fixed
@@ -735,6 +736,7 @@ for info on changes for earlier releases.
735736

736737
[`memo-parser`]: ./memo-parser/README.md
737738

739+
[#1940]: https://github.com/benmosher/eslint-plugin-import/pull/1940
738740
[#1889]: https://github.com/benmosher/eslint-plugin-import/pull/1889
739741
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
740742
[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854

src/rules/order.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,21 @@ function registerNode(context, importEntry, ranks, imported, excludedImportTypes
333333
}
334334
}
335335

336-
function isInVariableDeclarator(node) {
337-
return node &&
338-
(node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent))
336+
function isModuleLevelRequire(node) {
337+
let n = node
338+
// Handle cases like `const baz = require('foo').bar.baz`
339+
// and `const foo = require('foo')()`
340+
while (
341+
(n.parent.type === 'MemberExpression' && n.parent.object === n) ||
342+
(n.parent.type === 'CallExpression' && n.parent.callee === n)
343+
) {
344+
n = n.parent
345+
}
346+
return (
347+
n.parent.type === 'VariableDeclarator' &&
348+
n.parent.parent.type === 'VariableDeclaration' &&
349+
n.parent.parent.parent.type === 'Program'
350+
)
339351
}
340352

341353
const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index', 'object']
@@ -583,14 +595,6 @@ module.exports = {
583595
}
584596
}
585597
let imported = []
586-
let level = 0
587-
588-
function incrementLevel() {
589-
level++
590-
}
591-
function decrementLevel() {
592-
level--
593-
}
594598

595599
return {
596600
ImportDeclaration: function handleImports(node) {
@@ -641,7 +645,7 @@ module.exports = {
641645
)
642646
},
643647
CallExpression: function handleRequires(node) {
644-
if (level !== 0 || !isStaticRequire(node) || !isInVariableDeclarator(node.parent)) {
648+
if (!isStaticRequire(node) || !isModuleLevelRequire(node)) {
645649
return
646650
}
647651
const name = node.arguments[0].value
@@ -671,16 +675,6 @@ module.exports = {
671675

672676
imported = []
673677
},
674-
FunctionDeclaration: incrementLevel,
675-
FunctionExpression: incrementLevel,
676-
ArrowFunctionExpression: incrementLevel,
677-
BlockStatement: incrementLevel,
678-
ObjectExpression: incrementLevel,
679-
'FunctionDeclaration:exit': decrementLevel,
680-
'FunctionExpression:exit': decrementLevel,
681-
'ArrowFunctionExpression:exit': decrementLevel,
682-
'BlockStatement:exit': decrementLevel,
683-
'ObjectExpression:exit': decrementLevel,
684678
}
685679
},
686680
}

tests/src/rules/order.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ruleTester.run('order', rule, {
7474
var result = add(1, 2);
7575
var _ = require('lodash');`,
7676
}),
77-
// Ignore requires that are not at the top-level
77+
// Ignore requires that are not at the top-level #1
7878
test({
7979
code: `
8080
var index = require('./');
@@ -86,6 +86,18 @@ ruleTester.run('order', rule, {
8686
require('fs');
8787
}`,
8888
}),
89+
// Ignore requires that are not at the top-level #2
90+
test({
91+
code: `
92+
const foo = [
93+
require('./foo'),
94+
require('fs'),
95+
]`,
96+
}),
97+
// Ignore requires in template literal (#1936)
98+
test({
99+
code: "const foo = `${require('./a')} ${require('fs')}`",
100+
}),
89101
// Ignore unknown/invalid cases
90102
test({
91103
code: `

0 commit comments

Comments
 (0)