From 964b261a866cec4e283ee13efd58a2e402aed211 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 19 Feb 2025 17:35:20 +0200 Subject: [PATCH] refactor(compiler): improve `stringify` (#60013) This is the same change that was made in this commit (https://github.com/angular/angular/commit/cf3a5073ece9eb313bb4832b3f78aa1bfeca09c6). See its description for clarification. PR Close #60013 --- packages/compiler/src/util.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/compiler/src/util.ts b/packages/compiler/src/util.ts index d16d06c924c..024632afcf3 100644 --- a/packages/compiler/src/util.ts +++ b/packages/compiler/src/util.ts @@ -85,19 +85,16 @@ export function stringify(token: any): string { } if (Array.isArray(token)) { - return '[' + token.map(stringify).join(', ') + ']'; + return `[${token.map(stringify).join(', ')}]`; } if (token == null) { return '' + token; } - if (token.overriddenName) { - return `${token.overriddenName}`; - } - - if (token.name) { - return `${token.name}`; + const name = token.overriddenName || token.name; + if (name) { + return `${name}`; } if (!token.toString) { @@ -106,14 +103,14 @@ export function stringify(token: any): string { // WARNING: do not try to `JSON.stringify(token)` here // see https://github.com/angular/angular/issues/23440 - const res = token.toString(); + const result = token.toString(); - if (res == null) { - return '' + res; + if (result == null) { + return '' + result; } - const newLineIndex = res.indexOf('\n'); - return newLineIndex === -1 ? res : res.substring(0, newLineIndex); + const newLineIndex = result.indexOf('\n'); + return newLineIndex >= 0 ? result.slice(0, newLineIndex) : result; } export class Version {