Skip to content

Commit

Permalink
Ignore non-module-level requires
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed Nov 11, 2020
1 parent 2a08ea0 commit 6cfd55e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
40 changes: 16 additions & 24 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,21 @@ function registerNode(context, importEntry, ranks, imported, excludedImportTypes
}
}

function isInVariableDeclarator(node) {
return node &&
(node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent))
function isModuleLevelRequire(node) {
let n = node
// Handle cases like `const baz = require('foo').bar.baz`
// and `const foo = require('foo')()`
while (
(n.parent.type === 'MemberExpression' && n.parent.object === n) ||
(n.parent.type === 'CallExpression' && n.parent.callee === n)
) {
n = n.parent
}
return (
n.parent.type === 'VariableDeclarator' &&
n.parent.parent.type === 'VariableDeclaration' &&
n.parent.parent.parent.type === 'Program'
)
}

const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index', 'object']
Expand Down Expand Up @@ -583,14 +595,6 @@ module.exports = {
}
}
let imported = []
let level = 0

function incrementLevel() {
level++
}
function decrementLevel() {
level--
}

return {
ImportDeclaration: function handleImports(node) {
Expand Down Expand Up @@ -641,7 +645,7 @@ module.exports = {
)
},
CallExpression: function handleRequires(node) {
if (level !== 0 || !isStaticRequire(node) || !isInVariableDeclarator(node.parent)) {
if (!isStaticRequire(node) || !isModuleLevelRequire(node)) {
return
}
const name = node.arguments[0].value
Expand Down Expand Up @@ -671,18 +675,6 @@ module.exports = {

imported = []
},
FunctionDeclaration: incrementLevel,
FunctionExpression: incrementLevel,
ArrowFunctionExpression: incrementLevel,
BlockStatement: incrementLevel,
ObjectExpression: incrementLevel,
TemplateLiteral: incrementLevel,
'FunctionDeclaration:exit': decrementLevel,
'FunctionExpression:exit': decrementLevel,
'ArrowFunctionExpression:exit': decrementLevel,
'BlockStatement:exit': decrementLevel,
'ObjectExpression:exit': decrementLevel,
'TemplateLiteral:exit': decrementLevel,
}
},
}
10 changes: 9 additions & 1 deletion tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ruleTester.run('order', rule, {
var result = add(1, 2);
var _ = require('lodash');`,
}),
// Ignore requires that are not at the top-level
// Ignore requires that are not at the top-level #1
test({
code: `
var index = require('./');
Expand All @@ -86,6 +86,14 @@ ruleTester.run('order', rule, {
require('fs');
}`,
}),
// Ignore requires that are not at the top-level #2
test({
code: `
const foo = [
require('./foo'),
require('fs'),
]`,
}),
// Ignore requires in template literal (#1936)
test({
code: "const foo = `${require('./a')} ${require('fs')}`",
Expand Down

0 comments on commit 6cfd55e

Please sign in to comment.