Skip to content

Commit

Permalink
Add custom type assertions for goog.is* functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ribrdb committed May 29, 2019
1 parent 6c8a2bd commit 3cdae25
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,47 @@ private boolean isTemplateOf(JSType type, String typeName) {
&& type.toMaybeTemplatizedType().getReferenceName().equals(typeName);
}

private boolean handleSpecialTypeAssertionFunctions(String propName) {
if (!isGoogNamespace) return false;
if (propName.equals("isArray")) {
emit("(val : any ) : val is any[] ");
return true;
}
if (propName.equals("isBoolean")) {
emit("(val : any ) : val is boolean ");
return true;
}
if (propName.equals("isDef")) {
emit("<T> (val : T ) : val is Exclude<T, undefined> ");
return true;
}
if (propName.equals("isDefAndNotNull")) {
emit("<T> (val : T ) : val is NonNullable<T> ");
return true;
}
if (propName.equals("isFunction")) {
emit("(val : any ) : val is Function ");
return true;
}
if (propName.equals("isNull")) {
emit("(val : any ) : val is null ");
return true;
}
if (propName.equals("isNumber")) {
emit("(val : any ) : val is number ");
return true;
}
if (propName.equals("isObject")) {
emit("(val : any ) : val is Object ");
return true;
}
if (propName.equals("isString")) {
emit("(val : any ) : val is string ");
return true;
}
return false;
}

/**
* Closure has an experimental feature - Type Transformation Expression (TTE) - used to type
* functions like Promise.then and Promise.all. The feature is rarely used and impossible to
Expand Down Expand Up @@ -3384,7 +3425,8 @@ void walkInnerSymbols(ObjectType type, String innerNamespace) {
private void visitFunctionExpression(String propName, FunctionType ftype) {
emit("function");
emit(propName);
visitFunctionDeclaration(ftype, Collections.<String>emptyList());
if (!handleSpecialTypeAssertionFunctions(propName))
visitFunctionDeclaration(ftype, Collections.<String>emptyList());
emit(";");
emitBreak();
}
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/com/google/javascript/clutz/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ declare namespace goog {
*/
function exportProperty (object : ಠ_ಠ.clutz.GlobalObject | null , publicName : string , symbol : any ) : void ;
function inherits (childCtor : Function , parentCtor : Function ) : void ;
function isDef (val : any ) : boolean ;
function isArray (val : any ) : val is any[] ;
function isBoolean (val : any ) : val is boolean ;
function isDef <T> (val : T ) : val is Exclude<T, undefined> ;
function isDefAndNotNull <T> (val : T ) : val is NonNullable<T> ;
function isFunction (val : any ) : val is Function ;
function isNull (val : any ) : val is null ;
function isNumber (val : any ) : val is number ;
function isObject (val : any ) : val is Object ;
function isString (val : any ) : val is string ;
function require (name : string ) : ಠ_ಠ.clutz.ClosureSymbolNotGoogProvided;
let /**
* Reference to the global context. In most cases this will be 'window'.
Expand Down
52 changes: 50 additions & 2 deletions src/test/java/com/google/javascript/clutz/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,59 @@ goog.require = function(name) {};
goog.inherits = function(childCtor, parentCtor) {};

/**
* @param {?} val Variable to test.
* @return {boolean} Whether variable is defined.
* @param {?} val
* @return {boolean}
*/
goog.isArray = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isBoolean = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isDef = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isDefAndNotNull = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isFunction = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isNull = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isNumber = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isObject = function(val) {};

/**
* @param {?} val
* @return {boolean}
*/
goog.isString = function(val) {};

goog.provide('goog.Uri');
/** @constructor */
goog.Uri = function() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ declare namespace goog {
*/
function exportProperty (object : ಠ_ಠ.clutz.GlobalObject | null , publicName : string , symbol : any ) : void ;
function inherits (childCtor : ಠ_ಠ.clutz.partial.FunctionAlias , parentCtor : ಠ_ಠ.clutz.partial.FunctionAlias ) : void ;
function isDef (val : any ) : boolean ;
function isArray (val : any ) : val is any[] ;
function isBoolean (val : any ) : val is boolean ;
function isDef <T> (val : T ) : val is Exclude<T, undefined> ;
function isDefAndNotNull <T> (val : T ) : val is NonNullable<T> ;
function isFunction (val : any ) : val is Function ;
function isNull (val : any ) : val is null ;
function isNumber (val : any ) : val is number ;
function isObject (val : any ) : val is Object ;
function isString (val : any ) : val is string ;
function require (name : string ) : ಠ_ಠ.clutz.ClosureSymbolNotGoogProvided;
let /**
* Reference to the global context. In most cases this will be 'window'.
Expand Down

0 comments on commit 3cdae25

Please sign in to comment.