Skip to content

Commit

Permalink
Reset translator comments when babel loads a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Nov 19, 2024
1 parent cd13809 commit 8b8008a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/tools/extract-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,17 @@ function getPrecedingComment(line, comments) {

const makeI18nPlugin = cfg => {
const entries = [];
const translatorComments = {};
let currFile = '';
let translatorComments = {};
const i18nPlugin = ({types}) => {
return {
visitor: {
Program(path) {
Program(path, state) {
if (currFile !== state.file.opts.filename) {
// clear translator comments when the file changes
currFile = state.file.opts.filename;
translatorComments = {};
}
path.container.comments
.filter(comment => comment.value.trim().startsWith(TRANSLATOR_COMMENT_TAG))
.forEach(comment => {
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions test-data/comments2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable babel/quotes, react/prop-types, react/jsx-curly-brace-presence */

import {makeComponents} from '../src/client';
const {Translate, PluralTranslate} = makeComponents();

// Keep this call on the same line as the corresponding call in comments1.jsx
// This tests that the comments do not get mixed up when babel loads another file
Translate.string('baz');
// Same for this call
PluralTranslate.string('baz', 'bazs', 42);
4 changes: 3 additions & 1 deletion tests/__snapshots__/extract.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,9 @@ msgstr \\"\\"
#. translator comment
msgid \\"baz\\"
msgstr \\"\\"
msgid_plural \\"bazs\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#. Title
msgid \\"Hello & World\\"
Expand Down
18 changes: 16 additions & 2 deletions tests/extract.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import extractFromFiles from '../src/tools/extract';

const expectExtracted = (file, headers, base, addLocation = 'full') =>
expect(extractFromFiles([file], {base: base || process.cwd(), addLocation}, headers, false));
expect(
extractFromFiles(
Array.isArray(file) ? file : [file],
{base: base || process.cwd(), addLocation},
headers,
false
)
);

test('Messages are properly extracted', () => {
expectExtracted('test-data/example.jsx', {Custom: 'Headers'}).toMatchSnapshot();
Expand Down Expand Up @@ -35,5 +42,12 @@ test('Non-string call ignored', () => {
});

test('Translator comments are properly extracted', () => {
expectExtracted('test-data/comments.jsx', undefined, undefined, 'never').toMatchSnapshot();
// Test with multiple files to ensure the internal state (i.e. translatorComments) is
// properly reset when the file changes
expectExtracted(
['test-data/comments1.jsx', 'test-data/comments2.jsx'],
undefined,
undefined,
'never'
).toMatchSnapshot();
});

0 comments on commit 8b8008a

Please sign in to comment.