Make ICU types stricter for inline JSX
This commit is contained in:
parent
9e7a6ea8bc
commit
6655bfc576
52 changed files with 220 additions and 200 deletions
|
@ -50,11 +50,27 @@ const messageKeys = Object.keys(globalMessages).sort((a, b) => {
|
|||
return a.localeCompare(b);
|
||||
}) as Array<keyof typeof globalMessages>;
|
||||
|
||||
function generateType(
|
||||
name: string,
|
||||
stringType: ts.TypeNode,
|
||||
componentType: ts.TypeNode
|
||||
): ts.Statement {
|
||||
function filterDefaultParams(params: Map<string, ICUMessageParamType>) {
|
||||
const filteredParams = new Map<string, ICUMessageParamType>();
|
||||
|
||||
for (const [key, value] of params) {
|
||||
if (key === 'emojify') {
|
||||
continue;
|
||||
}
|
||||
|
||||
filteredParams.set(key, value);
|
||||
}
|
||||
|
||||
return filteredParams;
|
||||
}
|
||||
|
||||
const ComponentOrStringNode =
|
||||
ts.factory.createTypeReferenceNode('ComponentOrString');
|
||||
const ComponentNode = ts.factory.createTypeReferenceNode('Component');
|
||||
const StringToken = ts.factory.createToken(ts.SyntaxKind.StringKeyword);
|
||||
const NeverToken = ts.factory.createToken(ts.SyntaxKind.NeverKeyword);
|
||||
|
||||
function generateType(name: string, supportsComponents: boolean): ts.Statement {
|
||||
const props = new Array<ts.TypeElement>();
|
||||
for (const key of messageKeys) {
|
||||
if (key === 'smartling') {
|
||||
|
@ -70,7 +86,21 @@ function generateType(
|
|||
|
||||
const { messageformat } = message;
|
||||
|
||||
const params = getICUMessageParams(messageformat);
|
||||
const rawParams = getICUMessageParams(messageformat);
|
||||
const params = filterDefaultParams(rawParams);
|
||||
|
||||
if (!supportsComponents) {
|
||||
const needsComponents = Array.from(rawParams.values()).some(value => {
|
||||
return value.type === 'jsx';
|
||||
});
|
||||
|
||||
if (needsComponents) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const stringType = supportsComponents ? ComponentOrStringNode : StringToken;
|
||||
const componentType = supportsComponents ? ComponentNode : NeverToken;
|
||||
|
||||
let paramType: ts.TypeNode;
|
||||
if (params.size === 0) {
|
||||
|
@ -193,21 +223,9 @@ statements.push(
|
|||
)
|
||||
);
|
||||
|
||||
statements.push(
|
||||
generateType(
|
||||
'ICUJSXMessageParamsByKeyType',
|
||||
ts.factory.createTypeReferenceNode('ComponentOrString'),
|
||||
ts.factory.createTypeReferenceNode('Component')
|
||||
)
|
||||
);
|
||||
statements.push(generateType('ICUJSXMessageParamsByKeyType', true));
|
||||
|
||||
statements.push(
|
||||
generateType(
|
||||
'ICUStringMessageParamsByKeyType',
|
||||
ts.factory.createToken(ts.SyntaxKind.StringKeyword),
|
||||
ts.factory.createToken(ts.SyntaxKind.NeverKeyword)
|
||||
)
|
||||
);
|
||||
statements.push(generateType('ICUStringMessageParamsByKeyType', false));
|
||||
|
||||
const root = ts.factory.createSourceFile(
|
||||
statements,
|
||||
|
|
|
@ -40,7 +40,7 @@ async function main() {
|
|||
|
||||
// Find uses in either:
|
||||
// - `i18n('key')`
|
||||
// - `<Intl id="key"/>`
|
||||
// - `<I18n id="key"/>`
|
||||
|
||||
try {
|
||||
const result = await execa(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue