Skip to content

Commit

Permalink
fix: detect getTranslate in server components (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 authored Jan 27, 2025
1 parent bcc2461 commit de1a28d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/extractor/parserReact/tokenMergers/useTranslateMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const useTranslateMerger = {

switch (state) {
case S.Idle:
if (type === 'function.call' && token === 'useTranslate') {
if (
type === 'function.call' &&
(token === 'useTranslate' || token === 'getTranslate')
) {
return S.ExpectBracket;
}
break;
Expand Down
18 changes: 17 additions & 1 deletion test/unit/extractor/react/useTranslate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function extractReactKeys(
describe.each(['js', 'ts', 'jsx', 'tsx'])('useTranslate (.%s)', (ext) => {
const FILE_NAME = `test.${ext}`;

it('extracts from the t call with signature t(string))', async () => {
it('extracts from the t call with signature t(string) and useTranslate', async () => {
const expected = [{ keyName: 'key1', line: 5 }];

const code = `
Expand All @@ -36,6 +36,22 @@ describe.each(['js', 'ts', 'jsx', 'tsx'])('useTranslate (.%s)', (ext) => {
expect(extracted.keys).toEqual(expected);
});

it('extracts from the t call with signature t(string) and getTranslate', async () => {
const expected = [{ keyName: 'key1', line: 5 }];

const code = `
import '@tolgee/react'
async function Test () {
const t = await getTranslate()
t('key1')
}
`;

const extracted = await extractReactKeys(code, FILE_NAME);
expect(extracted.warnings).toEqual([]);
expect(extracted.keys).toEqual(expected);
});

it('extracts from the t call with signature t(string, string)', async () => {
const expected = [
{ keyName: 'key1', defaultValue: 'default value', line: 5 },
Expand Down

0 comments on commit de1a28d

Please sign in to comment.