fix(ivy): use globally unique names for i18n constants (#25689)

Closure compiler requires that the i18n message constants of the form

const MSG_XYZ = goog.getMessage('...');

have names that are unique across an entire compilation, even if the
variables themselves are local to a given module. This means that in
practice these names must be unique in a codebase.

The best way to guarantee this requirement is met is to encode the
relative file name of the file into which the constant is being written
into the constant name itself. This commit implements that solution.

PR Close #25689
This commit is contained in:
Alex Rickabaugh
2018-08-27 11:11:02 -07:00
committed by Misko Hevery
parent bd0eb0d1d4
commit cc29b9cf93
14 changed files with 95 additions and 28 deletions

View File

@ -122,7 +122,8 @@ export class ConstantPool {
// */
// const MSG_XYZ = goog.getMsg('message');
// ```
getTranslation(message: string, meta: {description?: string, meaning?: string}): o.Expression {
getTranslation(message: string, meta: {description?: string, meaning?: string}, suffix: string):
o.Expression {
// The identity of an i18n message depends on the message and its meaning
const key = meta.meaning ? `${message}\u0000\u0000${meta.meaning}` : message;
@ -138,7 +139,7 @@ export class ConstantPool {
}
// Call closure to get the translation
const variable = o.variable(this.freshTranslationName());
const variable = o.variable(this.freshTranslationName(suffix));
const fnCall = o.variable(GOOG_GET_MSG).callFn([o.literal(message)]);
const msgStmt = variable.set(fnCall).toDeclStmt(o.INFERRED_TYPE, [o.StmtModifier.Final]);
this.statements.push(msgStmt);
@ -257,8 +258,8 @@ export class ConstantPool {
private freshName(): string { return this.uniqueName(CONSTANT_PREFIX); }
private freshTranslationName(): string {
return this.uniqueName(TRANSLATION_PREFIX).toUpperCase();
private freshTranslationName(suffix: string): string {
return this.uniqueName(TRANSLATION_PREFIX + suffix).toUpperCase();
}
private keyOf(expression: o.Expression) {