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

Commit 6465f5a

Browse files
committed
fix: local Error class extending global Error class.
Introduces an auxillary .d.ts `closure.lib.d.ts` that needs to be passed into all consumers of cl2dts. Closes #81
1 parent 9f51ee4 commit 6465f5a

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ public Void caseStringType() {
594594

595595
@Override
596596
public Void caseObjectType(ObjectType type) {
597+
if (type.getDisplayName() != null && type.getDisplayName().equals("Error")) {
598+
// global Error is aliased as GlobalError in closure.lib.d.ts.
599+
emit("GlobalError");
600+
return null;
601+
}
597602
// Closure doesn't require that all the type params be declared, but TS does
598603
if (!type.getTemplateTypeMap().isEmpty()
599604
&& !typeRegistry.getNativeType(OBJECT_TYPE).equals(type)) {

src/resources/closure.lib.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Work around for https://github.com/Microsoft/TypeScript/issues/983
2+
// All Cl2dts namespaces are below ಠ_ಠ.cl2dts_internal, thus
3+
// this acts as global.
4+
declare namespace ಠ_ಠ.cl2dts_internal {
5+
type GlobalError = Error;
6+
var GlobalError: ErrorConstructor;
7+
}

src/test/java/com/google/javascript/cl2dts/DeclarationSyntaxTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void testDeclarationSyntax() throws Exception {
4848

4949
final List<String> tscCommand =
5050
Lists.newArrayList(TSC.toString(), "--noEmit", "--skipDefaultLibCheck");
51+
tscCommand.add("src/resources/closure.lib.d.ts");
5152
tscCommand.addAll(goldenFilePaths);
5253
runChecked(tscCommand);
5354
}
@@ -57,6 +58,7 @@ public void testDeclarationUsage() throws Exception {
5758
List<File> inputs = DeclarationGeneratorTests.getTestInputFiles(TS_SOURCES);
5859
final List<String> tscCommand =
5960
Lists.newArrayList(TSC.toString(), "--noEmit", "--skipDefaultLibCheck", "-m", "commonjs");
61+
tscCommand.add("src/resources/closure.lib.d.ts");
6062
for (File input : inputs) {
6163
tscCommand.add(input.getPath());
6264
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ declare namespace ಠ_ಠ.cl2dts_internal.typesWithExterns {
1818
function topLevelFunction ( ...a : any [] ) : any ;
1919
interface ExtendsIThenable extends PromiseLike < any > {
2020
}
21+
class Error extends GlobalError {
22+
}
2123
}
2224
declare module 'goog:typesWithExterns' {
2325
import alias = ಠ_ಠ.cl2dts_internal.typesWithExterns;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,10 @@ typesWithExterns.topLevelFunction = function() {};
9898
* @interface
9999
* @extends {IThenable}
100100
*/
101-
typesWithExterns.ExtendsIThenable = function() {};
101+
typesWithExterns.ExtendsIThenable = function() {};
102+
103+
/**
104+
* @constructor
105+
* @extends {Error}
106+
*/
107+
typesWithExterns.Error = function() {};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {elementMaybe, id} from 'goog:typesWithExterns';
1+
import {elementMaybe, id, Error} from 'goog:typesWithExterns';
22

33
var el: Element = elementMaybe();
44
var els: ArrayLike<any> = id(document.getElementsByClassName('foo'));
5+
6+
var myError: Error = new Error();

0 commit comments

Comments
 (0)