From e54e865c34077a816acf4c6653a6531a59781828 Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Tue, 7 Mar 2017 10:31:28 +0300 Subject: [PATCH] fix(core): stringify shouldn't throw when toString returns null/undefined (#14975) Fixes #14948 PR Close #14975 --- packages/core/src/util.ts | 5 +++++ packages/core/test/util_spec.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/core/test/util_spec.ts diff --git a/packages/core/src/util.ts b/packages/core/src/util.ts index 217bb6ffff4f3..800f57109178d 100644 --- a/packages/core/src/util.ts +++ b/packages/core/src/util.ts @@ -69,6 +69,11 @@ export function stringify(token: any): string { } const res = token.toString(); + + if (res == null) { + return '' + res; + } + const newLineIndex = res.indexOf('\n'); return newLineIndex === -1 ? res : res.substring(0, newLineIndex); } diff --git a/packages/core/test/util_spec.ts b/packages/core/test/util_spec.ts new file mode 100644 index 0000000000000..f1138eaa69d21 --- /dev/null +++ b/packages/core/test/util_spec.ts @@ -0,0 +1,19 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {stringify} from '../src/util'; + +export function main() { + describe('stringify', () => { + it('should return string undefined when toString returns undefined', + () => expect(stringify({toString: (): string => undefined})).toBe('undefined')); + + it('should return string null when toString returns null', + () => expect(stringify({toString: (): string => null})).toBe('null')); + }); +}