-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathuseTranslationHook.ts
102 lines (89 loc) · 2.88 KB
/
useTranslationHook.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as BabelCore from '@babel/core';
import * as BabelTypes from '@babel/types';
import { CommentHint, getCommentHintForPath } from '../comments';
import { Config } from '../config';
import { ExtractedKey } from '../keys';
import {
getFirstOrNull,
evaluateIfConfident,
referencesImport,
} from './commons';
import extractTFunction from './tFunction';
/**
* Check whether a given CallExpression path is a call to `useTranslation` hook.
* @param path: node path to check
* @returns true if the given call expression is indeed a call to
* `useTranslation`
*/
function isUseTranslationHook(
path: BabelCore.NodePath<BabelTypes.CallExpression>,
): boolean {
const callee = path.get('callee');
return referencesImport(callee, 'react-i18next', 'useTranslation');
}
/**
* Parse `useTranslation()` hook to extract all its translation keys and
* options.
* @param path: useTranslation call node path.
* @param config: plugin configuration
* @param commentHints: parsed comment hints
*/
export default function extractUseTranslationHook(
path: BabelCore.NodePath<BabelTypes.CallExpression>,
config: Config,
commentHints: CommentHint[] = [],
skipCheck = false,
): ExtractedKey[] {
if (!skipCheck && !isUseTranslationHook(path)) return [];
let ns: string | null;
const nsCommentHint = getCommentHintForPath(path, 'NAMESPACE', commentHints);
if (nsCommentHint) {
// We got a comment hint, take its value as namespace.
ns = nsCommentHint.value;
} else {
// Otherwise, try to get namespace from arguments.
const namespaceArgument = path.get('arguments')[0];
ns = getFirstOrNull(evaluateIfConfident(namespaceArgument));
}
const parentPath = path.parentPath;
if (!parentPath.isVariableDeclarator()) return [];
const id = parentPath.get('id');
const tBinding = id.scope.bindings['t'];
if (!tBinding) return [];
let keyPrefix: string | null = null;
const optionsArgument = path.get('arguments')[1];
const options = getFirstOrNull(evaluateIfConfident(optionsArgument));
if (options) {
keyPrefix = options.keyPrefix || keyPrefix;
}
let keys = Array<ExtractedKey>();
for (const reference of tBinding.referencePaths) {
if (
reference.parentPath?.isCallExpression() &&
reference.parentPath.get('callee') === reference
) {
keys = [
...keys,
...extractTFunction(
reference.parentPath,
config,
commentHints,
true,
).map((k) => ({
// Add namespace if it was not explicitely set in t() call.
...k,
parsedOptions: {
...k.parsedOptions,
keyPrefix: k.parsedOptions.keyPrefix || keyPrefix,
ns: k.parsedOptions.ns || ns,
},
})),
];
}
}
return keys.map((k) => ({
...k,
sourceNodes: [path.node, ...k.sourceNodes],
extractorName: extractUseTranslationHook.name,
}));
}