Skip to content

Commit 8555a3a

Browse files
Keen Yee LiauIgorMinar
authored andcommitted
fix(compiler): Pretty print object instead of [Object object] (#22689)
The 'stringify' function prints an object as [Object object] which is not very helpful in many cases, especially in a diagnostics message. This commit changes the behavior to pretty print an object. PR Close #22689
1 parent f1db789 commit 8555a3a

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

packages/compiler/src/util.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export interface OutputContext {
163163
importExpr(reference: any, typeParams?: o.Type[]|null, useSummaries?: boolean): o.Expression;
164164
}
165165

166+
const MAX_LENGTH_STRINGIFY = 100;
167+
166168
export function stringify(token: any): string {
167169
if (typeof token === 'string') {
168170
return token;
@@ -184,14 +186,27 @@ export function stringify(token: any): string {
184186
return `${token.name}`;
185187
}
186188

187-
const res = token.toString();
189+
let res;
190+
try {
191+
res = JSON.stringify(token);
192+
} catch {
193+
res = token.toString();
194+
}
188195

189196
if (res == null) {
190197
return '' + res;
191198
}
192199

193200
const newLineIndex = res.indexOf('\n');
194-
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
201+
if (0 < newLineIndex) {
202+
res = res.substring(0, newLineIndex);
203+
}
204+
205+
if (MAX_LENGTH_STRINGIFY < res.length) {
206+
res = res.substring(0, MAX_LENGTH_STRINGIFY) + '...';
207+
}
208+
209+
return res;
195210
}
196211

197212
/**

packages/compiler/test/metadata_resolver_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ import {TEST_COMPILER_PROVIDERS} from './test_bindings';
409409

410410
expect(() => { resolver.getNgModuleMetadata(InvalidModule); })
411411
.toThrowError(
412-
`Unexpected value '[object Object]' imported by the module 'InvalidModule'. Please add a @NgModule annotation.`);
412+
`Unexpected value '{"ngModule":true}' imported by the module 'InvalidModule'. Please add a @NgModule annotation.`);
413413
}));
414414
});
415415

packages/compiler/test/util_spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
import {fakeAsync} from '@angular/core/testing/src/fake_async';
10-
import {SyncAsync, escapeRegExp, splitAtColon, utf8Encode} from '../src/util';
10+
11+
import {SyncAsync, escapeRegExp, splitAtColon, stringify, utf8Encode} from '../src/util';
1112

1213
{
1314
describe('util', () => {
@@ -75,5 +76,23 @@ import {SyncAsync, escapeRegExp, splitAtColon, utf8Encode} from '../src/util';
7576
([input, output]: [string, string]) => { expect(utf8Encode(input)).toEqual(output); });
7677
});
7778
});
79+
80+
describe('stringify', () => {
81+
it('should pretty print an Object', () => {
82+
const result = stringify({hello: 'world'});
83+
expect(result).toBe('{"hello":"world"}');
84+
});
85+
86+
it('should truncate large object', () => {
87+
const result = stringify({
88+
selector: 'app-root',
89+
preserveWhitespaces: false,
90+
templateUrl: './app.component.ng.html',
91+
styleUrls: ['./app.component.css']
92+
});
93+
expect(result).toBe(
94+
'{"selector":"app-root","preserveWhitespaces":false,"templateUrl":"./app.component.ng.html","styleUrl...');
95+
});
96+
});
7897
});
79-
}
98+
}

0 commit comments

Comments
 (0)