Skip to content

Commit 6ac80ba

Browse files
When augmenting Array.prototype, make the functions non-enumerable by default so as to not affect clients that call for-in
1 parent 33cee0c commit 6ac80ba

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/services/syntax/syntaxList.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,44 @@ module TypeScript.Syntax {
2525
// Debug.assert(!separators || separators.length === 0);
2626
}
2727

28-
Array.prototype.kind = function () {
29-
return this.separators === undefined ? SyntaxKind.List : SyntaxKind.SeparatedList;
28+
function addArrayFunction(name: string, func: Function) {
29+
if (Object.defineProperty) {
30+
Object.defineProperty(Array.prototype, name, { value: func, writable: true });
31+
}
32+
else {
33+
(<any>Array.prototype)[name] = func;
34+
}
3035
}
3136

32-
Array.prototype.separatorCount = function (): number {
37+
addArrayFunction("kind", function () {
38+
return this.separators === undefined ? SyntaxKind.List : SyntaxKind.SeparatedList;
39+
});
40+
41+
addArrayFunction("childCount", function (): number {
42+
return this.separators ? this.separatedListLength : this.length;
43+
});
44+
45+
addArrayFunction("childAt", function (index: number): ISyntaxNodeOrToken {
46+
if (this.separators) {
47+
return index % 2 === 0 ? this[index >> 1] : this.separators[index >> 1];
48+
}
49+
else {
50+
return this[index];
51+
}
52+
});
53+
54+
addArrayFunction("separatorCount", function (): number {
3355
assertEmptyLists();
3456
// Debug.assert(this.kind === SyntaxKind.SeparatedList);
3557
return this.separators.length;
36-
}
58+
});
3759

38-
Array.prototype.separatorAt = function (index: number): ISyntaxToken {
60+
addArrayFunction("separatorAt", function (index: number): ISyntaxToken {
3961
assertEmptyLists();
4062
// Debug.assert(this.kind === SyntaxKind.SeparatedList);
4163
// Debug.assert(index >= 0 && index < this.separators.length);
4264
return this.separators[index];
43-
}
65+
});
4466

4567
export function emptyList<T extends ISyntaxNodeOrToken>(): T[] {
4668
return <T[]><any>_emptyList;

0 commit comments

Comments
 (0)