From 6f59f2f5a34615cc5c9a2e5ba40c9a397dfd1c1a Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Fri, 6 Feb 2015 17:18:49 -0800 Subject: [PATCH] fix(transpile): fix usage of `int` and references to `assert` module --- modules/angular2/src/facade/collection.es6 | 4 +- modules/angular2/src/facade/lang.es6 | 47 +++++++++++-------- modules/rtts_assert/src/rtts_assert.es6 | 8 ---- modules/rtts_assert/test/rtts_assert_spec.es6 | 24 +--------- 4 files changed, 30 insertions(+), 53 deletions(-) diff --git a/modules/angular2/src/facade/collection.es6 b/modules/angular2/src/facade/collection.es6 index 58fb3b1457..b1dd79c59a 100644 --- a/modules/angular2/src/facade/collection.es6 +++ b/modules/angular2/src/facade/collection.es6 @@ -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; diff --git a/modules/angular2/src/facade/lang.es6 b/modules/angular2/src/facade/lang.es6 index 345c6bee98..7bd020aac5 100644 --- a/modules/angular2/src/facade/lang.es6 +++ b/modules/angular2/src/facade/lang.es6 @@ -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! -window.assert = assert; +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,18 +169,18 @@ export class NumberWrapper { } } -export function int() {}; -int.assert = function(value) { - return value == null || typeof value == 'number' && value === Math.floor(value); +var RegExp; +if (assertionsEnabled_) { + RegExp = assert.define('RegExp', function(obj) { + assert(obj).is(assert.structure({ + single: window.RegExp, + multiple: window.RegExp + })); + }); +} else { + RegExp = {}; } -export var RegExp = assert.define('RegExp', function(obj) { - assert(obj).is(assert.structure({ - single: window.RegExp, - multiple: window.RegExp - })); -}); - export class RegExpWrapper { static create(regExpStr, flags:string = ''):RegExp { flags = flags.replace(/g/g, ''); @@ -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) { diff --git a/modules/rtts_assert/src/rtts_assert.es6 b/modules/rtts_assert/src/rtts_assert.es6 index ef28830574..3d4c25f070 100644 --- a/modules/rtts_assert/src/rtts_assert.es6 +++ b/modules/rtts_assert/src/rtts_assert.es6 @@ -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; diff --git a/modules/rtts_assert/test/rtts_assert_spec.es6 b/modules/rtts_assert/test/rtts_assert_spec.es6 index f2a9c48f57..193e359031 100644 --- a/modules/rtts_assert/test/rtts_assert_spec.es6 +++ b/modules/rtts_assert/test/rtts_assert_spec.es6 @@ -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() {