From dda3f49952b8e12f7ec63fea8ab64fed0bb238c5 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 18 Jun 2020 11:49:18 +0100 Subject: [PATCH] refactor(compiler): add source-map spans to localized strings (#32912) Previously localized strings were not mapped to their original source location, so it was not possible to back-trace them in tools like the i18n message extractor. PR Close #32912 --- .../src/ngtsc/translator/src/translator.ts | 4 +++- .../src/render3/view/i18n/localize_utils.ts | 20 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts index 699f8e1478..c91a6bdf59 100644 --- a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts +++ b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts @@ -274,9 +274,11 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor } visitLocalizedString(ast: LocalizedString, context: Context): ts.Expression { - return this.scriptTarget >= ts.ScriptTarget.ES2015 ? + const localizedString = this.scriptTarget >= ts.ScriptTarget.ES2015 ? createLocalizedStringTaggedTemplate(ast, context, this) : createLocalizedStringFunctionCall(ast, context, this, this.imports); + this.setSourceMapRange(localizedString, ast); + return localizedString; } visitExternalExpr(ast: ExternalExpr, context: Context): ts.PropertyAccessExpression diff --git a/packages/compiler/src/render3/view/i18n/localize_utils.ts b/packages/compiler/src/render3/view/i18n/localize_utils.ts index e821749551..127a3f6d94 100644 --- a/packages/compiler/src/render3/view/i18n/localize_utils.ts +++ b/packages/compiler/src/render3/view/i18n/localize_utils.ts @@ -7,6 +7,7 @@ */ import * as i18n from '../../../i18n/i18n_ast'; import * as o from '../../../output/output_ast'; +import {ParseSourceSpan} from '../../../parse_util'; import {serializeIcuNode} from './icu_serializer'; import {formatI18nPlaceholderName} from './util'; @@ -14,13 +15,13 @@ import {formatI18nPlaceholderName} from './util'; export function createLocalizeStatements( variable: o.ReadVarExpr, message: i18n.Message, params: {[name: string]: o.Expression}): o.Statement[] { - const statements = []; - const {messageParts, placeHolders} = serializeI18nMessageForLocalize(message); - statements.push(new o.ExpressionStatement(variable.set( - o.localizedString(message, messageParts, placeHolders, placeHolders.map(ph => params[ph]))))); - - return statements; + const sourceSpan = getSourceSpan(message); + const expressions = placeHolders.map(ph => params[ph]); + const localizedString = + o.localizedString(message, messageParts, placeHolders, expressions, sourceSpan); + const variableInitialization = variable.set(localizedString); + return [new o.ExpressionStatement(variableInitialization)]; } class MessagePiece { @@ -90,6 +91,13 @@ export function serializeI18nMessageForLocalize(message: i18n.Message): return processMessagePieces(pieces); } +function getSourceSpan(message: i18n.Message): ParseSourceSpan { + const startNode = message.nodes[0]; + const endNode = message.nodes[message.nodes.length - 1]; + return new ParseSourceSpan( + startNode.sourceSpan.start, endNode.sourceSpan.end, startNode.sourceSpan.details); +} + /** * Convert the list of serialized MessagePieces into two arrays. *