@ts-intl/dependency
Version:
I18n keys dependency analysis
37 lines (36 loc) • 1.28 kB
JavaScript
import { SyntaxKind, } from 'typescript';
export const isIntlCall = (node, opts) => {
const { funcNamePattern = '', hookNamePattern = '', richNamePattern = '', } = opts;
// t()
const funcNameRegexp = new RegExp(funcNamePattern);
if (testName(node.expression, funcNameRegexp)) {
return true;
}
const expression = node.expression;
// useHook()()
const hookNameRegexp = new RegExp(hookNamePattern);
if (expression.kind === SyntaxKind.CallExpression &&
testName(expression.expression, hookNameRegexp)) {
return true;
}
const richNameRegexp = new RegExp(richNamePattern);
if (expression.kind === SyntaxKind.PropertyAccessExpression &&
testName(expression.name, richNameRegexp)) {
const pNode = expression.expression;
// t.rich()
if (testName(pNode, funcNameRegexp))
return true;
// useHook().rich()
const hNode = pNode.expression;
if (hNode.kind === SyntaxKind.CallExpression &&
testName(hNode.expression, hookNameRegexp)) {
return true;
}
return true;
}
return false;
};
const testName = (node, regexp) => {
return (node.kind === SyntaxKind.Identifier &&
regexp.test(node.text));
};