1// This test case is a condensed version of Angular 2's ListWrapper. Prior to #7448 2// this would cause the compiler to run out of memory. 3 4function outer<T>(x: T) { 5 class Inner { 6 static y: T = x; 7 } 8 return Inner; 9} 10let y: number = outer(5).y; 11 12class ListWrapper2 { 13 static clone<T>(dit: typeof ListWrapper2, array: T[]): T[] { return array.slice(0); } 14 static reversed<T>(dit: typeof ListWrapper2, array: T[]): T[] { 15 var a = ListWrapper2.clone(dit, array); 16 return a; 17 } 18} 19namespace tessst { 20 /** 21 * Iterates through 'array' by index and performs the callback on each element of array until the callback 22 * returns a truthy value, then returns that value. 23 * If no such value is found, the callback is applied to each element of array and undefined is returned. 24 */ 25 export function funkyFor<T, U>(array: T[], callback: (element: T, index: number) => U): U { 26 if (array) { 27 for (let i = 0, len = array.length; i < len; i++) { 28 const result = callback(array[i], i); 29 if (result) { 30 return result; 31 } 32 } 33 } 34 return undefined; 35 } 36} 37interface Scanner { 38 scanRange<T>(start: number, length: number, callback: () => T): T; 39} 40class ListWrapper { 41 // JS has no way to express a statically fixed size list, but dart does so we 42 // keep both methods. 43 static createFixedSize(dit: typeof ListWrapper, size: number): any[] { return new Array(size); } 44 static createGrowableSize(dit: typeof ListWrapper, size: number): any[] { return new Array(size); } 45 static clone<T>(dit: typeof ListWrapper, array: T[]): T[] { return array.slice(0); } 46 static forEachWithIndex<T>(dit: typeof ListWrapper, array: T[], fn: (t: T, n: number) => void) { 47 for (var i = 0; i < array.length; i++) { 48 fn(array[i], i); 49 } 50 } 51 static first<T>(dit: typeof ListWrapper, array: T[]): T { 52 if (!array) return null; 53 return array[0]; 54 } 55 static last<T>(dit: typeof ListWrapper, array: T[]): T { 56 if (!array || array.length == 0) return null; 57 return array[array.length - 1]; 58 } 59 static indexOf<T>(dit: typeof ListWrapper, array: T[], value: T, startIndex: number = 0): number { 60 return array.indexOf(value, startIndex); 61 } 62 static contains<T>(dit: typeof ListWrapper, list: T[], el: T): boolean { return list.indexOf(el) !== -1; } 63 static reversed<T>(dit: typeof ListWrapper, array: T[]): T[] { 64 var a = ListWrapper.clone(dit, array); 65 let scanner: Scanner; 66 scanner.scanRange(3, 5, () => { }); 67 return tessst.funkyFor(array, t => t.toString()) ? a.reverse() : a; 68 } 69 static concat(dit: typeof ListWrapper, a: any[], b: any[]): any[] { return a.concat(b); } 70 static insert<T>(dit: typeof ListWrapper, list: T[], index: number, value: T) { list.splice(index, 0, value); } 71 static removeAt<T>(dit: typeof ListWrapper, list: T[], index: number): T { 72 var res = list[index]; 73 list.splice(index, 1); 74 return res; 75 } 76 static removeAll<T>(dit: typeof ListWrapper, list: T[], items: T[]) { 77 for (var i = 0; i < items.length; ++i) { 78 var index = list.indexOf(items[i]); 79 list.splice(index, 1); 80 } 81 } 82 static remove<T>(dit: typeof ListWrapper, list: T[], el: T): boolean { 83 var index = list.indexOf(el); 84 if (index > -1) { 85 list.splice(index, 1); 86 return true; 87 } 88 return false; 89 } 90 static clear(dit: typeof ListWrapper, list: any[]) { list.length = 0; } 91 static isEmpty(dit: typeof ListWrapper, list: any[]): boolean { return list.length == 0; } 92 static fill(dit: typeof ListWrapper, list: any[], value: any, start: number = 0, end: number = null) { 93 list.fill(value, start, end === null ? list.length : end); 94 } 95 static equals(dit: typeof ListWrapper, a: any[], b: any[]): boolean { 96 if (a.length != b.length) return false; 97 for (var i = 0; i < a.length; ++i) { 98 if (a[i] !== b[i]) return false; 99 } 100 return true; 101 } 102 static slice<T>(dit: typeof ListWrapper, l: T[], from: number = 0, to: number = null): T[] { 103 return l.slice(from, to === null ? undefined : to); 104 } 105 static splice<T>(dit: typeof ListWrapper, l: T[], from: number, length: number): T[] { return l.splice(from, length); } 106 static sort<T>(dit: typeof ListWrapper, l: T[], compareFn?: (a: T, b: T) => number) { 107 if (isPresent(compareFn)) { 108 l.sort(compareFn); 109 } else { 110 l.sort(); 111 } 112 } 113 static toString<T>(dit: typeof ListWrapper, l: T[]): string { return l.toString(); } 114 static toJSON<T>(dit: typeof ListWrapper, l: T[]): string { return JSON.stringify(l); } 115 116 static maximum<T>(dit: typeof ListWrapper, list: T[], predicate: (t: T) => number): T { 117 if (list.length == 0) { 118 return null; 119 } 120 var solution: T = null; 121 var maxValue = -Infinity; 122 for (var index = 0; index < list.length; index++) { 123 var candidate = list[index]; 124 if (isBlank(candidate)) { 125 continue; 126 } 127 var candidateValue = predicate(candidate); 128 if (candidateValue > maxValue) { 129 solution = candidate; 130 maxValue = candidateValue; 131 } 132 } 133 return solution; 134 } 135} 136let cloned = ListWrapper.clone(ListWrapper, [1,2,3,4]); 137declare function isBlank(x: any): boolean; 138declare function isPresent<T>(compareFn?: (a: T, b: T) => number): boolean; 139interface Array<T> { 140 fill(value: any, start: number, end: number): void; 141}