diff --git a/packages/compiler/src/util.ts b/packages/compiler/src/util.ts index 02d903634c..ba629ca02a 100644 --- a/packages/compiler/src/util.ts +++ b/packages/compiler/src/util.ts @@ -163,6 +163,8 @@ export interface OutputContext { importExpr(reference: any, typeParams?: o.Type[]|null, useSummaries?: boolean): o.Expression; } +const MAX_LENGTH_STRINGIFY = 100; + export function stringify(token: any): string { if (typeof token === 'string') { return token; @@ -184,14 +186,27 @@ export function stringify(token: any): string { return `${token.name}`; } - const res = token.toString(); + let res; + try { + res = JSON.stringify(token); + } catch { + res = token.toString(); + } if (res == null) { return '' + res; } const newLineIndex = res.indexOf('\n'); - return newLineIndex === -1 ? res : res.substring(0, newLineIndex); + if (0 < newLineIndex) { + res = res.substring(0, newLineIndex); + } + + if (MAX_LENGTH_STRINGIFY < res.length) { + res = res.substring(0, MAX_LENGTH_STRINGIFY) + '...'; + } + + return res; } /** diff --git a/packages/compiler/test/metadata_resolver_spec.ts b/packages/compiler/test/metadata_resolver_spec.ts index 2ef89defb0..b322208453 100644 --- a/packages/compiler/test/metadata_resolver_spec.ts +++ b/packages/compiler/test/metadata_resolver_spec.ts @@ -409,7 +409,7 @@ import {TEST_COMPILER_PROVIDERS} from './test_bindings'; expect(() => { resolver.getNgModuleMetadata(InvalidModule); }) .toThrowError( - `Unexpected value '[object Object]' imported by the module 'InvalidModule'. Please add a @NgModule annotation.`); + `Unexpected value '{"ngModule":true}' imported by the module 'InvalidModule'. Please add a @NgModule annotation.`); })); }); diff --git a/packages/compiler/test/util_spec.ts b/packages/compiler/test/util_spec.ts index 188c6c3739..0b14729be5 100644 --- a/packages/compiler/test/util_spec.ts +++ b/packages/compiler/test/util_spec.ts @@ -7,7 +7,8 @@ */ import {fakeAsync} from '@angular/core/testing/src/fake_async'; -import {SyncAsync, escapeRegExp, splitAtColon, utf8Encode} from '../src/util'; + +import {SyncAsync, escapeRegExp, splitAtColon, stringify, utf8Encode} from '../src/util'; { describe('util', () => { @@ -75,5 +76,23 @@ import {SyncAsync, escapeRegExp, splitAtColon, utf8Encode} from '../src/util'; ([input, output]: [string, string]) => { expect(utf8Encode(input)).toEqual(output); }); }); }); + + describe('stringify', () => { + it('should pretty print an Object', () => { + const result = stringify({hello: 'world'}); + expect(result).toBe('{"hello":"world"}'); + }); + + it('should truncate large object', () => { + const result = stringify({ + selector: 'app-root', + preserveWhitespaces: false, + templateUrl: './app.component.ng.html', + styleUrls: ['./app.component.css'] + }); + expect(result).toBe( + '{"selector":"app-root","preserveWhitespaces":false,"templateUrl":"./app.component.ng.html","styleUrl...'); + }); + }); }); -} \ No newline at end of file +}