-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add no-duplicate-requires rule
PR-URL: #21712 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jon Moss <[email protected]>
- Loading branch information
Showing
8 changed files
with
116 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
common.skipIfEslintMissing(); | ||
|
||
const { RuleTester } = require('../../tools/node_modules/eslint'); | ||
const rule = require('../../tools/eslint-rules/no-duplicate-requires'); | ||
|
||
new RuleTester().run('no-duplicate-requires', rule, { | ||
valid: [ | ||
{ | ||
code: 'require("a"); require("b"); (function() { require("a"); });', | ||
}, | ||
{ | ||
code: 'require(a); require(a);', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'require("a"); require("a");', | ||
errors: [{ message: '\'a\' require is duplicated.' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @fileoverview Ensure modules are not required twice at top level of a module | ||
* @author devsnek | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
|
||
function isString(node) { | ||
return node && node.type === 'Literal' && typeof node.value === 'string'; | ||
} | ||
|
||
function isRequireCall(node) { | ||
return node.callee.type === 'Identifier' && node.callee.name === 'require'; | ||
} | ||
|
||
function isTopLevel(node) { | ||
do { | ||
if (node.type === 'FunctionDeclaration' || | ||
node.type === 'FunctionExpression' || | ||
node.type === 'ArrowFunctionExpression' || | ||
node.type === 'ClassBody' || | ||
node.type === 'MethodDefinition') { | ||
return false; | ||
} | ||
} while (node = node.parent); | ||
return true; | ||
} | ||
|
||
module.exports = (context) => { | ||
if (context.parserOptions.sourceType === 'module') { | ||
return {}; | ||
} | ||
|
||
function getRequiredModuleNameFromCall(node) { | ||
// node has arguments and first argument is string | ||
if (node.arguments.length && isString(node.arguments[0])) { | ||
return node.arguments[0].value.trim(); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
const required = new Set(); | ||
|
||
const rules = { | ||
CallExpression: (node) => { | ||
if (isRequireCall(node) && isTopLevel(node)) { | ||
const moduleName = getRequiredModuleNameFromCall(node); | ||
if (moduleName === undefined) { | ||
return; | ||
} | ||
if (required.has(moduleName)) { | ||
context.report( | ||
node, | ||
'\'{{moduleName}}\' require is duplicated.', | ||
{ moduleName } | ||
); | ||
} else { | ||
required.add(moduleName); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
return rules; | ||
}; |