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 pug regular expression #24006

Merged
merged 3 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion src/dev/i18n/extractors/__snapshots__/pug.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`dev/i18n/extractors/pug extracts messages from pug template 1`] = `
exports[`dev/i18n/extractors/pug extracts messages from pug template with interpolation 1`] = `
Array [
"message-id",
Object {
"context": "Message context",
"message": "Default message",
},
]
`;

exports[`dev/i18n/extractors/pug extracts messages from pug template without interpolation 1`] = `
Array [
"message-id",
Object {
Expand Down
2 changes: 1 addition & 1 deletion src/dev/i18n/extractors/pug.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { createFailError } from '../../run';
/**
* Matches `i18n(...)` in `#{i18n('id', { defaultMessage: 'Message text' })}`
*/
const PUG_I18N_REGEX = /(?<=\#\{)i18n\((([^)']|'([^'\\]|\\.)*')*\)(?=\}))/g;
const PUG_I18N_REGEX = /i18n\((([^)']|'([^'\\]|\\.)*')*)\)/g;

/**
* Example: `#{i18n('message-id', { defaultMessage: 'Message text' })}`
Expand Down
13 changes: 11 additions & 2 deletions src/dev/i18n/extractors/pug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { extractPugMessages } from './pug';

describe('dev/i18n/extractors/pug', () => {
test('extracts messages from pug template', () => {
test('extracts messages from pug template with interpolation', () => {
const source = Buffer.from(`\
#{i18n('message-id', { defaultMessage: 'Default message', context: 'Message context' })}
`);
Expand All @@ -29,9 +29,18 @@ describe('dev/i18n/extractors/pug', () => {
expect(messageObject).toMatchSnapshot();
});

test('extracts messages from pug template without interpolation', () => {
const source = Buffer.from(`\
.kibanaWelcomeText(data-error-message=i18n('message-id', { defaultMessage: 'Default message', context: 'Message context' }))
`);
const [messageObject] = extractPugMessages(source);

expect(messageObject).toMatchSnapshot();
});

test('throws on empty id', () => {
const source = Buffer.from(`\
#{i18n('', { defaultMessage: 'Default message', context: 'Message context' })}
h1= i18n('', { defaultMessage: 'Default message', context: 'Message context' })
LeanidShutau marked this conversation as resolved.
Show resolved Hide resolved
`);

expect(() => extractPugMessages(source).next()).toThrowErrorMatchingSnapshot();
Expand Down