Skip to content

Commit

Permalink
tools: allow RegExp in required-modules eslint rule
Browse files Browse the repository at this point in the history
PR-URL: #27647
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
richardlau authored and targos committed May 14, 2019
1 parent 140b44f commit 1365683
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
4 changes: 3 additions & 1 deletion test/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ rules:
node-core/inspector-check: error
node-core/number-isnan: error
## common module is mandatory in tests
node-core/required-modules: [error, common]
node-core/required-modules:
- error
- common: 'common(/index\.(m)?js)?$'
node-core/require-common-first: error
node-core/no-duplicate-requires: off

Expand Down
22 changes: 19 additions & 3 deletions test/parallel/test-eslint-required-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,38 @@ new RuleTester().run('required-modules', rule, {
valid: [
{
code: 'require("common")',
options: ['common']
options: [{ common: 'common' }]
},
{
code: 'foo',
options: []
},
{
code: 'require("common")',
options: [{ common: 'common(/index\\.(m)?js)?$' }]
},
{
code: 'require("common/index.js")',
options: [{ common: 'common(/index\\.(m)?js)?$' }]
},
],
invalid: [
{
code: 'foo',
options: ['common'],
options: [{ common: 'common' }],
errors: [{ message: 'Mandatory module "common" must be loaded.' }]
},
{
code: 'require("common/fixtures.js")',
options: [{ common: 'common(/index\\.(m)?js)?$' }],
errors: [{
message:
'Mandatory module "common" must be loaded.'
}]
},
{
code: 'require("somethingElse")',
options: ['common'],
options: [{ common: 'common' }],
errors: [{ message: 'Mandatory module "common" must be loaded.' }]
}
]
Expand Down
36 changes: 17 additions & 19 deletions tools/eslint-rules/required-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
*/
'use strict';

const path = require('path');

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
// Trim required module names
const requiredModules = context.options;
const options = context.options[0];
const requiredModules = options ? Object.keys(options).map((x) => {
return [ x, new RegExp(options[x]) ];
}) : [];
const isESM = context.parserOptions.sourceType === 'module';

const foundModules = [];
Expand Down Expand Up @@ -46,14 +47,10 @@ module.exports = function(context) {
* @returns {undefined|String} required module name or undefined
*/
function getRequiredModuleName(str) {
if (str === '../common/index.mjs') {
return 'common';
}

const value = path.basename(str);

// Check if value is in required modules array
return requiredModules.indexOf(value) !== -1 ? value : undefined;
const match = requiredModules.find(([, test]) => {
return test.test(str);
});
return match ? match[0] : undefined;
}

/**
Expand All @@ -75,9 +72,9 @@ module.exports = function(context) {
'Program:exit'(node) {
if (foundModules.length < requiredModules.length) {
const missingModules = requiredModules.filter(
(module) => foundModules.indexOf(module) === -1
([module]) => foundModules.indexOf(module) === -1
);
missingModules.forEach((moduleName) => {
missingModules.forEach(([moduleName]) => {
context.report(
node,
'Mandatory module "{{moduleName}}" must be loaded.',
Expand Down Expand Up @@ -110,10 +107,11 @@ module.exports = function(context) {
return rules;
};

module.exports.schema = {
'type': 'array',
'additionalItems': {
'type': 'string'
},
'uniqueItems': true
module.exports.meta = {
schema: [{
'type': 'object',
'additionalProperties': {
'type': 'string'
},
}],
};

0 comments on commit 1365683

Please sign in to comment.