From b1b43c93066ee7947152c93c2e2b6d5353cace2f Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Thu, 20 Jun 2024 01:46:23 +0100 Subject: [PATCH] chore(NA): use noext on micromatch --- .../rules/no_restricted_paths.js | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js index a5645873f2678..9fe3fd10d984e 100644 --- a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js +++ b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js @@ -1,6 +1,34 @@ +/* eslint-disable-line @kbn/eslint/require-license-header */ +/* + * This product uses import/no-restricted-paths which is available under a + * "MIT" license. + * + * The MIT License (MIT) + * + * Copyright (c) 2015-present, Ben Mosher + * https://github.com/benmosher/eslint-plugin-import + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ const path = require('path'); const resolve = require('eslint-module-utils/resolve').default; -const { match } = require('micromatch'); +const mm = require('micromatch'); function isStaticRequire(node) { return ( @@ -15,7 +43,7 @@ function isStaticRequire(node) { } function traverseToTopFolder(src, pattern) { - while (match([src], pattern, { dot: true }).length > 0) { + while (mm([src], pattern, { noext: true }).length > 0) { const srcIdx = src.lastIndexOf(path.sep); src = src.slice(0, srcIdx); } @@ -23,6 +51,7 @@ function traverseToTopFolder(src, pattern) { } function isSameFolderOrDescendent(src, imported, pattern) { + // to allow to exclude file by name in pattern (e.g., !**/index*) we start with file dirname and then traverse const srcFileFolderRoot = traverseToTopFolder(path.dirname(src), pattern); const importedFileFolderRoot = traverseToTopFolder(path.dirname(imported), pattern); @@ -80,16 +109,12 @@ module.exports = { ? path.relative(basePath, absoluteImportPath) : importPath; - const isTargetMatch = match([relativeSrcFile], target, { dot: true }).length > 0; - const isFromMatch = match([relativeImportFile], from, { dot: true }).length > 0; - if ( - !isTargetMatch || - !isFromMatch || + !mm([relativeSrcFile], target, { noext: true }).length || + !mm([relativeImportFile], from, { noext: true }).length || (allowSameFolder && isSameFolderOrDescendent(relativeSrcFile, relativeImportFile, from)) - ) { + ) continue; - } context.report({ node,