Skip to content

Commit

Permalink
refactor: deprecate i18n.t
Browse files Browse the repository at this point in the history
  • Loading branch information
acid-chicken committed Jan 19, 2024
1 parent a637b4e commit 7dbf873
Show file tree
Hide file tree
Showing 84 changed files with 7,701 additions and 343 deletions.
123 changes: 91 additions & 32 deletions locales/generateDTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ts from 'typescript';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const parameterRegExp = /\{(\w+)\}/g;
const specialWordsRegExp = /^(?:\d+|const)$/;

function createMemberType(item) {
if (typeof item !== 'string') {
Expand All @@ -16,32 +17,57 @@ 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.createTupleTypeNode(
parameters.map((parameter) =>
ts.factory.createNamedTupleMember(
undefined,
ts.factory.createIdentifier(
specialWordsRegExp.test(parameter)
? `_${parameter}`
: parameter,
),
undefined,
ts.factory.createUnionTypeNode([
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
]),
),
),
),
],
)
: 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 @@ -71,10 +97,30 @@ export default function generateDTS() {
[
ts.factory.createTypeParameterDeclaration(
undefined,
ts.factory.createIdentifier('T'),
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('string'),
undefined,
ts.factory.createIdentifier('TUnion'),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
),
ts.factory.createTypeParameterDeclaration(
undefined,
ts.factory.createIdentifier('TTuple'),
ts.factory.createTypeOperatorNode(
ts.SyntaxKind.ReadonlyKeyword,
ts.factory.createArrayTypeNode(
ts.factory.createUnionTypeNode([
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
]),
),
),
ts.factory.createTypeOperatorNode(
ts.SyntaxKind.ReadonlyKeyword,
ts.factory.createArrayTypeNode(
ts.factory.createUnionTypeNode([
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
]),
),
),
),
],
Expand All @@ -87,7 +133,10 @@ export default function generateDTS() {
),
undefined,
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier('T'),
ts.factory.createUnionTypeNode([
ts.factory.createIdentifier('TUnion'),
ts.factory.createIdentifier('TTuple'),
]),
undefined,
),
),
Expand Down Expand Up @@ -115,7 +164,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 +235,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 +269,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 7dbf873

Please sign in to comment.