Skip to content

Commit 776f17d

Browse files
committed
feat(interfaces): ModuleResolutionHost
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 3bd4124 commit 776f17d

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ This package is fully typed with [TypeScript][].
9898

9999
- [`FileSystem`](./src/interfaces/file-system.mts)
100100
- [`LoadTsconfigOptions`](./src/interfaces/options-load-tsconfig.mts)
101+
- [`ModuleResolutionHost`](./src/interfaces/host-module-resolution.mts)
101102
- [`ReadTsconfigOptions`](./src/interfaces/options-read-tsconfig.mts)
102103
- [`ResolvePathOptions`](./src/interfaces/options-resolve-path.mts)
103104
- [`ResolvedTsconfig`](./src/interfaces/options-resolve-path.mts)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @file Type Tests - ModuleResolutionHost
3+
* @module tsconfig-utils/interfaces/tests/unit-d/ModuleResolutionHost
4+
*/
5+
6+
import type TestSubject from '#interfaces/host-module-resolution'
7+
import type { ModuleId } from '@flex-development/mlly'
8+
import type ts from 'typescript'
9+
10+
describe('unit-d:interfaces/ModuleResolutionHost', () => {
11+
it('should match ts.ModuleResolutionHost', () => {
12+
expectTypeOf<TestSubject>().toMatchTypeOf<ts.ModuleResolutionHost>()
13+
})
14+
15+
describe('directoryExists', () => {
16+
type Subject = TestSubject['directoryExists']
17+
18+
it('should match [this: void]', () => {
19+
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
20+
})
21+
22+
describe('parameters', () => {
23+
it('should be callable with [ModuleId]', () => {
24+
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
25+
})
26+
})
27+
28+
describe('returns', () => {
29+
it('should return boolean', () => {
30+
expectTypeOf<Subject>().returns.toEqualTypeOf<boolean>()
31+
})
32+
})
33+
})
34+
35+
describe('fileExists', () => {
36+
type Subject = TestSubject['fileExists']
37+
38+
it('should match [this: void]', () => {
39+
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
40+
})
41+
42+
describe('parameters', () => {
43+
it('should be callable with [ModuleId]', () => {
44+
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
45+
})
46+
})
47+
48+
describe('returns', () => {
49+
it('should return boolean', () => {
50+
expectTypeOf<Subject>().returns.toEqualTypeOf<boolean>()
51+
})
52+
})
53+
})
54+
55+
describe('getDirectories', () => {
56+
type Subject = TestSubject['getDirectories']
57+
58+
it('should match [this: void]', () => {
59+
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
60+
})
61+
62+
describe('parameters', () => {
63+
it('should be callable with [ModuleId]', () => {
64+
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
65+
})
66+
})
67+
68+
describe('returns', () => {
69+
it('should return string[]', () => {
70+
expectTypeOf<Subject>().returns.toEqualTypeOf<string[]>()
71+
})
72+
})
73+
})
74+
75+
describe('readFile', () => {
76+
type Subject = TestSubject['readFile']
77+
78+
it('should match [this: void]', () => {
79+
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
80+
})
81+
82+
describe('parameters', () => {
83+
it('should be callable with [ModuleId]', () => {
84+
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
85+
})
86+
})
87+
88+
describe('returns', () => {
89+
it('should return string | undefined', () => {
90+
expectTypeOf<Subject>().returns.toEqualTypeOf<string | undefined>()
91+
})
92+
})
93+
})
94+
95+
describe('realpath', () => {
96+
type Subject = TestSubject['realpath']
97+
98+
it('should match [this: void]', () => {
99+
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
100+
})
101+
102+
describe('parameters', () => {
103+
it('should be callable with [ModuleId]', () => {
104+
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
105+
})
106+
})
107+
108+
describe('returns', () => {
109+
it('should return string', () => {
110+
expectTypeOf<Subject>().returns.toEqualTypeOf<string>()
111+
})
112+
})
113+
})
114+
})
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @file Interfaces - ModuleResolutionHost
3+
* @module tsconfig-utils/interfaces/ModuleResolutionHost
4+
*/
5+
6+
import type { ModuleId } from '@flex-development/mlly'
7+
8+
/**
9+
* Module resolution host API.
10+
*
11+
* The module resolution host acts a bridge between the TypeScript compiler and
12+
* the file system.
13+
*/
14+
interface ModuleResolutionHost {
15+
/**
16+
* Check if a directory exists at `id`.
17+
*
18+
* @see {@linkcode ModuleId}
19+
*
20+
* @this {void}
21+
*
22+
* @param {ModuleId} id
23+
* The module id to check
24+
* @return {boolean}
25+
* `true` if directory exists at `id`, `false` otherwise
26+
*/
27+
directoryExists(this: void, id: ModuleId): boolean
28+
29+
/**
30+
* Check if a file exists at `id`.
31+
*
32+
* @see {@linkcode ModuleId}
33+
*
34+
* @this {void}
35+
*
36+
* @param {ModuleId} id
37+
* The module id to check
38+
* @return {boolean}
39+
* `true` if file exists at `id`, `false` otherwise
40+
*/
41+
fileExists(this: void, id: ModuleId): boolean
42+
43+
/**
44+
* Get the path to current working directory.
45+
*
46+
* @this {void}
47+
*
48+
* @return {string}
49+
* Path to current working directory
50+
*/
51+
getCurrentDirectory(this: void): string
52+
53+
/**
54+
* Get a list of subdirectories.
55+
*
56+
* @see {@linkcode ModuleId}
57+
*
58+
* @this {void}
59+
*
60+
* @param {ModuleId} id
61+
* The directory path or URL to read
62+
* @return {string[]}
63+
* List of subdirectory names
64+
*/
65+
getDirectories(this: void, id: ModuleId): string[]
66+
67+
/**
68+
* Get the contents of a file.
69+
*
70+
* @see {@linkcode ModuleId}
71+
*
72+
* @this {void}
73+
*
74+
* @param {ModuleId} id
75+
* Module id of file
76+
* @return {Buffer | string}
77+
* File contents or `undefined` if file does not exist at `id`
78+
*/
79+
readFile(this: void, id: ModuleId): string | undefined
80+
81+
/**
82+
* Get the resolved pathname for `id`.
83+
*
84+
* @see {@linkcode ModuleId}
85+
*
86+
* @this {void}
87+
*
88+
* @param {ModuleId} id
89+
* The path or `file:` URL to handle
90+
* @return {string}
91+
* Resolved pathname
92+
*/
93+
realpath(this: void, id: ModuleId): string
94+
95+
/**
96+
* Treat filenames as case-sensitive?
97+
*/
98+
useCaseSensitiveFileNames?: ((this: void) => boolean) | boolean | undefined
99+
}
100+
101+
export type { ModuleResolutionHost as default }

src/interfaces/index.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55

66
export type { default as FileSystem } from '#interfaces/file-system'
7+
export type {
8+
default as ModuleResolutionHost
9+
} from '#interfaces/host-module-resolution'
710
export type {
811
default as LoadTsconfigOptions
912
} from '#interfaces/options-load-tsconfig'

0 commit comments

Comments
 (0)