Skip to content

Commit

Permalink
chore(NA): use noext on micromatch
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic committed Jun 20, 2024
1 parent 2c608d7 commit b1b43c9
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -15,14 +43,15 @@ 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);
}
return src;
}

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);

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b1b43c9

Please sign in to comment.