Skip to content

Commit

Permalink
better handling of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Asger Feldthaus committed Apr 3, 2014
1 parent 01ed87f commit e11470b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
11 changes: 11 additions & 0 deletions testcases/return-enum.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare enum FontStyle {
Normal, Bold, Italic
}
declare enum FontSize {
Small, Large
}

declare function getStyle(): FontStyle;
declare function badStyle(x:number): FontStyle;
declare function bad(): FontStyle;
declare function badConvert(x:FontStyle): FontSize;
23 changes: 23 additions & 0 deletions testcases/return-enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var FontStyle = {
Normal: "normal",
Bold: "bold",
Italic: "italic"
};

var FontSize = {
Large: "large",
Small: "small"
}

function getStyle() {
return "normal";
}
function badStyle(x) {
return x;
}
function bad() {
return "yikes"
}
function badConvert(x) {
return x;
}
1 change: 1 addition & 0 deletions testcases/return-enum.jsnap

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions tscheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,6 @@ function check(type, value, path, userPath, parentKey, tpath) {
}
checkCallSignature(call, value.key, objPrty.get.key, qualify(path,k))
}
// todo: getters and setters require static analysis
}
}
}
Expand Down Expand Up @@ -1170,7 +1169,7 @@ function printCoverage() {
// Formatting types and values
// ------------------------------------------

// TODO: restrict depth to avoid printing gigantic types
// XXX: restrict depth to avoid printing gigantic types

function formatTypeProperty(name,prty) {
return name + (prty.optional ? '?' : '') + ': ' + formatType(prty.type)
Expand Down Expand Up @@ -1602,7 +1601,8 @@ function Analyzer() {
var all_unodes = []
var unode_id = 0
function UNode() {
// all_unodes.push(this) // TODO: remove when not debuggin
// all_unodes.push(this) // for debugging
// this.origin = new Error().stack // for debugging

// union-find information
this.parent = this
Expand All @@ -1623,8 +1623,6 @@ function Analyzer() {
this._isObject = false
this._level = LEVEL_BOT
this._isFree = false

// this.origin = new Error().stack // FIXME: debugging only
}
UNode.prototype = {
get id() { return this.rep()._id },
Expand Down Expand Up @@ -2046,7 +2044,18 @@ function Analyzer() {
this.primitives.add({type: 'value', value: t.value})
break;
case 'enum':
this.makeAny(); // TODO: proper handling of enums
var vals = enum_values.get(t.name)
if (vals.length === 0) {
this.makeAny();
} else {
vals.forEach(function(v) {
if (v && typeof v === 'object') {
unifyLater(this, getConcreteObject(v.key))
} else {
self.primitives.add({type: 'value', value: v})
}
})
}
break;
default:
throw new Error("Unexpected type: " + t.type)
Expand Down

0 comments on commit e11470b

Please sign in to comment.