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

Commit 3bc7de5

Browse files
committed
fix: Function type vars should not output as classes.
Previous attempts to fix this missed the fact that externs need to be present to reproduce the bug.
1 parent b5f2e28 commit 3bc7de5

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,10 @@ private void walk(TypedVar symbol, boolean isDefault) {
405405
FunctionType ftype = (FunctionType) type;
406406

407407
// Closure represents top-level functions as classes because they might be new-able.
408+
// This happens through externs es3.js which has Function marked as constructor.
408409
// See https://github.com/angular/closure-to-dts/issues/90
409-
boolean ordinaryFunctionAppearingAsClass = type.isConstructor()
410-
&& type.getJSDocInfo() != null && !type.getJSDocInfo().isConstructor();
410+
boolean ordinaryFunctionAppearingAsClass = type.isConstructor() &&
411+
type.getDisplayName().equals("Function");
411412

412413
if (type.isOrdinaryFunction() || ordinaryFunctionAppearingAsClass) {
413414
emit("function");

src/test/java/com/google/javascript/cl2dts/types_with_externs.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare namespace ಠ_ಠ.cl2dts_internal.typesWithExterns {
1515
type ArrayLike = NodeList | IArguments | { length : number } ;
1616
var c : NodeList | IArguments | { length : number } ;
1717
function id (x : NodeList | IArguments | { length : number } ) : NodeList | IArguments | { length : number } ;
18+
function topLevelFunction ( ...a : any [] ) : any ;
1819
}
1920
declare module 'goog:typesWithExterns' {
2021
import alias = ಠ_ಠ.cl2dts_internal.typesWithExterns;

src/test/java/com/google/javascript/cl2dts/types_with_externs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ typesWithExterns.c = null;
8787
* @param {typesWithExterns.ArrayLike} x
8888
* @returns {typesWithExterns.ArrayLike}
8989
*/
90-
typesWithExterns.id = function(x) { return x; }
90+
typesWithExterns.id = function(x) { return x; }
91+
92+
/**
93+
* @type {!Function}
94+
*/
95+
typesWithExterns.topLevelFunction = function() {};

0 commit comments

Comments
 (0)