diff --git a/src/rules/no-extraneous-dependencies.ts b/src/rules/no-extraneous-dependencies.ts index b7f16ba1f..b19e75161 100644 --- a/src/rules/no-extraneous-dependencies.ts +++ b/src/rules/no-extraneous-dependencies.ts @@ -13,6 +13,7 @@ import { pkgUp, importType, getFilePackageName, + globResolve, } from '../utils' type PackageDeps = ReturnType @@ -335,9 +336,7 @@ function testConfig(config: string[] | boolean | undefined, filename: string) { } // Array of globs. return config.some( - c => - minimatch(filename, c) || - minimatch(filename, path.resolve(c).replaceAll(path.sep, '/')), + c => minimatch(filename, c) || minimatch(filename, globResolve(c)), ) } diff --git a/src/rules/no-restricted-paths.ts b/src/rules/no-restricted-paths.ts index 8284079d7..999e4e4e7 100644 --- a/src/rules/no-restricted-paths.ts +++ b/src/rules/no-restricted-paths.ts @@ -5,7 +5,13 @@ import isGlob from 'is-glob' import { Minimatch } from 'minimatch' import type { Arrayable } from '../types' -import { importType, createRule, moduleVisitor, resolve } from '../utils' +import { + importType, + createRule, + moduleVisitor, + resolve, + globResolve, +} from '../utils' const containsPath = (filepath: string, target: string) => { const relative = path.relative(target, filepath) @@ -14,7 +20,7 @@ const containsPath = (filepath: string, target: string) => { function isMatchingTargetPath(filename: string, targetPath: string) { if (isGlob(targetPath)) { - const mm = new Minimatch(targetPath.replaceAll(path.sep, '/')) + const mm = new Minimatch(targetPath) return mm.match(filename) } @@ -120,7 +126,7 @@ export = createRule<[Options?], MessageId>({ const matchingZones = restrictedPaths.filter(zone => [zone.target] .flat() - .map(target => path.resolve(basePath, target)) + .map(target => globResolve(basePath, target)) .some(targetPath => isMatchingTargetPath(filename, targetPath)), ) @@ -171,15 +177,13 @@ export = createRule<[Options?], MessageId>({ ) { let isPathException: ((absoluteImportPath: string) => boolean) | undefined - const mm = new Minimatch(absoluteFrom.replaceAll(path.sep, '/')) + const mm = new Minimatch(absoluteFrom) const isPathRestricted = (absoluteImportPath: string) => mm.match(absoluteImportPath) const hasValidExceptions = zoneExcept.every(it => isGlob(it)) if (hasValidExceptions) { - const exceptionsMm = zoneExcept.map( - except => new Minimatch(except.replaceAll(path.sep, '/')), - ) + const exceptionsMm = zoneExcept.map(except => new Minimatch(except)) isPathException = (absoluteImportPath: string) => exceptionsMm.some(mm => mm.match(absoluteImportPath)) } @@ -267,12 +271,16 @@ export = createRule<[Options?], MessageId>({ const isGlobPattern = areGlobPatterns.every(Boolean) return allZoneFrom.map(singleZoneFrom => { - const absoluteFrom = path.resolve(basePath, singleZoneFrom) - if (isGlobPattern) { - return computeGlobPatternPathValidator(absoluteFrom, zoneExcept) + return computeGlobPatternPathValidator( + globResolve(basePath, singleZoneFrom), + zoneExcept, + ) } - return computeAbsolutePathValidator(absoluteFrom, zoneExcept) + return computeAbsolutePathValidator( + path.resolve(basePath, singleZoneFrom), + zoneExcept, + ) }) } diff --git a/src/rules/no-unassigned-import.ts b/src/rules/no-unassigned-import.ts index e09fecf34..eadbd7706 100644 --- a/src/rules/no-unassigned-import.ts +++ b/src/rules/no-unassigned-import.ts @@ -2,7 +2,7 @@ import path from 'node:path' import { minimatch } from 'minimatch' -import { isStaticRequire, createRule } from '../utils' +import { isStaticRequire, createRule, globResolve } from '../utils' function testIsAllow( globs: string[] | undefined, @@ -20,9 +20,7 @@ function testIsAllow( : path.resolve(path.dirname(filename), source) // get source absolute path return globs.some( - glob => - minimatch(filePath, glob) || - minimatch(filePath, path.resolve(glob).replaceAll(path.sep, '/')), + glob => minimatch(filePath, glob) || minimatch(filePath, globResolve(glob)), ) } diff --git a/src/utils/glob-resolve.ts b/src/utils/glob-resolve.ts new file mode 100644 index 000000000..3cd192d3d --- /dev/null +++ b/src/utils/glob-resolve.ts @@ -0,0 +1,8 @@ +import path from 'node:path' + +/** + * Wrapper around `path.resolve` that replaces all `path.sep` with `/` to ensure it remains a valid glob pattern. + */ +export const globResolve = (...paths: string[]): string => { + return path.resolve(...paths).replaceAll(path.sep, '/') +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 6e0b8c986..cb8d1ed64 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,6 +4,7 @@ export * from './declared-scope' export * from './docs-url' export * from './export-map' export * from './get-value' +export * from './glob-resolve' export * from './hash' export * from './ignore' export * from './import-declaration'