-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
b3f2126
commit e46db36
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |