Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
petebacondarwin authored and mhevery committed Feb 20, 2020
1 parent c91304f commit df75451
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -89,6 +89,24 @@ export function unwrapMessagePartsFromLocalizeCall(call: NodePath<t.CallExpressi
throw new BabelParseError(
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.');
}
}
}
}
}

Expand Down
Expand Up @@ -103,6 +103,22 @@ describe('makeEs5Plugin', () => {
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', () => {
const diagnostics = new Diagnostics();
const input = `
Expand Down

0 comments on commit df75451

Please sign in to comment.