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
This commit is contained in:
Pete Bacon Darwin 2020-06-18 11:49:18 +01:00 committed by Andrew Kushnir
parent decd95e7f0
commit dda3f49952
2 changed files with 17 additions and 7 deletions

View File

@ -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

View File

@ -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.
*