Skip to content

Commit

Permalink
feat(utils): normalizeTarget
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 8219ec8 commit 2cbabae
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/utils/__tests__/normalize-target.spec.ts
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
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { default as normalizeModule } from './normalize-module'
export { default as normalizeModuleDetection } from './normalize-module-detection'
export { default as normalizeModuleResolution } from './normalize-module-resolution'
export { default as normalizeNewLine } from './normalize-new-line'
export { default as normalizeTarget } from './normalize-target'
93 changes: 93 additions & 0 deletions src/utils/normalize-target.ts
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

0 comments on commit 2cbabae

Please sign in to comment.