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

Commit

Permalink
support goog.require syntax.
Browse files Browse the repository at this point in the history
Closes #113
  • Loading branch information
rkirov committed Oct 15, 2015
1 parent 0577e2a commit b73665c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ public int compare(TypedVar o1, TypedVar o2) {
treeWalker.walkDefaultInterface((FunctionType) symbol.getType());
emitNamespaceEnd();
}
emitGoogRequireSupport(namespace, isDefault ? symbol.getName() : namespace);
}

private void emitGoogRequireSupport(String namespace, String closureNamespace) {
// goog namespace doesn't need to be goog.required.
if (namespace.equals("goog")) return;
emitNamespaceBegin("goog");
String qualifiedClosureNamespace = INTERNAL_NAMESPACE + "." + closureNamespace;
emit("function require(name: '" + closureNamespace + "'): typeof " + qualifiedClosureNamespace + ";");
emitBreak();
emitNamespaceEnd();
}


Expand Down
4 changes: 4 additions & 0 deletions src/resources/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @provideGoog */
/** @const */ var goog = goog || {};
goog.provide = function (arg) {};
goog.require = function (arg) {};
4 changes: 4 additions & 0 deletions src/resources/closure.lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ declare namespace ಠ_ಠ.clutz_internal {
type GlobalError = Error;
var GlobalError: ErrorConstructor;
}

// Closures' goog namespace is accessible as global symbol without need for
// explicit goog.require, during the closure compiler pass.
declare var goog: typeof ಠ_ಠ.clutz_internal.goog;
7 changes: 7 additions & 0 deletions src/test/java/com/google/javascript/clutz/ProgramSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,24 @@ public ProgramSubject(FailureStrategy failureStrategy, ProgramSubject.Program su
public void generatesDeclarations(Boolean withExterns, String expected) {
Options opts = new Options(!withExterns);



List<SourceFile> sourceFiles = new ArrayList<>();
sourceFiles.add(SourceFile.fromFile("src/resources/base.js", UTF_8));

for (File nonroot : getSubject().nonroots) {
sourceFiles.add(SourceFile.fromFile(nonroot, UTF_8));
}

List<String> roots = new ArrayList<>();
roots.add("src/resources/base.js");

for (File root : getSubject().roots) {
sourceFiles.add(SourceFile.fromFile(root, UTF_8));
roots.add(root.getPath());
}


String actual = new DeclarationGenerator(opts)
.generateDeclarations(sourceFiles, NO_EXTERNS, new Depgraph(roots));
actual = DeclarationGenerator.GOLDEN_FILE_COMMENTS_REGEXP.matcher(actual).replaceAll("");
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/google/javascript/clutz/goog_require.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
declare namespace ಠ_ಠ.clutz_internal.foo {
class SimpleClass {
}
}
declare namespace ಠ_ಠ.clutz_internal.goog {
function require(name: 'foo.SimpleClass'): typeof ಠ_ಠ.clutz_internal.foo.SimpleClass;
}
declare module 'goog:foo.SimpleClass' {
import alias = ಠ_ಠ.clutz_internal.foo.SimpleClass;
export default alias;
}
declare namespace ಠ_ಠ.clutz_internal.foo.simpleNamespace {
var a : number ;
}
declare namespace ಠ_ಠ.clutz_internal.goog {
function require(name: 'foo.simpleNamespace'): typeof ಠ_ಠ.clutz_internal.foo.simpleNamespace;
}
declare module 'goog:foo.simpleNamespace' {
import alias = ಠ_ಠ.clutz_internal.foo.simpleNamespace;
export = alias;
}
declare namespace ಠ_ಠ.clutz_internal.goog {
function require (name : string ) : void ;
}
declare module 'goog:goog.require' {
import alias = ಠ_ಠ.clutz_internal.goog.require;
export default alias;
}
12 changes: 12 additions & 0 deletions src/test/java/com/google/javascript/clutz/goog_require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
goog.provide('foo.SimpleClass');
goog.provide('foo.simpleNamespace');

/**
* @constructor
*/
foo.SimpleClass = function() {};

/**
* @const {number}
*/
foo.simpleNamespace.a = 0;
10 changes: 10 additions & 0 deletions src/test/java/com/google/javascript/clutz/goog_require_usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const SimpleClass = goog.require('foo.SimpleClass');
const {a} = goog.require('foo.simpleNamespace');

// Asserting that types carry through.
var b: number = a;
// TODO(rado): this should be c: SimpleClass, but without ES6 module
// imports we cannot bring the type symbol in.
// Note that does not mean that the type won't flow through the assignment
// but simply it does not have accessible symbol in the client code.
var c = new SimpleClass();

0 comments on commit b73665c

Please sign in to comment.