fix(transpile): fix usage of `int` and references to `assert` module

This commit is contained in:
Tobias Bosch 2015-02-06 17:18:49 -08:00
parent f39c6dc2c7
commit 6f59f2f5a3
4 changed files with 30 additions and 53 deletions

View File

@ -166,8 +166,8 @@ export class ListWrapper {
static isEmpty(list) {
return list.length == 0;
}
static fill(list:List, value, start:int = 0, end:int = undefined) {
list.fill(value, start, end);
static fill(list:List, value, start:int = 0, end:int = null) {
list.fill(value, start, end === null ? undefined: end);
}
static equals(a:List, b:List):boolean {
if(a.length != b.length) return false;

View File

@ -1,12 +1,24 @@
import {assert} from 'rtts_assert/rtts_assert';
export {proxy} from 'rtts_assert/rtts_assert';
export var Type = Function;
export var Math = window.Math;
var assertionsEnabled_ = typeof assert !== 'undefined';
var int;
// global assert support, as Dart has it...
// TODO: `assert` calls need to be removed in production code!
if (assertionsEnabled_) {
window.assert = assert;
// `int` is not a valid JS type
int = assert.define('int', function(value) {
return typeof value === 'number' && value%1 === 0;
});
} else {
int = {};
window.assert = function() {};
}
export {int};
export class FIELD {
constructor(definition) {
@ -71,8 +83,8 @@ export class StringWrapper {
return s.startsWith(start);
}
static substring(s:string, start:int, end:int = undefined) {
return s.substring(start, end);
static substring(s:string, start:int, end:int = null) {
return s.substring(start, end === null ? undefined: end);
}
static replaceAllMapped(s:string, from:RegExp, cb:Function): string {
@ -157,17 +169,17 @@ export class NumberWrapper {
}
}
export function int() {};
int.assert = function(value) {
return value == null || typeof value == 'number' && value === Math.floor(value);
}
export var RegExp = assert.define('RegExp', function(obj) {
var RegExp;
if (assertionsEnabled_) {
RegExp = assert.define('RegExp', function(obj) {
assert(obj).is(assert.structure({
single: window.RegExp,
multiple: window.RegExp
}));
});
} else {
RegExp = {};
}
export class RegExpWrapper {
static create(regExpStr, flags:string = ''):RegExp {
@ -224,12 +236,7 @@ export function isJsObject(o):boolean {
}
export function assertionsEnabled():boolean {
try {
var x:int = "string";
return false;
} catch (e) {
return true;
}
return assertionsEnabled_;
}
export function print(obj) {

View File

@ -230,13 +230,6 @@ function returnType(actual, T) {
return actual;
}
// `int` is not a valid JS type, and traceur will leave
// it untouched. However, we want to be able to use it,
// so we provide it as a global
var intType = _global['int'] = define('int', function(value) {
return typeof value === 'number' && value%1 === 0;
});
// TODO(vojta): define these with DSL?
var string = type.string = define('string', function(value) {
return typeof value === 'string';
@ -362,7 +355,6 @@ assert.fail = fail;
assert.string = string;
assert.number = number;
assert.boolean = boolean;
assert.int = intType;
// custom types
assert.arrayOf = arrayOf;

View File

@ -10,8 +10,7 @@
// - [assert.structure](#assert-structure)
// - [Integrating with Traceur](#integrating-with-traceur)
import {assert} from 'rtts_assert/rtts_assert';
// Note: `assert` gets automatically included by traceur!
export function main() {
@ -370,27 +369,6 @@ describe('Traceur', function() {
});
});
// Note: `int` is not part of JS types, but rtts_assert exposes a global
// so that it can be used as well.
describe('int', function() {
it('should pass', function() {
var x:int = 10;
});
it('should fail', function() {
expect(() => {
var x:int = 'ok';
}).toThrowError('Expected an instance of int, got "ok"!');
});
it('should fail', function() {
expect(() => {
var x:int = 12.3;
}).toThrowError('Expected an instance of int, got 12.3!');
});
});
describe('generics', function() {