Skip to content

Commit 3a95f92

Browse files
authored
Merge pull request microsoft#11874 from Microsoft/emitHelper
Move emit helpers into related transformers
2 parents a5d9e96 + 9a1a605 commit 3a95f92

19 files changed

+1549
-1412
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19632,7 +19632,7 @@ namespace ts {
1963219632

1963319633
function isNameOfModuleOrEnumDeclaration(node: Identifier) {
1963419634
const parent = node.parent;
19635-
return isModuleOrEnumDeclaration(parent) && node === parent.name;
19635+
return parent && isModuleOrEnumDeclaration(parent) && node === parent.name;
1963619636
}
1963719637

1963819638
// When resolved as an expression identifier, if the given node references an exported entity, return the declaration

src/compiler/core.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ namespace ts {
571571
*/
572572
export function append<T>(to: T[] | undefined, value: T | undefined): T[] | undefined {
573573
if (value === undefined) return to;
574-
if (to === undefined) to = [];
574+
if (to === undefined) return [value];
575575
to.push(value);
576576
return to;
577577
}
@@ -592,6 +592,16 @@ namespace ts {
592592
return to;
593593
}
594594

595+
/**
596+
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
597+
*/
598+
export function stableSort<T>(array: T[], comparer: (x: T, y: T) => Comparison = compareValues) {
599+
return array
600+
.map((_, i) => i) // create array of indices
601+
.sort((x, y) => comparer(array[x], array[y]) || compareValues(x, y)) // sort indices by value then position
602+
.map(i => array[i]); // get sorted array
603+
}
604+
595605
export function rangeEquals<T>(array1: T[], array2: T[], pos: number, end: number) {
596606
while (pos < end) {
597607
if (array1[pos] !== array2[pos]) {
@@ -2099,6 +2109,17 @@ namespace ts {
20992109
}
21002110

21012111
/** Remove an item from an array, moving everything to its right one space left. */
2112+
export function orderedRemoveItem<T>(array: T[], item: T): boolean {
2113+
for (let i = 0; i < array.length; i++) {
2114+
if (array[i] === item) {
2115+
orderedRemoveItemAt(array, i);
2116+
return true;
2117+
}
2118+
}
2119+
return false;
2120+
}
2121+
2122+
/** Remove an item by index from an array, moving everything to its right one space left. */
21022123
export function orderedRemoveItemAt<T>(array: T[], index: number): void {
21032124
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
21042125
for (let i = index; i < array.length - 1; i++) {

src/compiler/emitter.ts

+52-284
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)