Skip to content

Commit 0273417

Browse files
committed
refactor(types): improve type annotations for better type coverage
Add explicit type annotations to improve type coverage: - UNDEFINED_TOKEN: explicit undefined type - globs.ts: typed matcherCache Map, typed require() calls - signal-exit.ts: typed __signal_exit_emitter__ - sorts.ts: typed semver and fastSort require() imports These changes help move toward 99%+ type coverage goal.
1 parent b81d0d2 commit 0273417

4 files changed

Lines changed: 27 additions & 24 deletions

File tree

src/constants/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const EMPTY_FILE = '/* empty */\n'
1818
export const EMPTY_VALUE = '<value>'
1919

2020
// Undefined token.
21-
export const UNDEFINED_TOKEN = undefined
21+
export const UNDEFINED_TOKEN: undefined = undefined
2222

2323
// Miscellaneous.
2424
export const V = 'v'

src/globs.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ let _fastGlob: typeof import('fast-glob') | undefined
104104
/*@__NO_SIDE_EFFECTS__*/
105105
function getFastGlob() {
106106
if (_fastGlob === undefined) {
107-
const globExport = /*@__PURE__*/ require('./external/fast-glob')
108-
_fastGlob = globExport.default || globExport
107+
const globExport = /*@__PURE__*/ require('./external/fast-glob') as
108+
| (typeof import('fast-glob') & { default?: typeof import('fast-glob') })
109+
| typeof import('fast-glob')
110+
_fastGlob = (
111+
'default' in globExport ? globExport.default : globExport
112+
) as typeof import('fast-glob')
109113
}
110114
return _fastGlob as typeof import('fast-glob')
111115
}
@@ -129,18 +133,15 @@ export function globStreamLicenses(
129133
'**/*.{cjs,cts,js,json,mjs,mts,ts}',
130134
]
131135
if (ignoreOriginals) {
132-
ignore.push(
133-
/*@__INLINE__*/ require('#constants/paths')
134-
.LICENSE_ORIGINAL_GLOB_RECURSIVE,
135-
)
136+
const { LICENSE_ORIGINAL_GLOB_RECURSIVE } =
137+
/*@__INLINE__*/ require('#constants/paths') as typeof import('#constants/paths')
138+
ignore.push(LICENSE_ORIGINAL_GLOB_RECURSIVE)
136139
}
137140
const fastGlob = getFastGlob()
141+
const paths =
142+
/*@__INLINE__*/ require('#constants/paths') as typeof import('#constants/paths')
138143
return fastGlob.globStream(
139-
[
140-
recursive
141-
? /*@__INLINE__*/ require('#constants/paths').LICENSE_GLOB_RECURSIVE
142-
: /*@__INLINE__*/ require('#constants/paths').LICENSE_GLOB,
143-
],
144+
[recursive ? paths.LICENSE_GLOB_RECURSIVE : paths.LICENSE_GLOB],
144145
{
145146
__proto__: null,
146147
absolute: true,
@@ -152,7 +153,7 @@ export function globStreamLicenses(
152153
)
153154
}
154155

155-
const matcherCache = new Map()
156+
const matcherCache = new Map<string, (path: string) => boolean>()
156157
/**
157158
* Get a cached glob matcher function.
158159
*/
@@ -163,7 +164,7 @@ export function getGlobMatcher(
163164
): (path: string) => boolean {
164165
const patterns = Array.isArray(glob) ? glob : [glob]
165166
const key = JSON.stringify({ patterns, options })
166-
let matcher = matcherCache.get(key)
167+
let matcher: ((path: string) => boolean) | undefined = matcherCache.get(key)
167168
if (matcher) {
168169
return matcher
169170
}
@@ -187,7 +188,7 @@ export function getGlobMatcher(
187188
matcher = picomatch(
188189
positivePatterns.length > 0 ? positivePatterns : patterns,
189190
matchOptions,
190-
)
191+
) as (path: string) => boolean
191192

192193
matcherCache.set(key, matcher)
193194
return matcher

src/signal-exit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
const ReflectApply = Reflect.apply
3737
const globalProcess = globalThis.process as
3838
| (NodeJS.Process & {
39-
__signal_exit_emitter__?: any
39+
__signal_exit_emitter__?: import('node:events').EventEmitter
4040
reallyExit?: (code?: number | undefined) => never
4141
})
4242
| undefined

src/sorts.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ export function naturalSorter<T>(
5656
arrayToSort: T[],
5757
): ReturnType<FastSortFunction> {
5858
if (_naturalSorter === undefined) {
59-
const fastSort = /*@__PURE__*/ require('./external/fast-sort')
60-
_naturalSorter = (fastSort as any).createNewSortInstance({
59+
const fastSort =
60+
/*@__PURE__*/ require('./external/fast-sort') as typeof import('fast-sort')
61+
_naturalSorter = fastSort.createNewSortInstance({
6162
comparer: naturalCompare,
62-
})
63+
}) as FastSortFunction
6364
}
64-
return _naturalSorter?.(arrayToSort)
65+
return (_naturalSorter as FastSortFunction)(arrayToSort)
6566
}
6667

6768
/**
@@ -77,9 +78,10 @@ export function compareStr(a: string, b: string): number {
7778
*/
7879
/*@__NO_SIDE_EFFECTS__*/
7980
export function compareSemver(a: string, b: string): number {
80-
const semver = /*@__PURE__*/ require('./external/semver')
81-
const validA = semver.valid(a)
82-
const validB = semver.valid(b)
81+
const semver =
82+
/*@__PURE__*/ require('./external/semver') as typeof import('semver')
83+
const validA: string | null = semver.valid(a)
84+
const validB: string | null = semver.valid(b)
8385

8486
if (!validA && !validB) {
8587
return 0
@@ -90,5 +92,5 @@ export function compareSemver(a: string, b: string): number {
9092
if (!validB) {
9193
return 1
9294
}
93-
return (semver as any).compare(a, b)
95+
return semver.compare(a, b) as number
9496
}

0 commit comments

Comments
 (0)