Skip to content

Commit d9a46e1

Browse files
committed
Allowing static arguments() and static caller() for target "es6".
Disallow non-function properties `static arguments` and `static caller`, though.
1 parent d58b5c8 commit d9a46e1

File tree

4 files changed

+38
-50
lines changed

4 files changed

+38
-50
lines changed

src/compiler/checker.ts

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14764,49 +14764,43 @@ namespace ts {
1476414764
}
1476514765
}
1476614766

14767-
// Static members may conflict with non-configurable non-writable built-in Function.prototype properties
14767+
// Static members may conflict with non-configurable non-writable built-in Function object properties
1476814768
// see https://github.com/microsoft/typescript/issues/442.
1476914769
function checkClassForStaticPropertyNameConflicts(node: ClassLikeDeclaration) {
14770-
const es5_descriptors: PropertyDescriptorMap = {
14771-
// see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3
14772-
// see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5
14773-
"name": { configurable: false, writable: false },
14774-
"length": { configurable: false, writable: false },
14775-
"prototype": { configurable: false, writable: false },
14776-
14777-
// see https://github.com/microsoft/typescript/issues/442
14778-
"caller": { configurable: false, writable: false },
14779-
"arguments": { configurable: false, writable: false }
14780-
};
14781-
const post_es5_descriptors: PropertyDescriptorMap = {
14782-
// see http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor
14783-
// see http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances
14784-
"name": { configurable: true, writable: false },
14785-
"length": { configurable: true, writable: false },
14786-
"prototype": { configurable: false, writable: false },
14787-
"caller": { configurable: false, writable: false },
14788-
"arguments": { configurable: false, writable: false }
14789-
};
1479014770
const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1;
1479114771
const className = getSymbolOfNode(node).name;
14792-
1479314772
for (const member of node.members) {
14794-
const isStatic = forEach(member.modifiers, (m: Modifier) => m.kind === SyntaxKind.StaticKeyword);
14795-
if (isStatic) {
14796-
const memberNameNode = member.name;
14797-
if (memberNameNode) {
14798-
const memberName = getPropertyNameForPropertyNameNode(memberNameNode);
14799-
let descriptor: PropertyDescriptor = undefined;
14800-
if (languageVersion <= ScriptTarget.ES5) {
14801-
descriptor = es5_descriptors.hasOwnProperty(memberName) ? es5_descriptors[memberName] : undefined;
14802-
}
14803-
else if (languageVersion > ScriptTarget.ES5) {
14804-
descriptor = post_es5_descriptors.hasOwnProperty(memberName) ? post_es5_descriptors[memberName] : undefined;
14805-
}
14806-
if (descriptor && descriptor.configurable === false && descriptor.writable === false) {
14773+
const isStatic = forEach(member.modifiers, (m: Modifier) => m.kind === SyntaxKind.StaticKeyword);
14774+
const isMethod = member.kind === SyntaxKind.MethodDeclaration;
14775+
const memberNameNode = member.name;
14776+
if (isStatic && memberNameNode) {
14777+
const memberName = getPropertyNameForPropertyNameNode(memberNameNode);
14778+
if (languageVersion <= ScriptTarget.ES5) { // ES3, ES5
14779+
// see also http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3
14780+
// see also http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5
14781+
if (memberName === "prototype" ||
14782+
memberName === "name" ||
14783+
memberName === "length" ||
14784+
memberName === "caller" ||
14785+
memberName === "arguments"
14786+
) {
1480714787
error(memberNameNode, message, memberName, className);
1480814788
}
1480914789
}
14790+
else { // ES6+
14791+
// see also http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor
14792+
// see also http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances
14793+
if (memberName === "prototype") {
14794+
error(memberNameNode, message, memberName, className);
14795+
}
14796+
else if ((
14797+
memberName === "caller" ||
14798+
memberName === "arguments" ) &&
14799+
isMethod === false
14800+
) {
14801+
error(memberNameNode, message, memberName, className);
14802+
}
14803+
}
1481014804
}
1481114805
}
1481214806
}

tests/baselines/reference/staticPropertyNameConflictsEs6.errors.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameCon
22
tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(31,12): error TS2300: Duplicate identifier 'prototype'.
33
tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(31,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'.
44
tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(37,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'.
5-
tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(42,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'.
65
tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(48,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'.
7-
tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(53,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArgumentsFn'.
86

97

10-
==== tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts (7 errors) ====
8+
==== tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts (5 errors) ====
119

1210

1311
class StaticName {
@@ -57,9 +55,7 @@ tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameCon
5755
}
5856

5957
class StaticCallerFn {
60-
static caller() {} // error
61-
~~~~~~
62-
!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'.
58+
static caller() {} // ok
6359
caller() {} // ok
6460
}
6561

@@ -72,9 +68,7 @@ tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameCon
7268
}
7369

7470
class StaticArgumentsFn {
75-
static arguments() {} // error
76-
~~~~~~~~~
77-
!!! error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArgumentsFn'.
71+
static arguments() {} // ok
7872
arguments() {} // ok
7973
}
8074

tests/baselines/reference/staticPropertyNameConflictsEs6.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class StaticCaller {
4040
}
4141

4242
class StaticCallerFn {
43-
static caller() {} // error
43+
static caller() {} // ok
4444
caller() {} // ok
4545
}
4646

@@ -51,7 +51,7 @@ class StaticArguments {
5151
}
5252

5353
class StaticArgumentsFn {
54-
static arguments() {} // error
54+
static arguments() {} // ok
5555
arguments() {} // ok
5656
}
5757

@@ -78,12 +78,12 @@ class StaticPrototypeFn {
7878
class StaticCaller {
7979
}
8080
class StaticCallerFn {
81-
static caller() { } // error
81+
static caller() { } // ok
8282
caller() { } // ok
8383
}
8484
class StaticArguments {
8585
}
8686
class StaticArgumentsFn {
87-
static arguments() { } // error
87+
static arguments() { } // ok
8888
arguments() { } // ok
8989
}

tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class StaticCaller {
4040
}
4141

4242
class StaticCallerFn {
43-
static caller() {} // error
43+
static caller() {} // ok
4444
caller() {} // ok
4545
}
4646

@@ -51,6 +51,6 @@ class StaticArguments {
5151
}
5252

5353
class StaticArgumentsFn {
54-
static arguments() {} // error
54+
static arguments() {} // ok
5555
arguments() {} // ok
5656
}

0 commit comments

Comments
 (0)