-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize-imports-not-used.ts
64 lines (58 loc) · 1.79 KB
/
normalize-imports-not-used.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @file Utilities - normalizeImportsNotUsed
* @module tsconfig-utils/utils/normalizeImportsNotUsed
*/
import { ImportsNotUsedKind } from '@flex-development/tsconfig-types'
import {
cast,
isString,
lowercase,
type Optional
} from '@flex-development/tutils'
import ts from 'typescript'
/**
* Converts the given `option` into a **programmatic**
* [`importsNotUsedAsValues`][1] option.
*
* TypeScript programs expect a {@linkcode ts.ImportsNotUsedAsValues} value.
*
* If the `option` is already programmatic, it will be returned unmodified. If
* it cannot be converted, `undefined` will be returned instead.
*
* [1]: https://www.typescriptlang.org/tsconfig#importsNotUsedAsValues
*
* @param {unknown} option - Option to evaluate
* @return {Optional<ts.ImportsNotUsedAsValues>} `ts.ImportsNotUsedAsValues`
* value or `undefined`
*/
const normalizeImportsNotUsed = (
option: unknown
): Optional<ts.ImportsNotUsedAsValues> => {
// lowercase user option if it is a string
if (isString(option)) option = lowercase(option)
/**
* TypeScript program compiler option value, if any.
*
* @var {Optional<ts.ImportsNotUsedAsValues>} ret
*/
let ret: Optional<ts.ImportsNotUsedAsValues>
// normalize user compiler option
switch (cast<ImportsNotUsedKind | ts.ImportsNotUsedAsValues>(option)) {
case ImportsNotUsedKind.Error:
case ts.ImportsNotUsedAsValues.Error:
ret = ts.ImportsNotUsedAsValues.Error
break
case ImportsNotUsedKind.Preserve:
case ts.ImportsNotUsedAsValues.Preserve:
ret = ts.ImportsNotUsedAsValues.Preserve
break
case ImportsNotUsedKind.Remove:
case ts.ImportsNotUsedAsValues.Remove:
ret = ts.ImportsNotUsedAsValues.Remove
break
default:
break
}
return ret
}
export default normalizeImportsNotUsed