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:
parent
decd95e7f0
commit
dda3f49952
|
@ -274,9 +274,11 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor
|
||||||
}
|
}
|
||||||
|
|
||||||
visitLocalizedString(ast: LocalizedString, context: Context): ts.Expression {
|
visitLocalizedString(ast: LocalizedString, context: Context): ts.Expression {
|
||||||
return this.scriptTarget >= ts.ScriptTarget.ES2015 ?
|
const localizedString = this.scriptTarget >= ts.ScriptTarget.ES2015 ?
|
||||||
createLocalizedStringTaggedTemplate(ast, context, this) :
|
createLocalizedStringTaggedTemplate(ast, context, this) :
|
||||||
createLocalizedStringFunctionCall(ast, context, this, this.imports);
|
createLocalizedStringFunctionCall(ast, context, this, this.imports);
|
||||||
|
this.setSourceMapRange(localizedString, ast);
|
||||||
|
return localizedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
visitExternalExpr(ast: ExternalExpr, context: Context): ts.PropertyAccessExpression
|
visitExternalExpr(ast: ExternalExpr, context: Context): ts.PropertyAccessExpression
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
*/
|
*/
|
||||||
import * as i18n from '../../../i18n/i18n_ast';
|
import * as i18n from '../../../i18n/i18n_ast';
|
||||||
import * as o from '../../../output/output_ast';
|
import * as o from '../../../output/output_ast';
|
||||||
|
import {ParseSourceSpan} from '../../../parse_util';
|
||||||
|
|
||||||
import {serializeIcuNode} from './icu_serializer';
|
import {serializeIcuNode} from './icu_serializer';
|
||||||
import {formatI18nPlaceholderName} from './util';
|
import {formatI18nPlaceholderName} from './util';
|
||||||
|
@ -14,13 +15,13 @@ import {formatI18nPlaceholderName} from './util';
|
||||||
export function createLocalizeStatements(
|
export function createLocalizeStatements(
|
||||||
variable: o.ReadVarExpr, message: i18n.Message,
|
variable: o.ReadVarExpr, message: i18n.Message,
|
||||||
params: {[name: string]: o.Expression}): o.Statement[] {
|
params: {[name: string]: o.Expression}): o.Statement[] {
|
||||||
const statements = [];
|
|
||||||
|
|
||||||
const {messageParts, placeHolders} = serializeI18nMessageForLocalize(message);
|
const {messageParts, placeHolders} = serializeI18nMessageForLocalize(message);
|
||||||
statements.push(new o.ExpressionStatement(variable.set(
|
const sourceSpan = getSourceSpan(message);
|
||||||
o.localizedString(message, messageParts, placeHolders, placeHolders.map(ph => params[ph])))));
|
const expressions = placeHolders.map(ph => params[ph]);
|
||||||
|
const localizedString =
|
||||||
return statements;
|
o.localizedString(message, messageParts, placeHolders, expressions, sourceSpan);
|
||||||
|
const variableInitialization = variable.set(localizedString);
|
||||||
|
return [new o.ExpressionStatement(variableInitialization)];
|
||||||
}
|
}
|
||||||
|
|
||||||
class MessagePiece {
|
class MessagePiece {
|
||||||
|
@ -90,6 +91,13 @@ export function serializeI18nMessageForLocalize(message: i18n.Message):
|
||||||
return processMessagePieces(pieces);
|
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.
|
* Convert the list of serialized MessagePieces into two arrays.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue