Skip to content

Commit

Permalink
feat(utils): normalizeImportsNotUsed
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 6, 2023
1 parent 23ee270 commit 532b575
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
"yaml-eslint-parser": "1.1.0"
},
"peerDependencies": {
"@types/node": ">=14.18.36"
"@types/node": ">=14.18.36",
"typescript": ">=4.7"
},
"peerDependenciesMeta": {
"@types/node": {
Expand Down
37 changes: 37 additions & 0 deletions src/utils/__tests__/normalize-imports-not-used.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file Unit Tests - normalizeImportsNotUsed
* @module tsconfig-utils/utils/tests/unit/normalizeImportsNotUsed
*/

import { ImportsNotUsedKind } from '@flex-development/tsconfig-types'
import ts from 'typescript'
import testSubject from '../normalize-imports-not-used'

describe('unit:utils/normalizeImportsNotUsed', () => {
it('should return normalized compiler option', () => {
// Arrange
const cases: [
...Parameters<typeof testSubject>,
ts.ImportsNotUsedAsValues
][] = [
['Error', ts.ImportsNotUsedAsValues.Error],
['Preserve', ts.ImportsNotUsedAsValues.Preserve],
['Remove', ts.ImportsNotUsedAsValues.Remove],
[ImportsNotUsedKind.Error, ts.ImportsNotUsedAsValues.Error],
[ImportsNotUsedKind.Preserve, ts.ImportsNotUsedAsValues.Preserve],
[ImportsNotUsedKind.Remove, ts.ImportsNotUsedAsValues.Remove],
[ts.ImportsNotUsedAsValues.Error, ts.ImportsNotUsedAsValues.Error],
[ts.ImportsNotUsedAsValues.Preserve, ts.ImportsNotUsedAsValues.Preserve],
[ts.ImportsNotUsedAsValues.Remove, ts.ImportsNotUsedAsValues.Remove]
]

// Act + Expect
cases.forEach(([option, expected]) => {
expect(testSubject(option)).to.equal(expected)
})
})

it('should return undefined if option cannot be normalized', () => {
expect(testSubject(faker.string.sample())).to.be.undefined
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { default as loadLibConfig } from './load-lib-config'
export { default as loadPathAliases } from './load-path-aliases'
export { default as loadPluginConfigs } from './load-plugin-configs'
export { default as loadTsconfig } from './load-tsconfig'
export { default as normalizeImportsNotUsed } from './normalize-imports-not-used'
60 changes: 60 additions & 0 deletions src/utils/normalize-imports-not-used.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file Utilities - normalizeImportsNotUsed
* @module tsconfig-utils/utils/normalizeImportsNotUsed
*/

import { getPropertyValue } from '#src/internal'
import { ImportsNotUsedKind } from '@flex-development/tsconfig-types'
import { isString } from '@flex-development/tutils'
import ts from 'typescript'

/**
* Converts the given `option` to 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 {ts.ImportsNotUsedAsValues | undefined} `ts.ImportsNotUsedAsValues`
* value or `undefined`
*/
const normalizeImportsNotUsed = (
option: unknown
): ts.ImportsNotUsedAsValues | undefined => {
// lowercase user option if it is a string
if (isString(option)) option = option.toLowerCase()

/**
* TypeScript program compiler option value, if any.
*
* @var {ts.ImportsNotUsedAsValues | undefined} ret
*/
let ret: ts.ImportsNotUsedAsValues | undefined

// normalize user compiler option
switch (option as ImportsNotUsedKind | ts.ImportsNotUsedAsValues) {
case ImportsNotUsedKind.Error:
case getPropertyValue(ts.ImportsNotUsedAsValues, 'Error'):
ret = getPropertyValue(ts.ImportsNotUsedAsValues, 'Error')
break
case ImportsNotUsedKind.Preserve:
case getPropertyValue(ts.ImportsNotUsedAsValues, 'Preserve'):
ret = getPropertyValue(ts.ImportsNotUsedAsValues, 'Preserve')
break
case ImportsNotUsedKind.Remove:
case getPropertyValue(ts.ImportsNotUsedAsValues, 'Remove'):
ret = getPropertyValue(ts.ImportsNotUsedAsValues, 'Remove')
break
default:
break
}

return ret
}

export default normalizeImportsNotUsed
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ __metadata:
yaml-eslint-parser: "npm:1.1.0"
peerDependencies:
"@types/node": ">=14.18.36"
typescript: ">=4.7"
peerDependenciesMeta:
"@types/node":
optional: true
Expand Down

0 comments on commit 532b575

Please sign in to comment.