Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tools] Fix js multiline parsing in angular expressions #24812

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dev/i18n/extractors/__snapshots__/html.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Array [
"kbn.dashboard.id-1",
Object {
"context": "Message context 1",
"message": "Message text 1",
"message": "Message text 1 {value}",
},
],
Array [
Expand Down
22 changes: 19 additions & 3 deletions src/dev/i18n/extractors/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { createFailError } from '../../run';
*/
const ANGULAR_EXPRESSION_REGEX = /\{\{+([\s\S]*?)\}\}+/g;

const LINEBREAK_REGEX = /\n/g;
const I18N_FILTER_MARKER = '| i18n: ';

/**
Expand All @@ -53,7 +54,7 @@ function parseFilterObjectExpression(expression, messageId) {

try {
// parse an object expression instead of block statement
ast = parse(`+${expression}`);
ast = parse(`+${expression}`.replace(LINEBREAK_REGEX, ' '));
} catch (error) {
if (error instanceof SyntaxError) {
const errorWithContext = createParserErrorMessage(` ${expression}`, error);
Expand Down Expand Up @@ -98,7 +99,7 @@ function parseIdExpression(expression) {
let ast;

try {
ast = parse(expression);
ast = parse(expression.replace(LINEBREAK_REGEX, ' '));
} catch (error) {
if (error instanceof SyntaxError) {
const errorWithContext = createParserErrorMessage(expression, error);
Expand Down Expand Up @@ -217,7 +218,22 @@ function* getDirectiveMessages(htmlContent) {
}

if (element.values) {
const nodes = parse(`+${element.values}`).program.body;
let ast;

try {
ast = parse(`+${element.values}`.replace(LINEBREAK_REGEX, ' '));
} catch (error) {
if (error instanceof SyntaxError) {
const errorWithContext = createParserErrorMessage(` ${element.values}`, error);
throw createFailError(
`Couldn't parse i18n-values attribute expression:\n${errorWithContext}`
);
}

throw error;
}

const nodes = ast.program.body;
const valuesObjectNode = [...traverseNodes(nodes)].find(node => isObjectExpression(node));
const valuesKeys = extractValuesKeysFromNode(valuesObjectNode);

Expand Down
6 changes: 5 additions & 1 deletion src/dev/i18n/extractors/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ const htmlSourceBuffer = Buffer.from(`
<div>
<p
i18n-id="kbn.dashboard.id-1"
i18n-default-message="Message text 1"
i18n-default-message="Message text 1 {value}"
i18n-context="Message context 1"
i18n-values="{
value: 'Multiline
string',
}"
></p>
</div>
<div>
Expand Down