@@ -68,13 +68,13 @@ namespace ts {
6868 }
6969
7070 // The global Map object. This may not be available, so we must test for it.
71- declare const Map : { new < T > ( ) : Map < T > } | undefined ;
71+ declare const Map : { new < T > ( ) : Map < T > } | undefined ;
7272 // Internet Explorer's Map doesn't support iteration, so don't use it.
7373 // tslint:disable-next-line no-in-operator variable-name
7474 const MapCtr = typeof Map !== "undefined" && "entries" in Map . prototype ? Map : shimMap ( ) ;
7575
7676 // Keep the class inside a function so it doesn't get compiled if it's not used.
77- function shimMap ( ) : { new < T > ( ) : Map < T > } {
77+ function shimMap ( ) : { new < T > ( ) : Map < T > } {
7878
7979 class MapIterator < T , U extends ( string | T | [ string , T ] ) > {
8080 private data : MapLike < T > ;
@@ -97,7 +97,7 @@ namespace ts {
9797 }
9898 }
9999
100- return class < T > implements Map < T > {
100+ return class < T > implements Map < T > {
101101 private data = createDictionaryObject < T > ( ) ;
102102 public size = 0 ;
103103
@@ -394,12 +394,14 @@ namespace ts {
394394 return result ;
395395 }
396396
397- export function mapIter < T , U > ( iter : Iterator < T > , mapFn : ( x : T ) => U ) : Iterator < U > {
398- return { next } ;
399- function next ( ) : { value : U , done : false } | { value : never , done : true } {
400- const iterRes = iter . next ( ) ;
401- return iterRes . done ? iterRes : { value : mapFn ( iterRes . value ) , done : false } ;
402- }
397+
398+ export function mapIterator < T , U > ( iter : Iterator < T > , mapFn : ( x : T ) => U ) : Iterator < U > {
399+ return {
400+ next ( ) {
401+ const iterRes = iter . next ( ) ;
402+ return iterRes . done ? iterRes : { value : mapFn ( iterRes . value ) , done : false } ;
403+ }
404+ } ;
403405 }
404406
405407 // Maps from T to T and avoids allocation if all elements map to themselves
@@ -936,6 +938,36 @@ namespace ts {
936938 return array . slice ( ) . sort ( comparer ) ;
937939 }
938940
941+ export function best < T > ( iter : Iterator < T > , isBetter : ( a : T , b : T ) => boolean ) : T | undefined {
942+ const x = iter . next ( ) ;
943+ if ( x . done ) {
944+ return undefined ;
945+ }
946+ let best = x . value ;
947+ while ( true ) {
948+ const { value, done } = iter . next ( ) ;
949+ if ( done ) {
950+ return best ;
951+ }
952+ if ( isBetter ( value , best ) ) {
953+ best = value ;
954+ }
955+ }
956+ }
957+
958+ export function arrayIterator < T > ( array : ReadonlyArray < T > ) : Iterator < T > {
959+ let i = 0 ;
960+ return { next : ( ) => {
961+ if ( i === array . length ) {
962+ return { value : undefined as never , done : true } ;
963+ }
964+ else {
965+ i ++ ;
966+ return { value : array [ i - 1 ] , done : false } ;
967+ }
968+ } } ;
969+ }
970+
939971 /**
940972 * Stable sort of an array. Elements equal to each other maintain their relative position in the array.
941973 */
@@ -1241,10 +1273,12 @@ namespace ts {
12411273 return result ;
12421274 }
12431275
1244- export function arrayToNumericMap < T > ( array : ReadonlyArray < T > , makeKey : ( value : T ) => number ) : T [ ] {
1245- const result : T [ ] = [ ] ;
1276+ export function arrayToNumericMap < T > ( array : ReadonlyArray < T > , makeKey : ( value : T ) => number ) : T [ ] ;
1277+ export function arrayToNumericMap < T , V > ( array : ReadonlyArray < T > , makeKey : ( value : T ) => number , makeValue : ( value : T ) => V ) : V [ ] ;
1278+ export function arrayToNumericMap < T , V > ( array : ReadonlyArray < T > , makeKey : ( value : T ) => number , makeValue ?: ( value : T ) => V ) : V [ ] {
1279+ const result : V [ ] = [ ] ;
12461280 for ( const value of array ) {
1247- result [ makeKey ( value ) ] = value ;
1281+ result [ makeKey ( value ) ] = makeValue ? makeValue ( value ) : value as any as V ;
12481282 }
12491283 return result ;
12501284 }
@@ -1343,6 +1377,12 @@ namespace ts {
13431377 return Array . isArray ? Array . isArray ( value ) : value instanceof Array ;
13441378 }
13451379
1380+ export function toArray < T > ( value : T | ReadonlyArray < T > ) : ReadonlyArray < T > ;
1381+ export function toArray < T > ( value : T | T [ ] ) : T [ ] ;
1382+ export function toArray < T > ( value : T | T [ ] ) : T [ ] {
1383+ return isArray ( value ) ? value : [ value ] ;
1384+ }
1385+
13461386 /**
13471387 * Tests whether a value is string
13481388 */
@@ -1942,7 +1982,7 @@ namespace ts {
19421982 : moduleKind === ModuleKind . System ;
19431983 }
19441984
1945- export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "alwaysStrict" ;
1985+ export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "strictPropertyInitialization" | " alwaysStrict";
19461986
19471987 export function getStrictOptionValue ( compilerOptions : CompilerOptions , flag : StrictOptionName ) : boolean {
19481988 return compilerOptions [ flag ] === undefined ? compilerOptions . strict : compilerOptions [ flag ] ;
@@ -2052,7 +2092,7 @@ namespace ts {
20522092 }
20532093 }
20542094
2055- export function getRelativePathToDirectoryOrUrl ( directoryPathOrUrl : string , relativeOrAbsolutePath : string , currentDirectory : string , getCanonicalFileName : ( fileName : string ) => string , isAbsolutePathAnUrl : boolean ) {
2095+ export function getRelativePathToDirectoryOrUrl ( directoryPathOrUrl : string , relativeOrAbsolutePath : string , currentDirectory : string , getCanonicalFileName : GetCanonicalFileName , isAbsolutePathAnUrl : boolean ) {
20562096 const pathComponents = getNormalizedPathOrUrlComponents ( relativeOrAbsolutePath , currentDirectory ) ;
20572097 const directoryComponents = getNormalizedPathOrUrlComponents ( directoryPathOrUrl , currentDirectory ) ;
20582098 if ( directoryComponents . length > 1 && lastOrUndefined ( directoryComponents ) === "" ) {
@@ -2188,6 +2228,10 @@ namespace ts {
21882228 return expectedPos >= 0 && str . indexOf ( suffix , expectedPos ) === expectedPos ;
21892229 }
21902230
2231+ export function removeSuffix ( str : string , suffix : string ) : string {
2232+ return endsWith ( str , suffix ) ? str . slice ( 0 , str . length - suffix . length ) : str ;
2233+ }
2234+
21912235 export function stringContains ( str : string , substring : string ) : boolean {
21922236 return str . indexOf ( substring ) !== - 1 ;
21932237 }
@@ -2654,6 +2698,17 @@ namespace ts {
26542698 return < T > ( removeFileExtension ( path ) + newExtension ) ;
26552699 }
26562700
2701+ /**
2702+ * Takes a string like "jquery-min.4.2.3" and returns "jquery"
2703+ */
2704+ export function removeMinAndVersionNumbers ( fileName : string ) {
2705+ // Match a "." or "-" followed by a version number or 'min' at the end of the name
2706+ const trailingMinOrVersion = / [ . - ] ( ( m i n ) | ( \d + ( \. \d + ) * ) ) $ / ;
2707+
2708+ // The "min" or version may both be present, in either order, so try applying the above twice.
2709+ return fileName . replace ( trailingMinOrVersion , "" ) . replace ( trailingMinOrVersion , "" ) ;
2710+ }
2711+
26572712 export interface ObjectAllocator {
26582713 getNodeConstructor ( ) : new ( kind : SyntaxKind , pos ?: number , end ?: number ) => Node ;
26592714 getTokenConstructor ( ) : new < TKind extends SyntaxKind > ( kind : TKind , pos ?: number , end ?: number ) => Token < TKind > ;
@@ -2716,6 +2771,12 @@ namespace ts {
27162771 VeryAggressive = 3 ,
27172772 }
27182773
2774+ /**
2775+ * Safer version of `Function` which should not be called.
2776+ * Every function should be assignable to this, but this should not be assignable to every function.
2777+ */
2778+ export type AnyFunction = ( ...args : never [ ] ) => void ;
2779+
27192780 export namespace Debug {
27202781 export let currentAssertionLevel = AssertionLevel . None ;
27212782 export let isDebugging = false ;
@@ -2724,7 +2785,7 @@ namespace ts {
27242785 return currentAssertionLevel >= level ;
27252786 }
27262787
2727- export function assert ( expression : boolean , message ?: string , verboseDebugInfo ?: string | ( ( ) => string ) , stackCrawlMark ?: Function ) : void {
2788+ export function assert ( expression : boolean , message ?: string , verboseDebugInfo ?: string | ( ( ) => string ) , stackCrawlMark ?: AnyFunction ) : void {
27282789 if ( ! expression ) {
27292790 if ( verboseDebugInfo ) {
27302791 message += "\r\nVerbose Debug Information: " + ( typeof verboseDebugInfo === "string" ? verboseDebugInfo : verboseDebugInfo ( ) ) ;
@@ -2758,7 +2819,7 @@ namespace ts {
27582819 }
27592820 }
27602821
2761- export function fail ( message ?: string , stackCrawlMark ?: Function ) : never {
2822+ export function fail ( message ?: string , stackCrawlMark ?: AnyFunction ) : never {
27622823 debugger ;
27632824 const e = new Error ( message ? `Debug Failure. ${ message } ` : "Debug Failure." ) ;
27642825 if ( ( < any > Error ) . captureStackTrace ) {
@@ -2767,11 +2828,11 @@ namespace ts {
27672828 throw e ;
27682829 }
27692830
2770- export function assertNever ( member : never , message ?: string , stackCrawlMark ?: Function ) : never {
2831+ export function assertNever ( member : never , message ?: string , stackCrawlMark ?: AnyFunction ) : never {
27712832 return fail ( message || `Illegal value: ${ member } ` , stackCrawlMark || assertNever ) ;
27722833 }
27732834
2774- export function getFunctionName ( func : Function ) {
2835+ export function getFunctionName ( func : AnyFunction ) {
27752836 if ( typeof func !== "function" ) {
27762837 return "" ;
27772838 }
@@ -2827,7 +2888,8 @@ namespace ts {
28272888 }
28282889 }
28292890
2830- export function createGetCanonicalFileName ( useCaseSensitiveFileNames : boolean ) : ( fileName : string ) => string {
2891+ export type GetCanonicalFileName = ( fileName : string ) => string ;
2892+ export function createGetCanonicalFileName ( useCaseSensitiveFileNames : boolean ) : GetCanonicalFileName {
28312893 return useCaseSensitiveFileNames
28322894 ? ( ( fileName ) => fileName )
28332895 : ( ( fileName ) => fileName . toLowerCase ( ) ) ;
@@ -2854,7 +2916,7 @@ namespace ts {
28542916 return findBestPatternMatch ( patterns , _ => _ , candidate ) ;
28552917 }
28562918
2857- export function patternText ( { prefix, suffix} : Pattern ) : string {
2919+ export function patternText ( { prefix, suffix } : Pattern ) : string {
28582920 return `${ prefix } *${ suffix } ` ;
28592921 }
28602922
@@ -2884,7 +2946,7 @@ namespace ts {
28842946 return matchedValue ;
28852947 }
28862948
2887- function isPatternMatch ( { prefix, suffix} : Pattern , candidate : string ) {
2949+ function isPatternMatch ( { prefix, suffix } : Pattern , candidate : string ) {
28882950 return candidate . length >= prefix . length + suffix . length &&
28892951 startsWith ( candidate , prefix ) &&
28902952 endsWith ( candidate , suffix ) ;
0 commit comments