Merge pull request #1022 from Microsoft/defineProperty

When augmenting Array.prototype, make the functions non-enumerable by de...
This commit is contained in:
CyrusNajmabadi
2014-10-31 18:55:34 -07:00
3 changed files with 3308 additions and 17 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -3,6 +3,7 @@
interface Array<T> {
data: number;
separators?: TypeScript.ISyntaxToken[];
separatedListLength?: number;
kind(): TypeScript.SyntaxKind;
parent: TypeScript.ISyntaxElement;
@@ -15,10 +16,6 @@ interface Array<T> {
}
module TypeScript {
export function separatedListChildCount(list: ISyntaxNodeOrToken[]) {
return list.length + list.separators.length;
}
export function separatedListChildAt(list: ISyntaxNodeOrToken[], index: number) {
return index % 2 === 0 ? list[index >> 1] : list.separators[index >> 1];
}
@@ -31,6 +28,7 @@ module TypeScript.Syntax {
var _emptySeparators: ISyntaxToken[] = [];
_emptySeparatedList.separators = _emptySeparators;
_emptySeparatedList.separatedListLength = 0;
function assertEmptyLists() {
// Debug.assert(_emptyList.length === 0);
@@ -38,35 +36,44 @@ module TypeScript.Syntax {
// Debug.assert(!separators || separators.length === 0);
}
Array.prototype.kind = function () {
function addArrayFunction(name: string, func: Function) {
if (Object.defineProperty) {
Object.defineProperty(Array.prototype, name, { value: func, writable: true });
}
else {
(<any>Array.prototype)[name] = func;
}
}
addArrayFunction("kind", function () {
return this.separators === undefined ? SyntaxKind.List : SyntaxKind.SeparatedList;
}
});
Array.prototype.childCount = function (): number {
return this.separators ? separatedListChildCount(this) : this.length;
}
addArrayFunction("childCount", function (): number {
return this.separators ? this.separatedListLength : this.length;
});
Array.prototype.childAt = function (index: number): ISyntaxNodeOrToken {
addArrayFunction("childAt", function (index: number): ISyntaxNodeOrToken {
if (this.separators) {
return index % 2 === 0 ? this[index >> 1] : this.separators[index >> 1];
}
else {
return this[index];
}
}
});
Array.prototype.separatorCount = function (): number {
addArrayFunction("separatorCount", function (): number {
assertEmptyLists();
// Debug.assert(this.kind === SyntaxKind.SeparatedList);
return this.separators.length;
}
});
Array.prototype.separatorAt = function (index: number): ISyntaxToken {
addArrayFunction("separatorAt", function (index: number): ISyntaxToken {
assertEmptyLists();
// Debug.assert(this.kind === SyntaxKind.SeparatedList);
// Debug.assert(index >= 0 && index < this.separators.length);
return this.separators[index];
}
});
export function emptyList<T extends ISyntaxNodeOrToken>(): T[] {
return <T[]><any>_emptyList;
@@ -103,8 +110,8 @@ module TypeScript.Syntax {
separators[i].parent = nodes;
}
nodes.separators = separators.length === 0 ? _emptySeparators : separators;
nodes.separatedListLength = nodes.length + separators.length;
return nodes;
}

View File

@@ -28,7 +28,7 @@ module TypeScript {
}
public visitSeparatedList(list: ISyntaxNodeOrToken[]): void {
for (var i = 0, n = separatedListChildCount(list); i < n; i++) {
for (var i = 0, n = list.separatedListLength; i < n; i++) {
if (i % 2 === 0) {
list[i >> 1].accept(this);
}