Skip to content

Commit 4cd2d3f

Browse files
committed
Report privacy errors on return types
1 parent 4115077 commit 4cd2d3f

13 files changed

+5036
-51
lines changed

Diff for: src/compiler/diagnosticInformationMap.generated.ts

+14
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ module ts {
134134
Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_private_module_2: { code: 2047, category: DiagnosticCategory.Error, key: "Parameter '{0}' of public method from exported class has or is using name '{1}' from private module '{2}'." },
135135
Parameter_0_of_method_from_exported_interface_has_or_is_using_name_1_from_private_module_2: { code: 2048, category: DiagnosticCategory.Error, key: "Parameter '{0}' of method from exported interface has or is using name '{1}' from private module '{2}'." },
136136
Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2: { code: 2049, category: DiagnosticCategory.Error, key: "Parameter '{0}' of exported function has or is using name '{1}' from private module '{2}'." },
137+
Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_0: { code: 2052, category: DiagnosticCategory.Error, key: "Return type of constructor signature from exported interface has or is using private name '{0}'." },
138+
Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_name_0: { code: 2053, category: DiagnosticCategory.Error, key: "Return type of call signature from exported interface has or is using private name '{0}'." },
139+
Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_name_0: { code: 2054, category: DiagnosticCategory.Error, key: "Return type of index signature from exported interface has or is using private name '{0}'." },
140+
Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_name_0: { code: 2055, category: DiagnosticCategory.Error, key: "Return type of public static method from exported class has or is using private name '{0}'." },
141+
Return_type_of_public_method_from_exported_class_has_or_is_using_private_name_0: { code: 2056, category: DiagnosticCategory.Error, key: "Return type of public method from exported class has or is using private name '{0}'." },
142+
Return_type_of_method_from_exported_interface_has_or_is_using_private_name_0: { code: 2057, category: DiagnosticCategory.Error, key: "Return type of method from exported interface has or is using private name '{0}'." },
143+
Return_type_of_exported_function_has_or_is_using_private_name_0: { code: 2058, category: DiagnosticCategory.Error, key: "Return type of exported function has or is using private name '{0}'." },
144+
Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1: { code: 2061, category: DiagnosticCategory.Error, key: "Return type of constructor signature from exported interface has or is using name '{0}' from private module '{1}'." },
145+
Return_type_of_call_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1: { code: 2062, category: DiagnosticCategory.Error, key: "Return type of call signature from exported interface has or is using name '{0}' from private module '{1}'." },
146+
Return_type_of_index_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1: { code: 2063, category: DiagnosticCategory.Error, key: "Return type of index signature from exported interface has or is using name '{0}' from private module '{1}'." },
147+
Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_private_module_1: { code: 2064, category: DiagnosticCategory.Error, key: "Return type of public static method from exported class has or is using name '{0}' from private module '{1}'." },
148+
Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_private_module_1: { code: 2065, category: DiagnosticCategory.Error, key: "Return type of public method from exported class has or is using name '{0}' from private module '{1}'." },
149+
Return_type_of_method_from_exported_interface_has_or_is_using_name_0_from_private_module_1: { code: 2066, category: DiagnosticCategory.Error, key: "Return type of method from exported interface has or is using name '{0}' from private module '{1}'." },
150+
Return_type_of_exported_function_has_or_is_using_name_0_from_private_module_1: { code: 2067, category: DiagnosticCategory.Error, key: "Return type of exported function has or is using name '{0}' from private module '{1}'." },
137151
Type_parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1: { code: 2208, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of constructor signature from exported interface has or is using private name '{1}'." },
138152
Type_parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_name_1: { code: 2209, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of call signature from exported interface has or is using private name '{1}'." },
139153
Type_parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_name_1: { code: 2210, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of public static method from exported class has or is using private name '{1}'." },

Diff for: src/compiler/diagnosticMessages.json

+56
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,62 @@
528528
"category": "Error",
529529
"code": 2049
530530
},
531+
"Return type of constructor signature from exported interface has or is using private name '{0}'.": {
532+
"category": "Error",
533+
"code": 2052
534+
},
535+
"Return type of call signature from exported interface has or is using private name '{0}'.": {
536+
"category": "Error",
537+
"code": 2053
538+
},
539+
"Return type of index signature from exported interface has or is using private name '{0}'.": {
540+
"category": "Error",
541+
"code": 2054
542+
},
543+
"Return type of public static method from exported class has or is using private name '{0}'.": {
544+
"category": "Error",
545+
"code": 2055
546+
},
547+
"Return type of public method from exported class has or is using private name '{0}'.": {
548+
"category": "Error",
549+
"code": 2056
550+
},
551+
"Return type of method from exported interface has or is using private name '{0}'.": {
552+
"category": "Error",
553+
"code": 2057
554+
},
555+
"Return type of exported function has or is using private name '{0}'.": {
556+
"category": "Error",
557+
"code": 2058
558+
},
559+
"Return type of constructor signature from exported interface has or is using name '{0}' from private module '{1}'.": {
560+
"category": "Error",
561+
"code": 2061
562+
},
563+
"Return type of call signature from exported interface has or is using name '{0}' from private module '{1}'.": {
564+
"category": "Error",
565+
"code": 2062
566+
},
567+
"Return type of index signature from exported interface has or is using name '{0}' from private module '{1}'.": {
568+
"category": "Error",
569+
"code": 2063
570+
},
571+
"Return type of public static method from exported class has or is using name '{0}' from private module '{1}'.": {
572+
"category": "Error",
573+
"code": 2064
574+
},
575+
"Return type of public method from exported class has or is using name '{0}' from private module '{1}'.": {
576+
"category": "Error",
577+
"code": 2065
578+
},
579+
"Return type of method from exported interface has or is using name '{0}' from private module '{1}'.": {
580+
"category": "Error",
581+
"code": 2066
582+
},
583+
"Return type of exported function has or is using name '{0}' from private module '{1}'.": {
584+
"category": "Error",
585+
"code": 2067
586+
},
531587
"Type parameter '{0}' of constructor signature from exported interface has or is using private name '{1}'.": {
532588
"category": "Error",
533589
"code": 2208

Diff for: src/compiler/emitter.ts

+74-6
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ module ts {
18791879
var getSymbolVisibilityDiagnosticMessage: (symbolAccesibilityResult: SymbolAccessiblityResult) => {
18801880
errorNode: Node;
18811881
diagnosticMessage: DiagnosticMessage;
1882-
typeName: Identifier
1882+
typeName?: Identifier
18831883
}
18841884

18851885
function writeSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) {
@@ -1909,11 +1909,19 @@ module ts {
19091909
reportedDeclarationError = true;
19101910
var errorInfo = getSymbolVisibilityDiagnosticMessage(symbolAccesibilityResult);
19111911
if (errorInfo) {
1912-
diagnostics.push(createDiagnosticForNode(errorInfo.errorNode,
1913-
errorInfo.diagnosticMessage,
1914-
getSourceTextOfLocalNode(errorInfo.typeName),
1915-
symbolAccesibilityResult.errorSymbolName,
1916-
symbolAccesibilityResult.errorModuleName));
1912+
if (errorInfo.typeName) {
1913+
diagnostics.push(createDiagnosticForNode(errorInfo.errorNode,
1914+
errorInfo.diagnosticMessage,
1915+
getSourceTextOfLocalNode(errorInfo.typeName),
1916+
symbolAccesibilityResult.errorSymbolName,
1917+
symbolAccesibilityResult.errorModuleName));
1918+
}
1919+
else {
1920+
diagnostics.push(createDiagnosticForNode(errorInfo.errorNode,
1921+
errorInfo.diagnosticMessage,
1922+
symbolAccesibilityResult.errorSymbolName,
1923+
symbolAccesibilityResult.errorModuleName));
1924+
}
19171925
}
19181926
}
19191927
}
@@ -2394,10 +2402,70 @@ module ts {
23942402
// If this is not a constructor and is not private, emit the return type
23952403
if (node.kind !== SyntaxKind.Constructor && !(node.flags & NodeFlags.Private)) {
23962404
write(": ");
2405+
getSymbolVisibilityDiagnosticMessage = getReturnTypeVisibilityError;
23972406
resolver.writeReturnTypeOfSignatureDeclaration(node, enclosingDeclaration, TypeFormatFlags.None, writer);
2407+
// TODO(shkamat) This is just till we get rest of the error reporting up
2408+
getSymbolVisibilityDiagnosticMessage = undefined;
23982409
}
23992410
write(";");
24002411
writeLine();
2412+
2413+
function getReturnTypeVisibilityError(symbolAccesibilityResult: SymbolAccessiblityResult) {
2414+
// TODO(shkamat) Cannot access name errors
2415+
var diagnosticMessage: DiagnosticMessage;
2416+
switch (node.kind) {
2417+
case SyntaxKind.ConstructSignature:
2418+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2419+
Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 :
2420+
Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_0;
2421+
break;
2422+
2423+
case SyntaxKind.CallSignature:
2424+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2425+
Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 :
2426+
Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_name_0;
2427+
break;
2428+
2429+
case SyntaxKind.IndexSignature:
2430+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2431+
Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 :
2432+
Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_name_0;
2433+
break;
2434+
2435+
case SyntaxKind.Method:
2436+
if (node.flags & NodeFlags.Static) {
2437+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2438+
Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 :
2439+
Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_name_0;
2440+
}
2441+
else if (node.parent.kind === SyntaxKind.ClassDeclaration) {
2442+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2443+
Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 :
2444+
Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_private_name_0;
2445+
}
2446+
else {
2447+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2448+
Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_name_0_from_private_module_1 :
2449+
Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_private_name_0;
2450+
}
2451+
break;
2452+
2453+
case SyntaxKind.FunctionDeclaration:
2454+
diagnosticMessage = symbolAccesibilityResult.errorModuleName ?
2455+
Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_private_module_1 :
2456+
Diagnostics.Return_type_of_exported_function_has_or_is_using_private_name_0;
2457+
break;
2458+
2459+
default:
2460+
Debug.fail("This is unknown kind for signature: " + SyntaxKind[node.kind]);
2461+
}
2462+
2463+
return {
2464+
diagnosticMessage: diagnosticMessage,
2465+
errorNode: <Node>node.name || node,
2466+
};
2467+
}
2468+
24012469
}
24022470

24032471
function emitParameterDeclaration(node: ParameterDeclaration) {

Diff for: tests/baselines/reference/declInput-2.errors.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
==== tests/cases/compiler/declInput-2.ts (3 errors) ====
1+
==== tests/cases/compiler/declInput-2.ts (5 errors) ====
22
module M {
33
class C { }
44
export class E {}
@@ -19,10 +19,14 @@
1919
public m232(): E { return null;}
2020
public m242(): I1 { return null; }
2121
public m252(): I2 { return null; } // don't generate
22+
~~~~
23+
!!! Return type of public method from exported class has or is using private name 'I2'.
2224
public m26(i:I1) {}
2325
public m262(i:I2) {}
2426
~~~~
2527
!!! Parameter 'i' of public method from exported class has or is using private name 'I2'.
2628
public m3():C { return new C(); }
29+
~~
30+
!!! Return type of public method from exported class has or is using private name 'C'.
2731
}
2832
}

Diff for: tests/baselines/reference/exportImport.errors.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
==== tests/cases/compiler/consumer.ts (1 errors) ====
2+
import e = require('./exporter');
3+
4+
export function w(): e.w { // Should be OK
5+
~
6+
!!! Return type of exported function has or is using private name 'Widget1'.
7+
return new e.w();
8+
}
9+
==== tests/cases/compiler/w1.ts (0 errors) ====
10+
11+
export = Widget1
12+
class Widget1 { name = 'one'; }
13+
14+
==== tests/cases/compiler/exporter.ts (0 errors) ====
15+
export import w = require('./w1');
16+

Diff for: tests/baselines/reference/exportImport.js

-21
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,3 @@ declare class Widget1 {
4545
}
4646
//// [exporter.d.ts]
4747
export import w = require('./w1');
48-
//// [consumer.d.ts]
49-
export declare function w(): Widget1;
50-
51-
52-
//// [DtsFileErrors]
53-
54-
55-
==== tests/cases/compiler/consumer.d.ts (1 errors) ====
56-
export declare function w(): Widget1;
57-
~~~~~~~
58-
!!! Cannot find name 'Widget1'.
59-
60-
==== tests/cases/compiler/w1.d.ts (0 errors) ====
61-
export = Widget1;
62-
declare class Widget1 {
63-
name: string;
64-
}
65-
66-
==== tests/cases/compiler/exporter.d.ts (0 errors) ====
67-
export import w = require('./w1');
68-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
==== tests/cases/compiler/consumer.ts (1 errors) ====
2+
import e = require('./exporter');
3+
4+
export function w(): e.w { // Should be OK
5+
~
6+
!!! Return type of exported function has or is using private name 'Widget1'.
7+
return {name: 'value' };
8+
}
9+
==== tests/cases/compiler/w1.ts (0 errors) ====
10+
11+
export = Widget1
12+
interface Widget1 { name: string; }
13+
14+
==== tests/cases/compiler/exporter.ts (0 errors) ====
15+
export import w = require('./w1');
16+

Diff for: tests/baselines/reference/exportImportNonInstantiatedModule2.js

-21
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,3 @@ interface Widget1 {
3737
}
3838
//// [exporter.d.ts]
3939
export import w = require('./w1');
40-
//// [consumer.d.ts]
41-
export declare function w(): Widget1;
42-
43-
44-
//// [DtsFileErrors]
45-
46-
47-
==== tests/cases/compiler/consumer.d.ts (1 errors) ====
48-
export declare function w(): Widget1;
49-
~~~~~~~
50-
!!! Cannot find name 'Widget1'.
51-
52-
==== tests/cases/compiler/w1.d.ts (0 errors) ====
53-
export = Widget1;
54-
interface Widget1 {
55-
name: string;
56-
}
57-
58-
==== tests/cases/compiler/exporter.d.ts (0 errors) ====
59-
export import w = require('./w1');
60-

0 commit comments

Comments
 (0)