|
1 | 1 | import path from 'node:path' |
2 | 2 |
|
3 | 3 | import type { TSESTree } from '@typescript-eslint/utils' |
4 | | -import isGlob from 'is-glob' |
5 | | -import { Minimatch } from 'minimatch' |
6 | 4 |
|
7 | 5 | import type { Arrayable } from '../types' |
8 | | -import { importType, createRule, moduleVisitor, resolve } from '../utils' |
| 6 | +import { |
| 7 | + importType, |
| 8 | + createRule, |
| 9 | + moduleVisitor, |
| 10 | + resolve, |
| 11 | + isDynamicPattern, |
| 12 | + fileMatcher, |
| 13 | + isMatch, |
| 14 | +} from '../utils' |
9 | 15 |
|
10 | 16 | const containsPath = (filepath: string, target: string) => { |
11 | 17 | const relative = path.relative(target, filepath) |
12 | 18 | return relative === '' || !relative.startsWith('..') |
13 | 19 | } |
14 | 20 |
|
15 | 21 | function isMatchingTargetPath(filename: string, targetPath: string) { |
16 | | - if (isGlob(targetPath)) { |
17 | | - const mm = new Minimatch(targetPath, { windowsPathsNoEscape: true }) |
18 | | - return mm.match(filename) |
| 22 | + if (isDynamicPattern(targetPath)) { |
| 23 | + return isMatch(filename, targetPath) |
19 | 24 | } |
20 | 25 |
|
21 | 26 | return containsPath(filename, targetPath) |
@@ -171,17 +176,13 @@ export = createRule<[Options?], MessageId>({ |
171 | 176 | ) { |
172 | 177 | let isPathException: ((absoluteImportPath: string) => boolean) | undefined |
173 | 178 |
|
174 | | - const mm = new Minimatch(absoluteFrom, { windowsPathsNoEscape: true }) |
175 | | - const isPathRestricted = (absoluteImportPath: string) => |
176 | | - mm.match(absoluteImportPath) |
177 | | - const hasValidExceptions = zoneExcept.every(it => isGlob(it)) |
| 179 | + const isPathRestricted = fileMatcher(absoluteFrom) |
| 180 | + const hasValidExceptions = zoneExcept.every(it => isDynamicPattern(it)) |
178 | 181 |
|
179 | 182 | if (hasValidExceptions) { |
180 | | - const exceptionsMm = zoneExcept.map( |
181 | | - except => new Minimatch(except, { windowsPathsNoEscape: true }), |
182 | | - ) |
| 183 | + const exceptionsMm = zoneExcept.map(except => fileMatcher(except)) |
183 | 184 | isPathException = (absoluteImportPath: string) => |
184 | | - exceptionsMm.some(mm => mm.match(absoluteImportPath)) |
| 185 | + exceptionsMm.some(mm => mm(absoluteImportPath)) |
185 | 186 | } |
186 | 187 |
|
187 | 188 | const reportInvalidException = reportInvalidExceptionGlob |
@@ -258,7 +259,7 @@ export = createRule<[Options?], MessageId>({ |
258 | 259 | zoneExcept: string[] = [], |
259 | 260 | ) => { |
260 | 261 | const allZoneFrom = [zoneFrom].flat() |
261 | | - const areGlobPatterns = allZoneFrom.map(it => isGlob(it)) |
| 262 | + const areGlobPatterns = allZoneFrom.map(it => isDynamicPattern(it)) |
262 | 263 |
|
263 | 264 | if (areBothGlobPatternAndAbsolutePath(areGlobPatterns)) { |
264 | 265 | return [computeMixedGlobAndAbsolutePathValidator()] |
|
0 commit comments