Skip to content

Commit 544d0a4

Browse files
committed
Incorporate changes proposed by @sandersn
1 parent ff828fd commit 544d0a4

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/compiler/checker.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,24 +2142,25 @@ namespace ts {
21422142
return type.flags & TypeFlags.StringLiteral ? `"${escapeString((<LiteralType>type).text)}"` : (<LiteralType>type).text;
21432143
}
21442144

2145-
function getSymbolDisplayBuilder(): SymbolDisplayBuilder {
21462145

2147-
function getNameOfSymbol(symbol: Symbol): string {
2148-
if (symbol.declarations && symbol.declarations.length) {
2149-
const declaration = symbol.declarations[0];
2150-
if (declaration.name) {
2151-
return declarationNameToString(declaration.name);
2152-
}
2153-
switch (declaration.kind) {
2154-
case SyntaxKind.ClassExpression:
2155-
return "(Anonymous class)";
2156-
case SyntaxKind.FunctionExpression:
2157-
case SyntaxKind.ArrowFunction:
2158-
return "(Anonymous function)";
2159-
}
2146+
function getNameOfSymbol(symbol: Symbol): string {
2147+
if (symbol.declarations && symbol.declarations.length) {
2148+
const declaration = symbol.declarations[0];
2149+
if (declaration.name) {
2150+
return declarationNameToString(declaration.name);
2151+
}
2152+
switch (declaration.kind) {
2153+
case SyntaxKind.ClassExpression:
2154+
return "(Anonymous class)";
2155+
case SyntaxKind.FunctionExpression:
2156+
case SyntaxKind.ArrowFunction:
2157+
return "(Anonymous function)";
21602158
}
2161-
return symbol.name;
21622159
}
2160+
return symbol.name;
2161+
}
2162+
2163+
function getSymbolDisplayBuilder(): SymbolDisplayBuilder {
21632164

21642165
/**
21652166
* Writes only the name of the symbol out to the writer. Uses the original source text
@@ -15647,20 +15648,27 @@ namespace ts {
1564715648
}
1564815649
}
1564915650

15650-
// Static members may conflict with non-configurable non-writable built-in Function object properties
15651-
// see https://github.com/microsoft/typescript/issues/442.
15651+
/**
15652+
* Static members being set on a constructor function may conflict with built-in Function
15653+
* object properties. Esp. in ECMAScript 5 there are non-configurable and non-writable
15654+
* built-in properties. This check issues a transpile error when a class has a static
15655+
* member with the same name as a non-writable built-in property.
15656+
*
15657+
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3
15658+
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5
15659+
* @see http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor
15660+
* @see http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances
15661+
*/
1565215662
function checkClassForStaticPropertyNameConflicts(node: ClassLikeDeclaration) {
1565315663
const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1;
15654-
const className = getSymbolOfNode(node).name;
15664+
const className = getNameOfSymbol(getSymbolOfNode(node));
1565515665
for (const member of node.members) {
15656-
const isStatic = forEach(member.modifiers, (m: Modifier) => m.kind === SyntaxKind.StaticKeyword);
15666+
const isStatic = getModifierFlags(member) & ModifierFlags.Static;
1565715667
const isMethod = member.kind === SyntaxKind.MethodDeclaration;
1565815668
const memberNameNode = member.name;
1565915669
if (isStatic && memberNameNode) {
1566015670
const memberName = getPropertyNameForPropertyNameNode(memberNameNode);
1566115671
if (languageVersion <= ScriptTarget.ES5) { // ES3, ES5
15662-
// see also http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3
15663-
// see also http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5
1566415672
if (memberName === "prototype" ||
1566515673
memberName === "name" ||
1566615674
memberName === "length" ||
@@ -15671,8 +15679,6 @@ namespace ts {
1567115679
}
1567215680
}
1567315681
else { // ES6+
15674-
// see also http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor
15675-
// see also http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances
1567615682
if (memberName === "prototype") {
1567715683
error(memberNameNode, message, memberName, className);
1567815684
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,14 +2011,14 @@
20112011
"category": "Error",
20122012
"code": 2697
20132013
},
2014-
"Static property '{0}' conflicts with built-in property 'Function.{0}' of constructor function '{1}'.": {
2015-
"category": "Error",
2016-
"code": 2699
2017-
},
20182014
"Spread types may only be created from object types.": {
20192015
"category": "Error",
20202016
"code": 2698
20212017
},
2018+
"Static property '{0}' conflicts with built-in property 'Function.{0}' of constructor function '{1}'.": {
2019+
"category": "Error",
2020+
"code": 2699
2021+
},
20222022
"Rest types may only be created from object types.": {
20232023
"category": "Error",
20242024
"code": 2700

0 commit comments

Comments
 (0)