refactor(ivy): i18n - remove hack around TS tagged literals (#34135)
When first written there was no way to specify the raw text when programmatically creating a template tagged literal AST node. This is now fixed in TS and so the hack is no longer needed. PR Close #34135
This commit is contained in:
parent
e12933a3aa
commit
80d326bd49
@ -159,25 +159,12 @@ const BLOCK_MARKER = ':';
|
|||||||
* escaped with a backslash, `\:`. This function checks for this by looking at the `raw`
|
* escaped with a backslash, `\:`. This function checks for this by looking at the `raw`
|
||||||
* messagePart, which should still contain the backslash.
|
* messagePart, which should still contain the backslash.
|
||||||
*
|
*
|
||||||
* ---
|
|
||||||
*
|
|
||||||
* If the template literal was synthesized and downleveled by TypeScript to ES5 then its
|
|
||||||
* raw array will only contain empty strings. This is because the current TypeScript compiler uses
|
|
||||||
* the original source code to find the raw text and in the case of synthesized AST nodes, there is
|
|
||||||
* no source code to draw upon.
|
|
||||||
*
|
|
||||||
* The workaround in this function is to assume that the template literal did not contain an escaped
|
|
||||||
* placeholder name, and fall back on checking the cooked array instead.
|
|
||||||
* This is a limitation if compiling to ES5 in TypeScript but is not a problem if the TypeScript
|
|
||||||
* output is ES2015 and the code is downleveled by a separate tool as happens in the Angular CLI.
|
|
||||||
*
|
|
||||||
* @param messagePart The cooked message part to process.
|
* @param messagePart The cooked message part to process.
|
||||||
* @param rawMessagePart The raw message part to check.
|
* @param rawMessagePart The raw message part to check.
|
||||||
* @returns the message part with the placeholder name stripped, if found.
|
* @returns the message part with the placeholder name stripped, if found.
|
||||||
* @throws an error if the block is unterminated
|
* @throws an error if the block is unterminated
|
||||||
*/
|
*/
|
||||||
function stripBlock(messagePart: string, rawMessagePart: string) {
|
function stripBlock(messagePart: string, rawMessagePart: string) {
|
||||||
rawMessagePart = rawMessagePart || messagePart;
|
|
||||||
return rawMessagePart.charAt(0) === BLOCK_MARKER ?
|
return rawMessagePart.charAt(0) === BLOCK_MARKER ?
|
||||||
messagePart.substring(findEndOfBlock(messagePart, rawMessagePart) + 1) :
|
messagePart.substring(findEndOfBlock(messagePart, rawMessagePart) + 1) :
|
||||||
messagePart;
|
messagePart;
|
||||||
|
@ -182,18 +182,6 @@ export function parseMetadata(cooked: string, raw: string): MessageMetadata {
|
|||||||
* Since blocks are optional, it is possible that the content of a message block actually starts
|
* Since blocks are optional, it is possible that the content of a message block actually starts
|
||||||
* with a block marker. In this case the marker must be escaped `\:`.
|
* with a block marker. In this case the marker must be escaped `\:`.
|
||||||
*
|
*
|
||||||
* ---
|
|
||||||
*
|
|
||||||
* If the template literal was synthesized and downleveled by TypeScript to ES5 then its
|
|
||||||
* raw array will only contain empty strings. This is because the current TypeScript compiler uses
|
|
||||||
* the original source code to find the raw text and in the case of synthesized AST nodes, there is
|
|
||||||
* no source code to draw upon.
|
|
||||||
*
|
|
||||||
* The workaround in this function is to assume that the template literal did not contain an escaped
|
|
||||||
* placeholder name, and fall back on checking the cooked array instead.
|
|
||||||
* This is a limitation if compiling to ES5 in TypeScript but is not a problem if the TypeScript
|
|
||||||
* output is ES2015 and the code is downlevelled by a separate tool as happens in the Angular CLI.
|
|
||||||
*
|
|
||||||
* @param cooked The cooked version of the message part to parse.
|
* @param cooked The cooked version of the message part to parse.
|
||||||
* @param raw The raw version of the message part to parse.
|
* @param raw The raw version of the message part to parse.
|
||||||
* @returns An object containing the `text` of the message part and the text of the `block`, if it
|
* @returns An object containing the `text` of the message part and the text of the `block`, if it
|
||||||
@ -201,7 +189,6 @@ export function parseMetadata(cooked: string, raw: string): MessageMetadata {
|
|||||||
* @throws an error if the `block` is unterminated
|
* @throws an error if the `block` is unterminated
|
||||||
*/
|
*/
|
||||||
export function splitBlock(cooked: string, raw: string): {text: string, block?: string} {
|
export function splitBlock(cooked: string, raw: string): {text: string, block?: string} {
|
||||||
raw = raw || cooked;
|
|
||||||
if (raw.charAt(0) !== BLOCK_MARKER) {
|
if (raw.charAt(0) !== BLOCK_MARKER) {
|
||||||
return {text: cooked};
|
return {text: cooked};
|
||||||
} else {
|
} else {
|
||||||
|
@ -61,12 +61,6 @@ describe('messages utils', () => {
|
|||||||
expect(message.messageId).toEqual('2623373088949454037');
|
expect(message.messageId).toEqual('2623373088949454037');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle raw values that are empty (from synthesized AST)', () => {
|
|
||||||
const message =
|
|
||||||
parseMessage(makeTemplateObject(['a', ':one:b', ':two:c'], ['', '', '']), [1, 2]);
|
|
||||||
expect(message.messageId).toEqual('8865273085679272414');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should extract the meaning, description and placeholder names', () => {
|
it('should extract the meaning, description and placeholder names', () => {
|
||||||
const message1 = parseMessage(makeTemplateObject(['abc'], ['abc']), []);
|
const message1 = parseMessage(makeTemplateObject(['abc'], ['abc']), []);
|
||||||
expect(message1.messageParts).toEqual(['abc']);
|
expect(message1.messageParts).toEqual(['abc']);
|
||||||
@ -128,10 +122,6 @@ describe('messages utils', () => {
|
|||||||
':block with escaped : in it:abc def', ':block with escaped \\: in it:abc def'))
|
':block with escaped : in it:abc def', ':block with escaped \\: in it:abc def'))
|
||||||
.toEqual({text: 'abc def', block: 'block with escaped : in it'});
|
.toEqual({text: 'abc def', block: 'block with escaped : in it'});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle the empty raw part', () => {
|
|
||||||
expect(splitBlock(':block info:abc def', '')).toEqual({text: 'abc def', block: 'block info'});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findEndOfBlock()', () => {
|
describe('findEndOfBlock()', () => {
|
||||||
@ -193,11 +183,5 @@ describe('messages utils', () => {
|
|||||||
id: undefined
|
id: undefined
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle the empty raw part', () => {
|
|
||||||
expect(parseMetadata(':description:abc def', ''))
|
|
||||||
.toEqual(
|
|
||||||
{text: 'abc def', meaning: undefined, description: 'description', id: undefined});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user