-
-
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
8219ec8
commit 2cbabae
Showing
3 changed files
with
155 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,61 @@ | ||
/** | ||
* @file Unit Tests - normalizeTarget | ||
* @module tsconfig-utils/utils/tests/unit/normalizeTarget | ||
*/ | ||
|
||
import { ScriptTarget } from '@flex-development/tsconfig-types' | ||
import ts from 'typescript' | ||
import testSubject from '../normalize-target' | ||
|
||
describe('unit:utils/normalizeTarget', () => { | ||
it('should return normalized compiler option', () => { | ||
// Arrange | ||
const cases: [...Parameters<typeof testSubject>, ts.ScriptTarget][] = [ | ||
['ES3', ts.ScriptTarget.ES3], | ||
['ES5', ts.ScriptTarget.ES5], | ||
['ES6', ts.ScriptTarget.ES2015], | ||
['ES2015', ts.ScriptTarget.ES2015], | ||
['ES2016', ts.ScriptTarget.ES2016], | ||
['ES2017', ts.ScriptTarget.ES2017], | ||
['ES2018', ts.ScriptTarget.ES2018], | ||
['ES2019', ts.ScriptTarget.ES2019], | ||
['ES2020', ts.ScriptTarget.ES2020], | ||
['ES2021', ts.ScriptTarget.ES2021], | ||
['ES2022', ts.ScriptTarget.ES2022], | ||
['ESNext', ts.ScriptTarget.ESNext], | ||
[ScriptTarget.ES3, ts.ScriptTarget.ES3], | ||
[ScriptTarget.ES5, ts.ScriptTarget.ES5], | ||
[ScriptTarget.ES6, ts.ScriptTarget.ES2015], | ||
[ScriptTarget.ES2015, ts.ScriptTarget.ES2015], | ||
[ScriptTarget.ES2016, ts.ScriptTarget.ES2016], | ||
[ScriptTarget.ES2017, ts.ScriptTarget.ES2017], | ||
[ScriptTarget.ES2018, ts.ScriptTarget.ES2018], | ||
[ScriptTarget.ES2019, ts.ScriptTarget.ES2019], | ||
[ScriptTarget.ES2020, ts.ScriptTarget.ES2020], | ||
[ScriptTarget.ES2021, ts.ScriptTarget.ES2021], | ||
[ScriptTarget.ES2022, ts.ScriptTarget.ES2022], | ||
[ScriptTarget.ESNext, ts.ScriptTarget.ESNext], | ||
[ts.ScriptTarget.ES3, ts.ScriptTarget.ES3], | ||
[ts.ScriptTarget.ES5, ts.ScriptTarget.ES5], | ||
[ts.ScriptTarget.ES2015, ts.ScriptTarget.ES2015], | ||
[ts.ScriptTarget.ES2016, ts.ScriptTarget.ES2016], | ||
[ts.ScriptTarget.ES2017, ts.ScriptTarget.ES2017], | ||
[ts.ScriptTarget.ES2018, ts.ScriptTarget.ES2018], | ||
[ts.ScriptTarget.ES2019, ts.ScriptTarget.ES2019], | ||
[ts.ScriptTarget.ES2020, ts.ScriptTarget.ES2020], | ||
[ts.ScriptTarget.ES2021, ts.ScriptTarget.ES2021], | ||
[ts.ScriptTarget.ES2022, ts.ScriptTarget.ES2022], | ||
[ts.ScriptTarget.ESNext, ts.ScriptTarget.ESNext], | ||
[ts.ScriptTarget.JSON, ts.ScriptTarget.JSON] | ||
] | ||
|
||
// 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,93 @@ | ||
/** | ||
* @file Utilities - normalizeTarget | ||
* @module tsconfig-utils/utils/normalizeTarget | ||
*/ | ||
|
||
import { getPropertyValue } from '#src/internal' | ||
import { ScriptTarget } from '@flex-development/tsconfig-types' | ||
import { isString } from '@flex-development/tutils' | ||
import ts from 'typescript' | ||
|
||
/** | ||
* Converts the given `option` to a **programmatic** [`target`][1] option. | ||
* | ||
* TypeScript programs expect a {@linkcode ts.ScriptTarget} 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#target | ||
* | ||
* @param {unknown} option - Option to evaluate | ||
* @return {ts.ScriptTarget | undefined} `ts.ScriptTarget` value or `undefined` | ||
*/ | ||
const normalizeTarget = (option: unknown): ts.ScriptTarget | undefined => { | ||
// lowercase user option if it is a string | ||
if (isString(option)) option = option.toLowerCase() | ||
|
||
/** | ||
* TypeScript program compiler option value, if any. | ||
* | ||
* @var {ts.ScriptTarget | undefined} ret | ||
*/ | ||
let ret: ts.ScriptTarget | undefined | ||
|
||
// normalize user compiler option | ||
switch (option as ScriptTarget | ts.ScriptTarget) { | ||
case ScriptTarget.ES3: | ||
case getPropertyValue(ts.ScriptTarget, 'ES3'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES3') | ||
break | ||
case ScriptTarget.ES5: | ||
case getPropertyValue(ts.ScriptTarget, 'ES5'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES5') | ||
break | ||
case ScriptTarget.ES6: | ||
case ScriptTarget.ES2015: | ||
case getPropertyValue(ts.ScriptTarget, 'ES6'): | ||
case getPropertyValue(ts.ScriptTarget, 'ES2015'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2015') | ||
break | ||
case ScriptTarget.ES2016: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2016'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2016') | ||
break | ||
case ScriptTarget.ES2017: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2017'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2017') | ||
break | ||
case ScriptTarget.ES2018: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2018'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2018') | ||
break | ||
case ScriptTarget.ES2019: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2019'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2019') | ||
break | ||
case ScriptTarget.ES2020: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2020'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2020') | ||
break | ||
case ScriptTarget.ES2021: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2021'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2021') | ||
break | ||
case ScriptTarget.ES2022: | ||
case getPropertyValue(ts.ScriptTarget, 'ES2022'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ES2022') | ||
break | ||
case ScriptTarget.ESNext: | ||
case getPropertyValue(ts.ScriptTarget, 'ESNext'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'ESNext') | ||
break | ||
case getPropertyValue(ts.ScriptTarget, 'JSON'): | ||
ret = getPropertyValue(ts.ScriptTarget, 'JSON') | ||
break | ||
default: | ||
break | ||
} | ||
|
||
return ret | ||
} | ||
|
||
export default normalizeTarget |