From 1365683c23c1e4302331b98fb26ff32bc6317462 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Fri, 10 May 2019 21:47:01 -0400 Subject: [PATCH] tools: allow RegExp in required-modules eslint rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/27647 Reviewed-By: Rich Trott Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater --- test/.eslintrc.yaml | 4 ++- test/parallel/test-eslint-required-modules.js | 22 ++++++++++-- tools/eslint-rules/required-modules.js | 36 +++++++++---------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index 8eee5f39958dbe..8a36ac522acda4 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -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 diff --git a/test/parallel/test-eslint-required-modules.js b/test/parallel/test-eslint-required-modules.js index 6b2b0d95028d73..e81a7d4b241353 100644 --- a/test/parallel/test-eslint-required-modules.js +++ b/test/parallel/test-eslint-required-modules.js @@ -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.' }] } ] diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index 64b3d748afa630..9b895c90033c60 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -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 = []; @@ -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; } /** @@ -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.', @@ -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' + }, + }], };