Skip to content

Move emit helpers into related transformers #11874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 16, 2016
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19630,7 +19630,7 @@ namespace ts {

function isNameOfModuleOrEnumDeclaration(node: Identifier) {
const parent = node.parent;
return isModuleOrEnumDeclaration(parent) && node === parent.name;
return parent && isModuleOrEnumDeclaration(parent) && node === parent.name;
}

// When resolved as an expression identifier, if the given node references an exported entity, return the declaration
Expand Down
23 changes: 22 additions & 1 deletion src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ namespace ts {
*/
export function append<T>(to: T[] | undefined, value: T | undefined): T[] | undefined {
if (value === undefined) return to;
if (to === undefined) to = [];
if (to === undefined) return [value];
to.push(value);
return to;
}
Expand All @@ -592,6 +592,16 @@ namespace ts {
return to;
}

/**
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
*/
export function stableSort<T>(array: T[], comparer: (x: T, y: T) => Comparison = compareValues) {
return array
.map((_, i) => i) // create array of indices
.sort((x, y) => comparer(array[x], array[y]) || compareValues(x, y)) // sort indices by value then position
.map(i => array[i]); // get sorted array
}

export function rangeEquals<T>(array1: T[], array2: T[], pos: number, end: number) {
while (pos < end) {
if (array1[pos] !== array2[pos]) {
Expand Down Expand Up @@ -2099,6 +2109,17 @@ namespace ts {
}

/** Remove an item from an array, moving everything to its right one space left. */
export function orderedRemoveItem<T>(array: T[], item: T): boolean {
for (let i = 0; i < array.length; i++) {
if (array[i] === item) {
orderedRemoveItemAt(array, i);
return true;
}
}
return false;
}

/** Remove an item by index from an array, moving everything to its right one space left. */
export function orderedRemoveItemAt<T>(array: T[], index: number): void {
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
for (let i = index; i < array.length - 1; i++) {
Expand Down
336 changes: 52 additions & 284 deletions src/compiler/emitter.ts

Large diffs are not rendered by default.

Loading