Skip to content

Commit

Permalink
Add globResolve utility
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus committed Dec 19, 2024
1 parent 76c2ed0 commit 8ed4883
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/rules/no-extraneous-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
pkgUp,
importType,
getFilePackageName,
globResolve,
} from '../utils'

type PackageDeps = ReturnType<typeof extractDepFields>
Expand Down Expand Up @@ -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)),
)
}

Expand Down
30 changes: 19 additions & 11 deletions src/rules/no-restricted-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

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

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

Expand Down
6 changes: 2 additions & 4 deletions src/rules/no-unassigned-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)),
)
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/glob-resolve.ts
Original file line number Diff line number Diff line change
@@ -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, '/')
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8ed4883

Please sign in to comment.