@@ -13,6 +13,7 @@ import * as path from 'path';
1313import * as ts from 'typescript' ;
1414
1515import { TypeCheckHost , translateDiagnostics } from '../diagnostics/translate_diagnostics' ;
16+ import { compareVersions } from '../diagnostics/typescript_version' ;
1617import { MetadataCollector , ModuleMetadata , createBundleIndexHost } from '../metadata/index' ;
1718
1819import { CompilerHost , CompilerOptions , CustomTransformers , DEFAULT_ERROR_CODE , Diagnostic , DiagnosticMessageChain , EmitFlags , LazyRoute , LibrarySummary , Program , SOURCE , TsEmitArguments , TsEmitCallback , TsMergeEmitResultsCallback } from './api' ;
@@ -95,6 +96,19 @@ const defaultEmitCallback: TsEmitCallback =
9596 program . emit (
9697 targetSourceFile , writeFile , cancellationToken , emitOnlyDtsFiles , customTransformers ) ;
9798
99+ /**
100+ * Minimum supported TypeScript version
101+ * ∀ supported typescript version v, v >= MIN_TS_VERSION
102+ */
103+ const MIN_TS_VERSION = '2.7.2' ;
104+
105+ /**
106+ * Supremum of supported TypeScript versions
107+ * ∀ supported typescript version v, v < MAX_TS_VERSION
108+ * MAX_TS_VERSION is not considered as a supported TypeScript version
109+ */
110+ const MAX_TS_VERSION = '2.8.0' ;
111+
98112class AngularCompilerProgram implements Program {
99113 private rootNames : string [ ] ;
100114 private metadataCache : MetadataCache ;
@@ -124,10 +138,7 @@ class AngularCompilerProgram implements Program {
124138 private host : CompilerHost , oldProgram ?: Program ) {
125139 this . rootNames = [ ...rootNames ] ;
126140
127- if ( ( ts . version < '2.7.2' || ts . version >= '2.8.0' ) && ! options . disableTypeScriptVersionCheck ) {
128- throw new Error (
129- `The Angular Compiler requires TypeScript >=2.7.2 and <2.8.0 but ${ ts . version } was found instead.` ) ;
130- }
141+ checkVersion ( ts . version , MIN_TS_VERSION , MAX_TS_VERSION , options . disableTypeScriptVersionCheck ) ;
131142
132143 this . oldTsProgram = oldProgram ? oldProgram . getTsProgram ( ) : undefined ;
133144 if ( oldProgram ) {
@@ -847,6 +858,34 @@ class AngularCompilerProgram implements Program {
847858 }
848859}
849860
861+ /**
862+ * Checks whether a given version ∈ [minVersion, maxVersion[
863+ * An error will be thrown if the following statements are simultaneously true:
864+ * - the given version ∉ [minVersion, maxVersion[,
865+ * - the result of the version check is not meant to be bypassed (the parameter disableVersionCheck
866+ * is false)
867+ *
868+ * @param version The version on which the check will be performed
869+ * @param minVersion The lower bound version. A valid version needs to be greater than minVersion
870+ * @param maxVersion The upper bound version. A valid version needs to be strictly less than
871+ * maxVersion
872+ * @param disableVersionCheck Indicates whether version check should be bypassed
873+ *
874+ * @throws Will throw an error if the following statements are simultaneously true:
875+ * - the given version ∉ [minVersion, maxVersion[,
876+ * - the result of the version check is not meant to be bypassed (the parameter disableVersionCheck
877+ * is false)
878+ */
879+ export function checkVersion (
880+ version : string , minVersion : string , maxVersion : string ,
881+ disableVersionCheck : boolean | undefined ) {
882+ if ( ( compareVersions ( version , minVersion ) < 0 || compareVersions ( version , maxVersion ) >= 0 ) &&
883+ ! disableVersionCheck ) {
884+ throw new Error (
885+ `The Angular Compiler requires TypeScript >=${ minVersion } and <${ maxVersion } but ${ version } was found instead.` ) ;
886+ }
887+ }
888+
850889export function createProgram ( { rootNames, options, host, oldProgram} : {
851890 rootNames : ReadonlyArray < string > ,
852891 options : CompilerOptions ,
0 commit comments