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

Commit

Permalink
fix(bug): handle nested classes and enums.
Browse files Browse the repository at this point in the history
fixes #71
fixes #69
  • Loading branch information
alexeagle committed Sep 1, 2015
1 parent 2c50e99 commit c1686d3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.javascript.rhino.jstype.EnumType;
import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.NamedType;
import com.google.javascript.rhino.jstype.NoType;
Expand Down Expand Up @@ -491,7 +490,7 @@ public Void caseObjectType(ObjectType type) {
if (type.isRecordType()) {
visitRecordType((RecordType) type);
} else {
emit(type.getReferenceName());
emit(getRelativeName(type));
}
return null;
}
Expand All @@ -504,7 +503,7 @@ public Void caseUnionType(UnionType type) {

@Override
public Void caseNamedType(NamedType type) {
emit(type.getReferenceName());
emit(getRelativeName(type));
return null;
}

Expand Down Expand Up @@ -712,15 +711,21 @@ private void visitProperties(ObjectType objType, boolean isStatic) {
|| "constructor".equals(propName)) {
continue;
}
JSType propertyType = objType.getPropertyType(propName);
// Some symbols might be emitted as provides, so don't duplicate them
if (provides.contains(objType.getDisplayName() + "." + propName)) {
continue;
} else if (propertyType.isEnumType()) {
// For now, we don't emit static enum properties. We theorize it should not be needed.
emit("/* not emitting " + propName + " because it is an enum and it is not provided */");
emitBreak();
continue;
}

if (isStatic) {
emit("static");
}
emit(propName);
JSType propertyType = objType.getPropertyType(propName);
if (propertyType.isFunctionType()) {
visitFunctionDeclaration((FunctionType) propertyType);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
declare namespace ಠ_ಠ.cl2dts_internal.foo.bar.Baz {
type NestedEnum = number ;
var NestedEnum : {
A : NestedEnum ,
B : NestedEnum ,
};
}
declare module 'goog:foo.bar.Baz.NestedEnum' {
import alias = ಠ_ಠ.cl2dts_internal.foo.bar.Baz.NestedEnum;
export default alias;
}
declare namespace ಠ_ಠ.cl2dts_internal.foo.bar {
class Baz {
field : string ;
equals (b : Baz.NestedClass ) : boolean ;
method (a : string ) : number ;
static staticMethod (a : string ) : number ;
/* not emitting AnotherNestedEnum because it is an enum and it is not provided */
}
}
declare module 'goog:foo.bar.Baz' {
import alias = ಠ_ಠ.cl2dts_internal.foo.bar.Baz;
export default alias;
}
declare namespace ಠ_ಠ.cl2dts_internal.foo.bar.Baz {
class NestedClass {
}
}
declare module 'goog:foo.bar.Baz.NestedClass' {
import alias = ಠ_ಠ.cl2dts_internal.foo.bar.Baz.NestedClass;
export default alias;
}
24 changes: 24 additions & 0 deletions src/test/java/com/google/javascript/cl2dts/provide_single_class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
goog.provide('foo.bar.Baz');
goog.provide('foo.bar.Baz.NestedClass');
goog.provide('foo.bar.Baz.NestedEnum');

/** @constructor */
foo.bar.Baz = function() {
Expand All @@ -21,3 +23,25 @@ foo.bar.Baz.staticMethod = function(a) {
foo.bar.Baz.prototype.method = function(a) {
return Number(a)
};

/**
* @param {foo.bar.Baz.NestedClass} b
* @return {boolean}
*/
foo.bar.Baz.prototype.equals = function(b) {
return false;
};

/** @constructor */
foo.bar.Baz.NestedClass = function() {};

/** @enum */
foo.bar.Baz.NestedEnum = {
A: 1,
B: 2
};

//!! This is not goog.provided, and it would be strange to reference an enum type as a static class
//!! property. This is just here to assert that we gracefully ignore it.
/** @enum */
foo.bar.Baz.AnotherNestedEnum = {};
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/// <reference path="./provide_single_class"/>
import C from 'goog:foo.bar.Baz';
import NE from 'goog:foo.bar.Baz.NestedEnum';

var n: number = C.staticMethod("some");
let x = new C();
let s: string = x.field;
n = x.method("some");
let e: C.NestedEnum = C.NestedEnum.A;
e = NE.B;

0 comments on commit c1686d3

Please sign in to comment.