Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(ivy): verify whether TypeScript version is supported (#33377)
During the creation of an Angular program in the compiler, a check is done to verify whether the version of TypeScript is considered supported, producing an error if it is not. This check was missing in the Ivy compiler, so users may have ended up running an unsupported TypeScript version inadvertently. Resolves FW-1643 PR Close #33377
- Loading branch information
1 parent
09a2bb8
commit 4aa51b7
Showing
6 changed files
with
147 additions
and
73 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
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,59 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import * as ts from 'typescript'; | ||
import {compareVersions} from './diagnostics/typescript_version'; | ||
|
||
/** | ||
* Minimum supported TypeScript version | ||
* ∀ supported typescript version v, v >= MIN_TS_VERSION | ||
*/ | ||
const MIN_TS_VERSION = '3.6.4'; | ||
|
||
/** | ||
* Supremum of supported TypeScript versions | ||
* ∀ supported typescript version v, v < MAX_TS_VERSION | ||
* MAX_TS_VERSION is not considered as a supported TypeScript version | ||
*/ | ||
const MAX_TS_VERSION = '3.7.0'; | ||
|
||
/** | ||
* The currently used version of TypeScript, which can be adjusted for testing purposes using | ||
* `setTypeScriptVersionForTesting` and `restoreTypeScriptVersionForTesting` below. | ||
*/ | ||
let tsVersion = ts.version; | ||
|
||
export function setTypeScriptVersionForTesting(version: string): void { | ||
tsVersion = version; | ||
} | ||
|
||
export function restoreTypeScriptVersionForTesting(): void { | ||
tsVersion = ts.version; | ||
} | ||
|
||
/** | ||
* Checks whether a given version ∈ [minVersion, maxVersion[ | ||
* An error will be thrown if the following statements are simultaneously true: | ||
* - the given version ∉ [minVersion, maxVersion[, | ||
* | ||
* @param version The version on which the check will be performed | ||
* @param minVersion The lower bound version. A valid version needs to be greater than minVersion | ||
* @param maxVersion The upper bound version. A valid version needs to be strictly less than | ||
* maxVersion | ||
* | ||
* @throws Will throw an error if the given version ∉ [minVersion, maxVersion[ | ||
*/ | ||
export function checkVersion(version: string, minVersion: string, maxVersion: string) { | ||
if ((compareVersions(version, minVersion) < 0 || compareVersions(version, maxVersion) >= 0)) { | ||
throw new Error( | ||
`The Angular Compiler requires TypeScript >=${minVersion} and <${maxVersion} but ${version} was found instead.`); | ||
} | ||
} | ||
|
||
export function verifySupportedTypeScriptVersion(): void { | ||
checkVersion(tsVersion, MIN_TS_VERSION, MAX_TS_VERSION); | ||
} |
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
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,35 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {checkVersion} from '../src/typescript_support'; | ||
|
||
describe('checkVersion', () => { | ||
const MIN_TS_VERSION = '2.7.2'; | ||
const MAX_TS_VERSION = '2.8.0'; | ||
|
||
const versionError = (version: string) => | ||
`The Angular Compiler requires TypeScript >=${MIN_TS_VERSION} and <${MAX_TS_VERSION} but ${version} was found instead.`; | ||
|
||
it('should not throw when a supported TypeScript version is used', () => { | ||
expect(() => checkVersion('2.7.2', MIN_TS_VERSION, MAX_TS_VERSION)).not.toThrow(); | ||
expect(() => checkVersion('2.7.9', MIN_TS_VERSION, MAX_TS_VERSION)).not.toThrow(); | ||
}); | ||
|
||
it('should handle a TypeScript version < the minimum supported one', () => { | ||
expect(() => checkVersion('2.4.1', MIN_TS_VERSION, MAX_TS_VERSION)) | ||
.toThrowError(versionError('2.4.1')); | ||
expect(() => checkVersion('2.7.1', MIN_TS_VERSION, MAX_TS_VERSION)) | ||
.toThrowError(versionError('2.7.1')); | ||
}); | ||
|
||
it('should handle a TypeScript version > the maximum supported one', () => { | ||
expect(() => checkVersion('2.9.0', MIN_TS_VERSION, MAX_TS_VERSION)) | ||
.toThrowError(versionError('2.9.0')); | ||
expect(() => checkVersion('2.8.0', MIN_TS_VERSION, MAX_TS_VERSION)) | ||
.toThrowError(versionError('2.8.0')); | ||
}); | ||
}); |