From 6cfd55e3db2a9bef95ab073a31e5fefbf2c0127d Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Wed, 11 Nov 2020 13:10:25 +0800 Subject: [PATCH] Ignore non-module-level requires --- src/rules/order.js | 40 ++++++++++++++++------------------------ tests/src/rules/order.js | 10 +++++++++- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/rules/order.js b/src/rules/order.js index 4c9936f7e9..fe2376cfe1 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -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'] @@ -583,14 +595,6 @@ module.exports = { } } let imported = [] - let level = 0 - - function incrementLevel() { - level++ - } - function decrementLevel() { - level-- - } return { ImportDeclaration: function handleImports(node) { @@ -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 @@ -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, } }, } diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 5a6b5a17ed..497911ffbe 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -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('./'); @@ -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')}`",