Skip to content

Commit 7c5656d

Browse files
committed
refactor(lib): move getCompilerOptions to internal
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 4615851 commit 7c5656d

16 files changed

+101
-542
lines changed

.eslintrc.cjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const config = {
1414
overrides: [
1515
...require('./.eslintrc.base.cjs').overrides,
1616
{
17-
files: ['src/lib/__tests__/get-compiler-options.spec.ts'],
17+
files: [
18+
'src/internal/__tests__/get-compiler-options.spec.ts',
19+
'src/internal/compiler-options-json.ts'
20+
],
1821
rules: {
1922
'unicorn/no-keyword-prefix': 0
2023
}

src/interfaces/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @module mlly/interfaces
44
*/
55

6-
export type { default as CompilerOptionsJson } from './compiler-options-json'
76
export type { default as DynamicImport } from './import-dynamic'
87
export type { default as StaticImport } from './import-static'
98
export type { default as ResolveOptions } from './options-resolve'

src/lib/__mocks__/get-compiler-options.ts src/internal/__mocks__/get-compiler-options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @file Mock Library - getCompilerOptions
3-
* @module mlly/lib/mocks/getCompilerOptions
2+
* @file Mock Internals - getCompilerOptions
3+
* @module mlly/internal/mocks/getCompilerOptions
44
*/
55

66
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @file Unit Tests - getCompilerOptions
3+
* @module mlly/internal/tests/getCompilerOptions/unit
4+
*/
5+
6+
import type { Spy } from '#tests/interfaces'
7+
import * as loader from 'tsconfig-paths/lib/tsconfig-loader'
8+
import tsconfig from '../../../tsconfig.json' assert { type: 'json' }
9+
import testSubject from '../get-compiler-options'
10+
11+
vi.mock('tsconfig-paths/lib/tsconfig-loader')
12+
13+
describe('unit:internal/getCompilerOptions', () => {
14+
const loadTsconfig = loader.loadTsconfig as unknown as Spy
15+
16+
it('should return empty object is tsconfig is not found', () => {
17+
// Arrange
18+
loadTsconfig.mockImplementationOnce(() => void 0)
19+
20+
// Act + Expect
21+
expect(testSubject()).to.deep.equal({})
22+
})
23+
24+
it('should return empty object if user options are not found', () => {
25+
// Arrange
26+
loadTsconfig.mockImplementationOnce(() => ({}))
27+
28+
// Act + Expect
29+
expect(testSubject()).to.deep.equal({})
30+
})
31+
32+
it('should return user options', () => {
33+
// Arrange
34+
loadTsconfig.mockImplementationOnce(() => tsconfig)
35+
36+
// Act + Expect
37+
expect(testSubject()).to.deep.equal(tsconfig.compilerOptions)
38+
})
39+
})

src/interfaces/compiler-options-json.ts src/internal/compiler-options-json.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/**
2-
* @file Interfaces - CompilerOptionsJson
3-
* @module mlly/interfaces/CompilerOptionsJson
2+
* @file Internals - CompilerOptionsJson
3+
* @module mlly/internal/CompilerOptionsJson
44
*/
55

66
import type { CompilerOptionsValue, MapLike } from 'typescript'
77

88
/**
99
* TypeScript compiler options provided by a user.
1010
*
11+
* @internal
12+
*
1113
* @see https://www.typescriptlang.org/tsconfig
1214
*/
1315
interface CompilerOptionsJson {

src/internal/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Internals - Constants
3-
* @module mlly/internals/constants
3+
* @module mlly/internal/constants
44
*/
55

66
/**

src/internal/get-compiler-options.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file Internals - getCompilerOptions
3+
* @module mlly/internal/getCompilerOptions
4+
*/
5+
6+
import { loadTsconfig } from 'tsconfig-paths/lib/tsconfig-loader'
7+
import upath from 'upath'
8+
import type CompilerOptionsJson from './compiler-options-json'
9+
10+
/**
11+
* Retrieves TypeScript compiler options from `path`.
12+
*
13+
* Supports [`extends`][1].
14+
*
15+
* [1]: https://typescriptlang.org/tsconfig#extends
16+
*
17+
* @internal
18+
*
19+
* @param {string} [path=upath.resolve('tsconfig.json')] - Tsconfig path
20+
* @param {(path: string) => boolean} [exists] - File existence checker
21+
* @param {(filename: string) => string} [read] - File content reader
22+
* @return {CompilerOptionsJson} User compiler options
23+
*/
24+
const getCompilerOptions = (
25+
path: string = upath.resolve('tsconfig.json'),
26+
exists?: (path: string) => boolean,
27+
read?: (filename: string) => string
28+
): CompilerOptionsJson => {
29+
/**
30+
* Tsconfig object.
31+
*
32+
* @const {{ compilerOptions?: CompilerOptionsJson } | undefined} t
33+
*/
34+
const t: { compilerOptions?: CompilerOptionsJson } | undefined = loadTsconfig(
35+
path,
36+
exists,
37+
read
38+
)
39+
40+
return !t?.compilerOptions ? {} : t.compilerOptions
41+
}
42+
43+
export default getCompilerOptions

src/internal/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @file Internals
3-
* @module mlly/internals
3+
* @module mlly/internal
44
*/
55

6+
export type { default as CompilerOptionsJson } from './compiler-options-json'
67
export * from './constants'
8+
export { default as getCompilerOptions } from './get-compiler-options'

0 commit comments

Comments
 (0)