fix(localize): support minified ES5 `$localize` calls (#35562)

Some minification tooling modifies `$localize` calls
to contain a comma sequence of items, where the
cooked and raw values are assigned to variables.

This change improves our ability to recognize these
structures.

Fixes #35376

PR Close #35562
This commit is contained in:
Pete Bacon Darwin 2020-02-19 21:58:28 +01:00 committed by Miško Hevery
parent c91304f191
commit df75451a0c
2 changed files with 34 additions and 0 deletions

View File

@ -89,6 +89,24 @@ export function unwrapMessagePartsFromLocalizeCall(call: NodePath<t.CallExpressi
throw new BabelParseError( throw new BabelParseError(
cooked.node, 'Unexpected "makeTemplateObject()" function (expected an expression).'); cooked.node, 'Unexpected "makeTemplateObject()" function (expected an expression).');
} }
} else if (right.isSequenceExpression()) {
const expressions = right.get('expressions');
if (expressions.length > 2) {
// This is a minified sequence expression, where the first two expressions in the sequence
// are assignments of the cooked and raw arrays respectively.
const [first, second] = expressions;
if (first.isAssignmentExpression() && second.isAssignmentExpression()) {
cooked = first.get('right');
if (!cooked.isExpression()) {
throw new BabelParseError(
first.node, 'Unexpected cooked value, expected an expression.');
}
raw = second.get('right');
if (!raw.isExpression()) {
throw new BabelParseError(second.node, 'Unexpected raw value, expected an expression.');
}
}
}
} }
} }

View File

@ -103,6 +103,22 @@ describe('makeEs5Plugin', () => {
expect(output.code).toEqual('"try" + (40 + 2) + "me";'); expect(output.code).toEqual('"try" + (40 + 2) + "me";');
}); });
it('should handle minified code', () => {
const diagnostics = new Diagnostics();
const input = `$localize(
cachedObj||
(
cookedParts=['try', 'me'],
rawParts=['try', 'me'],
Object.defineProperty?
Object.defineProperty(cookedParts,"raw",{value:rawParts}):
cookedParts.raw=rawParts,
cachedObj=cookedParts
),40 + 2)`;
const output = transformSync(input, {plugins: [makeEs5TranslatePlugin(diagnostics, {})]}) !;
expect(output.code).toEqual('"try" + (40 + 2) + "me";');
});
it('should handle lazy-load helper calls', () => { it('should handle lazy-load helper calls', () => {
const diagnostics = new Diagnostics(); const diagnostics = new Diagnostics();
const input = ` const input = `