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
This commit is contained in:
Keen Yee Liau
2018-03-09 12:01:38 -08:00
committed by Igor Minar
parent f1db789450
commit 8555a3a3cd
3 changed files with 39 additions and 5 deletions

View File

@ -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;
}
/**