Skip to content

Commit

Permalink
feat(utils): normalizeModule
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 b3f2126 commit e46db36
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/utils/__tests__/normalize-module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file Unit Tests - normalizeModule
* @module tsconfig-utils/utils/tests/unit/normalizeModule
*/

import { ModuleKind } from '@flex-development/tsconfig-types'
import ts from 'typescript'
import testSubject from '../normalize-module'

describe('unit:utils/normalizeModule', () => {
it('should return normalized compiler option', () => {
// Arrange
const cases: [...Parameters<typeof testSubject>, ts.ModuleKind][] = [
['AMD', ts.ModuleKind.AMD],
['CommonJS', ts.ModuleKind.CommonJS],
['ES6', ts.ModuleKind.ES2015],
['ES2015', ts.ModuleKind.ES2015],
['ES2020', ts.ModuleKind.ES2020],
['ES2022', ts.ModuleKind.ES2022],
['ESNext', ts.ModuleKind.ESNext],
['Node16', ts.ModuleKind.Node16],
['NodeNext', ts.ModuleKind.NodeNext],
['None', ts.ModuleKind.None],
['System', ts.ModuleKind.System],
['UMD', ts.ModuleKind.UMD],
[ModuleKind.AMD, ts.ModuleKind.AMD],
[ModuleKind.CommonJS, ts.ModuleKind.CommonJS],
[ModuleKind.ES6, ts.ModuleKind.ES2015],
[ModuleKind.ES2015, ts.ModuleKind.ES2015],
[ModuleKind.ES2020, ts.ModuleKind.ES2020],
[ModuleKind.ES2022, ts.ModuleKind.ES2022],
[ModuleKind.ESNext, ts.ModuleKind.ESNext],
[ModuleKind.Node16, ts.ModuleKind.Node16],
[ModuleKind.NodeNext, ts.ModuleKind.NodeNext],
[ModuleKind.None, ts.ModuleKind.None],
[ModuleKind.System, ts.ModuleKind.System],
[ModuleKind.UMD, ts.ModuleKind.UMD],
[ts.ModuleKind.AMD, ts.ModuleKind.AMD],
[ts.ModuleKind.CommonJS, ts.ModuleKind.CommonJS],
[ts.ModuleKind.ES2015, ts.ModuleKind.ES2015],
[ts.ModuleKind.ES2020, ts.ModuleKind.ES2020],
[ts.ModuleKind.ES2022, ts.ModuleKind.ES2022],
[ts.ModuleKind.ESNext, ts.ModuleKind.ESNext],
[ts.ModuleKind.Node16, ts.ModuleKind.Node16],
[ts.ModuleKind.NodeNext, ts.ModuleKind.NodeNext],
[ts.ModuleKind.None, ts.ModuleKind.None],
[ts.ModuleKind.System, ts.ModuleKind.System],
[ts.ModuleKind.UMD, ts.ModuleKind.UMD]
]

// 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 @@ -13,3 +13,4 @@ export { default as loadTsconfig } from './load-tsconfig'
export { default as normalizeImportsNotUsed } from './normalize-imports-not-used'
export { default as normalizeJsx } from './normalize-jsx'
export { default as normalizeLib } from './normalize-lib'
export { default as normalizeModule } from './normalize-module'
90 changes: 90 additions & 0 deletions src/utils/normalize-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file Utilities - normalizeModule
* @module tsconfig-utils/utils/normalizeModule
*/

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

/**
* Converts the given `option` to a **programmatic** [`module`][1] option.
*
* TypeScript programs expect a {@linkcode ts.ModuleKind} 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#module
*
* @param {unknown} option - Option to evaluate
* @return {ts.ModuleKind | undefined} `ts.ModuleKind` value or `undefined`
*/
const normalizeModule = (option: unknown): ts.ModuleKind | undefined => {
// lowercase user option if it is a string
if (isString(option)) option = option.toLowerCase()

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

// normalize user compiler option
switch (option as ModuleKind | ts.ModuleKind) {
case ModuleKind.AMD:
case getPropertyValue(ts.ModuleKind, 'AMD'):
ret = getPropertyValue(ts.ModuleKind, 'AMD')
break
case ModuleKind.CommonJS:
case getPropertyValue(ts.ModuleKind, 'CommonJS'):
ret = getPropertyValue(ts.ModuleKind, 'CommonJS')
break
case ModuleKind.ES6:
case ModuleKind.ES2015:
case getPropertyValue(ts.ModuleKind, 'ES6'):
case getPropertyValue(ts.ModuleKind, 'ES2015'):
ret = getPropertyValue(ts.ModuleKind, 'ES2015')
break
case ModuleKind.ES2020:
case getPropertyValue(ts.ModuleKind, 'ES2020'):
ret = getPropertyValue(ts.ModuleKind, 'ES2020')
break
case ModuleKind.ES2022:
case getPropertyValue(ts.ModuleKind, 'ES2022'):
ret = getPropertyValue(ts.ModuleKind, 'ES2022')
break
case ModuleKind.ESNext:
case getPropertyValue(ts.ModuleKind, 'ESNext'):
ret = getPropertyValue(ts.ModuleKind, 'ESNext')
break
case ModuleKind.Node16:
case getPropertyValue(ts.ModuleKind, 'Node16'):
ret = getPropertyValue(ts.ModuleKind, 'Node16')
break
case ModuleKind.NodeNext:
case getPropertyValue(ts.ModuleKind, 'NodeNext'):
ret = getPropertyValue(ts.ModuleKind, 'NodeNext')
break
case ModuleKind.None:
case getPropertyValue(ts.ModuleKind, 'None'):
ret = getPropertyValue(ts.ModuleKind, 'None')
break
case ModuleKind.System:
case getPropertyValue(ts.ModuleKind, 'System'):
ret = getPropertyValue(ts.ModuleKind, 'System')
break
case ModuleKind.UMD:
case getPropertyValue(ts.ModuleKind, 'UMD'):
ret = getPropertyValue(ts.ModuleKind, 'UMD')
break
default:
break
}

return ret
}

export default normalizeModule

0 comments on commit e46db36

Please sign in to comment.