Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 79b458f

Browse files
lambertjamesdJames Lambert
authored and
James Lambert
committed
Ignore private annotation for constructors
1 parent 9061033 commit 79b458f

File tree

9 files changed

+80
-23
lines changed

9 files changed

+80
-23
lines changed

src/main/java/com/google/javascript/clutz/DeclarationGenerator.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ private boolean needsAlias(Set<String> shadowedSymbols, String provide, TypedVar
602602
if (symbol == null) return true;
603603
JSType type = symbol.getType();
604604
// Emit var foo : PrivateType for private symbols.
605-
if (isPrivate(type.getJSDocInfo())) return true;
605+
if (isPrivate(type.getJSDocInfo()) && !isConstructor(type.getJSDocInfo())) return true;
606606
// Only var declarations have collisions, while class, interface, and functions can coexist with
607607
// namespaces.
608608
if (type != null && (type.isInterface() || type.isConstructor() || type.isFunctionType())) {
@@ -647,7 +647,7 @@ private int declareNamespace(String namespace, TypedVar symbol, String emitName,
647647
emitNamespaceBegin(namespace);
648648
TreeWalker treeWalker = new TreeWalker(compiler.getTypeRegistry(), provides, isExtern);
649649
if (isDefault) {
650-
if (isPrivate(symbol.getJSDocInfo())) {
650+
if (isPrivate(symbol.getJSDocInfo()) && !isConstructor(symbol.getJSDocInfo())) {
651651
treeWalker.emitPrivateValue(emitName);
652652
} else {
653653
treeWalker.walk(symbol, emitName);
@@ -817,7 +817,7 @@ private boolean isPrototypeMethod(TypedVar other) {
817817

818818
private boolean isPrivateProperty(ObjectType obj, String propName) {
819819
JSDocInfo info = obj.getOwnPropertyJSDocInfo(propName);
820-
return isPrivate(info);
820+
return isPrivate(info) && !isConstructor(info);
821821
}
822822

823823
private boolean isTypeCheckSuppressedProperty(ObjectType obj, String propName) {
@@ -844,6 +844,10 @@ private boolean isPrivate(@Nullable JSDocInfo docInfo) {
844844
return docInfo != null && docInfo.getVisibility() == Visibility.PRIVATE;
845845
}
846846

847+
private boolean isConstructor(@Nullable JSDocInfo docInfo) {
848+
return docInfo != null && docInfo.isConstructor();
849+
}
850+
847851
private void declareModule(String name, boolean isDefault, String emitName) {
848852
declareModule(name, isDefault, emitName, false);
849853
}
@@ -1158,15 +1162,10 @@ private void visitClassOrInterface(String name, FunctionType ftype) {
11581162
ObjectType superType = getSuperType(ftype);
11591163
if (superType != null) {
11601164
emit("extends");
1161-
if (isPrivate(superType.getJSDocInfo())) {
1162-
// TypeScript does not allow public APIs that expose non-exported/private types.
1163-
emit(Constants.INTERNAL_NAMESPACE + ".PrivateClass");
1164-
} else {
1165-
boolean emitInstanceForObject = emitInstance &&
1166-
(opts.emitPlatformExterns || !isDefinedInPlatformExterns(superType));
1167-
Visitor<Void> visitor = new ExtendsImplementsTypeVisitor(emitInstanceForObject);
1168-
superType.visit(visitor);
1169-
}
1165+
boolean emitInstanceForObject = emitInstance &&
1166+
(opts.emitPlatformExterns || !isDefinedInPlatformExterns(superType));
1167+
Visitor<Void> visitor = new ExtendsImplementsTypeVisitor(emitInstanceForObject);
1168+
superType.visit(visitor);
11701169
}
11711170

11721171
Iterator<ObjectType> it = ftype.getOwnImplementedInterfaces().iterator();
@@ -1181,7 +1180,7 @@ private void visitClassOrInterface(String name, FunctionType ftype) {
11811180
private void emitCommaSeparatedInterfaces(Iterator<ObjectType> it) {
11821181
while (it.hasNext()) {
11831182
ObjectType type = it.next();
1184-
if (isPrivate(type.getJSDocInfo())) {
1183+
if (isPrivate(type.getJSDocInfo()) && !isConstructor(type.getJSDocInfo())) {
11851184
// TypeScript does not allow public APIs that expose non-exported/private types.
11861185
emit(Constants.INTERNAL_NAMESPACE + ".PrivateInterface");
11871186
} else {
@@ -1340,7 +1339,7 @@ private void visitType(JSType typeToVisit, boolean skipDefCheck, final boolean i
13401339
// See also JsdocToEs6TypedConverter in the Closure code base. This code is implementing the
13411340
// same algorithm starting from JSType nodes (as opposed to JSDocInfo), and directly
13421341
// generating textual output. Otherwise both algorithms should produce the same output.
1343-
if (isPrivate(typeToVisit)) {
1342+
if (isPrivate(typeToVisit) && !isConstructor(typeToVisit.getJSDocInfo())) {
13441343
// TypeScript does not allow public APIs that expose non-exported/private types. Just emit
13451344
// an empty object literal type for those, i.e. something that cannot be used for anything,
13461345
// except being passed around.
@@ -1642,8 +1641,9 @@ && getSuperType(type) == null) {
16421641
emitBreak();
16431642
}
16441643
// Constructors.
1645-
if (type.isConstructor() && (type).getParameters().iterator().hasNext()) {
1644+
if (type.isConstructor() && (type).getParameters().iterator().hasNext() && !isPrivate(type.getJSDocInfo())) {
16461645
maybeEmitJsDoc(type.getJSDocInfo(), /* ignoreParams */ false);
1646+
// TODO mark constuctor as private when source is annoated with @private for ts v2.0 and greater
16471647
emit("constructor");
16481648
visitFunctionParameters(type, false, classTemplateTypeNames);
16491649
emit(";");

src/test/java/com/google/javascript/clutz/nested_namespaces.d.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ declare module 'goog:nested.NotNestedEither' {
2323
export default alias;
2424
}
2525
declare namespace ಠ_ಠ.clutz.nested {
26-
var PrivateC__clutz_alias : ಠ_ಠ.clutz.PrivateType;
26+
class PrivateC extends PrivateC_Instance {
27+
}
28+
class PrivateC_Instance {
29+
private noStructuralTyping_: any;
30+
}
31+
}
32+
declare namespace ಠ_ಠ.clutz.goog {
33+
function require(name: 'nested.PrivateC'): typeof ಠ_ಠ.clutz.nested.PrivateC;
2734
}
2835
declare module 'goog:nested.PrivateC' {
29-
import alias = ಠ_ಠ.clutz.nested.PrivateC__clutz_alias;
36+
import alias = ಠ_ಠ.clutz.nested.PrivateC;
3037
export default alias;
3138
}
3239
declare namespace ಠ_ಠ.clutz.nested.PrivateC {
@@ -42,7 +49,7 @@ declare module 'goog:nested.PrivateC.Enum' {
4249
export default alias;
4350
}
4451
declare namespace ಠ_ಠ.clutz.nested {
45-
var foo__clutz_alias : ಠ_ಠ.clutz.PrivateType ;
52+
var foo__clutz_alias : ಠ_ಠ.clutz.nested.PrivateC ;
4653
}
4754
declare namespace ಠ_ಠ.clutz.goog {
4855
function require(name: 'nested.foo'): typeof ಠ_ಠ.clutz.nested.foo__clutz_alias;

src/test/java/com/google/javascript/clutz/private_class.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare namespace ಠ_ಠ.clutz.privateclass {
22
class A extends A_Instance {
33
}
4-
class A_Instance extends ಠ_ಠ.clutz.PrivateClass {
4+
class A_Instance extends ಠ_ಠ.clutz.privateclass.P_Instance {
55
}
66
class B extends B_Instance {
77
}
@@ -10,6 +10,11 @@ declare namespace ಠ_ಠ.clutz.privateclass {
1010
}
1111
interface I extends ಠ_ಠ.clutz.PrivateInterface {
1212
}
13+
class P extends P_Instance {
14+
}
15+
class P_Instance {
16+
private noStructuralTyping_: any;
17+
}
1318
}
1419
declare namespace ಠ_ಠ.clutz.goog {
1520
function require(name: 'privateclass'): typeof ಠ_ಠ.clutz.privateclass;

src/test/java/com/google/javascript/clutz/private_class.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ goog.provide('privateclass');
33
/**
44
* @constructor
55
* @private
6+
* @param {number} parameter
67
*/
7-
privateclass.P = function() {};
8+
privateclass.P = function(parameter) {};
89

910
/**
1011
* @interface

src/test/java/com/google/javascript/clutz/private_provided_single_class.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
declare namespace ಠ_ಠ.clutz.foo {
2-
var PrivateClass : ಠ_ಠ.clutz.PrivateType;
2+
class PrivateClass extends PrivateClass_Instance {
3+
}
4+
class PrivateClass_Instance {
5+
private noStructuralTyping_: any;
6+
}
7+
}
8+
declare namespace ಠ_ಠ.clutz.goog {
9+
function require(name: 'foo.PrivateClass'): typeof ಠ_ಠ.clutz.foo.PrivateClass;
310
}
411
declare module 'goog:foo.PrivateClass' {
512
import alias = ಠ_ಠ.clutz.foo.PrivateClass;

src/test/java/com/google/javascript/clutz/private_type.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ declare module 'goog:privatetype.Foo' {
2323
import alias = ಠ_ಠ.clutz.privatetype.Foo;
2424
export default alias;
2525
}
26+
declare namespace ಠ_ಠ.clutz.privatetype {
27+
class X_ extends X__Instance {
28+
static staticMethod ( ) : void ;
29+
}
30+
class X__Instance {
31+
private noStructuralTyping_: any;
32+
method ( ) : void ;
33+
}
34+
}
35+
declare namespace ಠ_ಠ.clutz.goog {
36+
function require(name: 'privatetype.X_'): typeof ಠ_ಠ.clutz.privatetype.X_;
37+
}
38+
declare module 'goog:privatetype.X_' {
39+
import alias = ಠ_ಠ.clutz.privatetype.X_;
40+
export default alias;
41+
}
2642
declare namespace ಠ_ಠ.clutz.privatetype {
2743
var enumUser : ಠ_ಠ.clutz.PrivateType ;
2844
}
@@ -34,7 +50,7 @@ declare module 'goog:privatetype.enumUser' {
3450
export default alias;
3551
}
3652
declare namespace ಠ_ಠ.clutz.privatetype {
37-
var user : ಠ_ಠ.clutz.PrivateType ;
53+
var user : ಠ_ಠ.clutz.privatetype.X_ ;
3854
}
3955
declare namespace ಠ_ಠ.clutz.goog {
4056
function require(name: 'privatetype.user'): typeof ಠ_ಠ.clutz.privatetype.user;

src/test/java/com/google/javascript/clutz/private_type.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ goog.provide('privatetype');
22
goog.provide('privatetype.enumUser');
33
goog.provide('privatetype.user');
44
goog.provide('privatetype.Foo');
5+
goog.provide('privatetype.X_');
56

67
/**
78
* @enum {string}
@@ -18,6 +19,10 @@ privatetype.user = new privatetype.X_();
1819
/** @constructor @private */
1920
privatetype.X_ = function() {};
2021

22+
privatetype.X_.staticMethod = function() {};
23+
24+
privatetype.X_.prototype.method = function() {};
25+
2126
/** @constructor */
2227
privatetype.Foo = function(a) {}
2328

src/test/java/com/google/javascript/clutz/privates.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
declare namespace ಠ_ಠ.clutz.priv {
2+
class PrivateClazz extends PrivateClazz_Instance {
3+
}
4+
class PrivateClazz_Instance {
5+
private noStructuralTyping_: any;
6+
}
27
class PublicClass extends PublicClass_Instance {
38
}
49
class PublicClass_Instance {
@@ -20,6 +25,13 @@ declare namespace ಠ_ಠ.clutz.priv2 {
2025
private noStructuralTyping_: any;
2126
}
2227
}
28+
declare namespace ಠ_ಠ.clutz.priv2.PublicClass {
29+
class PrivateNestedClass_ extends PrivateNestedClass__Instance {
30+
}
31+
class PrivateNestedClass__Instance {
32+
private noStructuralTyping_: any;
33+
}
34+
}
2335
declare namespace ಠ_ಠ.clutz.goog {
2436
function require(name: 'priv2.PublicClass'): typeof ಠ_ಠ.clutz.priv2.PublicClass;
2537
}

src/test/java/com/google/javascript/clutz/types_externs_with_platform.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ declare namespace ಠ_ಠ.clutz.functionNamespace {
7474
function dom (nodeOrEvent : Node | null | GlobalEvent ) : functionNamespaceHelperClass ;
7575
}
7676
declare namespace ಠ_ಠ.clutz.functionNamespace {
77-
var privateClass : ಠ_ಠ.clutz.PrivateType;
77+
class privateClass extends privateClass_Instance {
78+
}
79+
class privateClass_Instance {
80+
private noStructuralTyping_: any;
81+
}
7882
}
7983
declare namespace ಠ_ಠ.clutz {
8084
class functionNamespaceHelperClass extends functionNamespaceHelperClass_Instance {

0 commit comments

Comments
 (0)