fix(transpile): fix usage of `int` and references to `assert` module
This commit is contained in:
parent
f39c6dc2c7
commit
6f59f2f5a3
|
@ -166,8 +166,8 @@ export class ListWrapper {
|
||||||
static isEmpty(list) {
|
static isEmpty(list) {
|
||||||
return list.length == 0;
|
return list.length == 0;
|
||||||
}
|
}
|
||||||
static fill(list:List, value, start:int = 0, end:int = undefined) {
|
static fill(list:List, value, start:int = 0, end:int = null) {
|
||||||
list.fill(value, start, end);
|
list.fill(value, start, end === null ? undefined: end);
|
||||||
}
|
}
|
||||||
static equals(a:List, b:List):boolean {
|
static equals(a:List, b:List):boolean {
|
||||||
if(a.length != b.length) return false;
|
if(a.length != b.length) return false;
|
||||||
|
|
|
@ -1,12 +1,24 @@
|
||||||
import {assert} from 'rtts_assert/rtts_assert';
|
|
||||||
export {proxy} from 'rtts_assert/rtts_assert';
|
export {proxy} from 'rtts_assert/rtts_assert';
|
||||||
|
|
||||||
export var Type = Function;
|
export var Type = Function;
|
||||||
export var Math = window.Math;
|
export var Math = window.Math;
|
||||||
|
|
||||||
|
var assertionsEnabled_ = typeof assert !== 'undefined';
|
||||||
|
|
||||||
|
var int;
|
||||||
// global assert support, as Dart has it...
|
// global assert support, as Dart has it...
|
||||||
// TODO: `assert` calls need to be removed in production code!
|
// TODO: `assert` calls need to be removed in production code!
|
||||||
|
if (assertionsEnabled_) {
|
||||||
window.assert = assert;
|
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 {
|
export class FIELD {
|
||||||
constructor(definition) {
|
constructor(definition) {
|
||||||
|
@ -71,8 +83,8 @@ export class StringWrapper {
|
||||||
return s.startsWith(start);
|
return s.startsWith(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
static substring(s:string, start:int, end:int = undefined) {
|
static substring(s:string, start:int, end:int = null) {
|
||||||
return s.substring(start, end);
|
return s.substring(start, end === null ? undefined: end);
|
||||||
}
|
}
|
||||||
|
|
||||||
static replaceAllMapped(s:string, from:RegExp, cb:Function): string {
|
static replaceAllMapped(s:string, from:RegExp, cb:Function): string {
|
||||||
|
@ -157,17 +169,17 @@ export class NumberWrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function int() {};
|
var RegExp;
|
||||||
int.assert = function(value) {
|
if (assertionsEnabled_) {
|
||||||
return value == null || typeof value == 'number' && value === Math.floor(value);
|
RegExp = assert.define('RegExp', function(obj) {
|
||||||
}
|
|
||||||
|
|
||||||
export var RegExp = assert.define('RegExp', function(obj) {
|
|
||||||
assert(obj).is(assert.structure({
|
assert(obj).is(assert.structure({
|
||||||
single: window.RegExp,
|
single: window.RegExp,
|
||||||
multiple: window.RegExp
|
multiple: window.RegExp
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
RegExp = {};
|
||||||
|
}
|
||||||
|
|
||||||
export class RegExpWrapper {
|
export class RegExpWrapper {
|
||||||
static create(regExpStr, flags:string = ''):RegExp {
|
static create(regExpStr, flags:string = ''):RegExp {
|
||||||
|
@ -224,12 +236,7 @@ export function isJsObject(o):boolean {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertionsEnabled():boolean {
|
export function assertionsEnabled():boolean {
|
||||||
try {
|
return assertionsEnabled_;
|
||||||
var x:int = "string";
|
|
||||||
return false;
|
|
||||||
} catch (e) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function print(obj) {
|
export function print(obj) {
|
||||||
|
|
|
@ -230,13 +230,6 @@ function returnType(actual, T) {
|
||||||
return actual;
|
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?
|
// TODO(vojta): define these with DSL?
|
||||||
var string = type.string = define('string', function(value) {
|
var string = type.string = define('string', function(value) {
|
||||||
return typeof value === 'string';
|
return typeof value === 'string';
|
||||||
|
@ -362,7 +355,6 @@ assert.fail = fail;
|
||||||
assert.string = string;
|
assert.string = string;
|
||||||
assert.number = number;
|
assert.number = number;
|
||||||
assert.boolean = boolean;
|
assert.boolean = boolean;
|
||||||
assert.int = intType;
|
|
||||||
|
|
||||||
// custom types
|
// custom types
|
||||||
assert.arrayOf = arrayOf;
|
assert.arrayOf = arrayOf;
|
||||||
|
|
|
@ -10,8 +10,7 @@
|
||||||
// - [assert.structure](#assert-structure)
|
// - [assert.structure](#assert-structure)
|
||||||
// - [Integrating with Traceur](#integrating-with-traceur)
|
// - [Integrating with Traceur](#integrating-with-traceur)
|
||||||
|
|
||||||
import {assert} from 'rtts_assert/rtts_assert';
|
// Note: `assert` gets automatically included by traceur!
|
||||||
|
|
||||||
|
|
||||||
export function main() {
|
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() {
|
describe('generics', function() {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue