Skip to content

Commit

Permalink
refactor: deprecate i18n.t (#13039)
Browse files Browse the repository at this point in the history
* refactor: deprecate i18n.t

* revert: deprecate i18n.t

This reverts commit 7dbf873.

* chore: reimpl
  • Loading branch information
acid-chicken authored Jan 19, 2024
1 parent a637b4e commit 7881f06
Show file tree
Hide file tree
Showing 84 changed files with 7,311 additions and 222 deletions.
78 changes: 47 additions & 31 deletions locales/generateDTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,40 @@ function createMemberType(item) {
item.matchAll(parameterRegExp),
([, parameter]) => parameter,
);
if (!parameters.length) {
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
}
return ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('ParameterizedString'),
[
ts.factory.createUnionTypeNode(
parameters.map((parameter) =>
ts.factory.createLiteralTypeNode(
ts.factory.createStringLiteral(parameter),
return parameters.length
? ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('ParameterizedString'),
[
ts.factory.createUnionTypeNode(
parameters.map((parameter) =>
ts.factory.createStringLiteral(parameter),
),
),
),
),
],
);
],
)
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
}

function createMembers(record) {
return Object.entries(record).map(([k, v]) =>
ts.factory.createPropertySignature(
return Object.entries(record).map(([k, v]) => {
const node = ts.factory.createPropertySignature(
undefined,
ts.factory.createStringLiteral(k),
undefined,
createMemberType(v),
),
);
);
if (typeof v === 'string') {
ts.addSyntheticLeadingComment(
node,
ts.SyntaxKind.MultiLineCommentTrivia,
`*
* ${v.replace(/\n/g, '\n * ')}
`,
true,
);
}
return node;
});
}

export default function generateDTS() {
Expand Down Expand Up @@ -72,10 +80,8 @@ export default function generateDTS() {
ts.factory.createTypeParameterDeclaration(
undefined,
ts.factory.createIdentifier('T'),
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('string'),
undefined,
),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
),
],
undefined,
Expand Down Expand Up @@ -115,7 +121,6 @@ export default function generateDTS() {
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('ParameterizedString'),
[ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)],
),
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('ILocale'),
Expand Down Expand Up @@ -187,6 +192,24 @@ export default function generateDTS() {
),
ts.factory.createExportDefault(ts.factory.createIdentifier('locales')),
];
ts.addSyntheticLeadingComment(
elements[0],
ts.SyntaxKind.MultiLineCommentTrivia,
' eslint-disable ',
true,
);
ts.addSyntheticLeadingComment(
elements[0],
ts.SyntaxKind.SingleLineCommentTrivia,
' This file is generated by locales/generateDTS.js',
true,
);
ts.addSyntheticLeadingComment(
elements[0],
ts.SyntaxKind.SingleLineCommentTrivia,
' Do not edit this file directly.',
true,
);
const printed = ts
.createPrinter({
newLine: ts.NewLineKind.LineFeed,
Expand All @@ -203,12 +226,5 @@ export default function generateDTS() {
),
);

fs.writeFileSync(
`${__dirname}/index.d.ts`,
`/* eslint-disable */
// This file is generated by locales/generateDTS.js
// Do not edit this file directly.
${printed}`,
'utf-8',
);
fs.writeFileSync(`${__dirname}/index.d.ts`, printed, 'utf-8');
}
Loading

0 comments on commit 7881f06

Please sign in to comment.