mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[Tools] Add string concatenation parsing (#25381)
This commit is contained in:
parent
dc956a0a79
commit
b166eda54f
3 changed files with 35 additions and 12 deletions
|
@ -16,6 +16,8 @@ exports[`i18n utils should not escape linebreaks 1`] = `
|
|||
"
|
||||
`;
|
||||
|
||||
exports[`i18n utils should parse string concatenation 1`] = `"Very long concatenated string"`;
|
||||
|
||||
exports[`i18n utils should throw if "values" has a value that is unused in the message 1`] = `
|
||||
"\\"values\\" object contains unused properties (\\"namespace.message.id\\"):
|
||||
[url]."
|
||||
|
|
|
@ -26,6 +26,7 @@ import {
|
|||
isObjectProperty,
|
||||
isStringLiteral,
|
||||
isTemplateLiteral,
|
||||
isBinaryExpression,
|
||||
} from '@babel/types';
|
||||
import fs from 'fs';
|
||||
import glob from 'glob';
|
||||
|
@ -212,7 +213,7 @@ function parseTemplateLiteral(node, messageId) {
|
|||
return node.quasis[0].value.cooked;
|
||||
}
|
||||
|
||||
export function extractMessageValueFromNode(node, messageId) {
|
||||
function extractStringFromNode(node, messageId, errorMessage) {
|
||||
if (isStringLiteral(node)) {
|
||||
return node.value;
|
||||
}
|
||||
|
@ -221,21 +222,28 @@ export function extractMessageValueFromNode(node, messageId) {
|
|||
return parseTemplateLiteral(node, messageId);
|
||||
}
|
||||
|
||||
throw createFailError(
|
||||
if (isBinaryExpression(node, { operator: '+' })) {
|
||||
return (
|
||||
extractStringFromNode(node.left, messageId, errorMessage) +
|
||||
extractStringFromNode(node.right, messageId, errorMessage)
|
||||
);
|
||||
}
|
||||
|
||||
throw createFailError(errorMessage);
|
||||
}
|
||||
|
||||
export function extractMessageValueFromNode(node, messageId) {
|
||||
return extractStringFromNode(
|
||||
node,
|
||||
messageId,
|
||||
`defaultMessage value should be a string or template literal ("${messageId}").`
|
||||
);
|
||||
}
|
||||
|
||||
export function extractDescriptionValueFromNode(node, messageId) {
|
||||
if (isStringLiteral(node)) {
|
||||
return node.value;
|
||||
}
|
||||
|
||||
if (isTemplateLiteral(node)) {
|
||||
return parseTemplateLiteral(node, messageId);
|
||||
}
|
||||
|
||||
throw createFailError(
|
||||
return extractStringFromNode(
|
||||
node,
|
||||
messageId,
|
||||
`description value should be a string or template literal ("${messageId}").`
|
||||
);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
*/
|
||||
|
||||
import { parse } from '@babel/parser';
|
||||
import { isExpressionStatement, isObjectExpression } from '@babel/types';
|
||||
import { isExpressionStatement, isObjectExpression, isObjectProperty } from '@babel/types';
|
||||
|
||||
import {
|
||||
isI18nTranslateFunction,
|
||||
|
@ -27,6 +27,7 @@ import {
|
|||
formatJSString,
|
||||
checkValuesProperty,
|
||||
createParserErrorMessage,
|
||||
extractMessageValueFromNode,
|
||||
} from './utils';
|
||||
|
||||
const i18nTranslateSources = ['i18n', 'i18n.translate'].map(
|
||||
|
@ -153,4 +154,16 @@ describe('i18n utils', () => {
|
|||
checkValuesProperty(valuesKeys, defaultMessage, messageId)
|
||||
).toThrowErrorMatchingSnapshot();
|
||||
});
|
||||
|
||||
test(`should parse string concatenation`, () => {
|
||||
const source = `
|
||||
i18n('namespace.id', {
|
||||
defaultMessage: 'Very ' + 'long ' + 'concatenated ' + 'string',
|
||||
});`;
|
||||
const objectProperty = [...traverseNodes(parse(source).program.body)].find(node =>
|
||||
isObjectProperty(node)
|
||||
);
|
||||
|
||||
expect(extractMessageValueFromNode(objectProperty.value)).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue