From d9be26429a75a290dc939d2b34587e67eee0f11a Mon Sep 17 00:00:00 2001 From: Fabien Drault Date: Mon, 5 Aug 2024 17:30:43 +0200 Subject: [PATCH] feat(config): Add prettier ignore and eslint disable prettier rule config --- i18n-type.config.json | 9 -------- src/command/init.ts | 4 ++++ src/config/config-loader.ts | 4 ++++ src/templates/generate-type.ts | 5 ++--- src/templates/translations.mustache | 6 +++--- src/translation/generate-template-data.ts | 26 ++++++++++------------- 6 files changed, 24 insertions(+), 30 deletions(-) delete mode 100644 i18n-type.config.json diff --git a/i18n-type.config.json b/i18n-type.config.json deleted file mode 100644 index 3e4ba53..0000000 --- a/i18n-type.config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "input": { - "format": "flatten", - "path": "./i18n/en.json" - }, - "output": { - "path": "./i18n/translations.d.ts" - } -} \ No newline at end of file diff --git a/src/command/init.ts b/src/command/init.ts index 5143ab5..860b71a 100644 --- a/src/command/init.ts +++ b/src/command/init.ts @@ -28,6 +28,10 @@ export function init() { }, }, ], + extra: { + prettierIgnore: true, + eslintDisablePrettier: false, + }, }; console.log(`Initialize config file ${configurationFilename}\n`); writeDefaultConfiguration(defaultConfiguration, configurationFilename); diff --git a/src/config/config-loader.ts b/src/config/config-loader.ts index 3e23164..f62e3fc 100644 --- a/src/config/config-loader.ts +++ b/src/config/config-loader.ts @@ -29,4 +29,8 @@ export interface Configuration { path: string; }; rules: Rule[]; + extra?: { + prettierIgnore: boolean; + eslintDisablePrettier: boolean; + }; } diff --git a/src/templates/generate-type.ts b/src/templates/generate-type.ts index ba58d4b..1a8a71d 100644 --- a/src/templates/generate-type.ts +++ b/src/templates/generate-type.ts @@ -11,9 +11,7 @@ function openTemplate() { const templatePath = path.join(__dirname, "translations.mustache"); return fs.readFileSync(templatePath, "utf-8"); } catch (error) { - console.error( - `Error reading or parsing the template file: ${(error as Error).message}` - ); + console.error(`Error reading or parsing the template file: ${(error as Error).message}`); process.exit(1); } } @@ -41,6 +39,7 @@ export function generateType(configuration: Configuration) { const generatedType = Mustache.render(template, { keys: templateData, ...utilityChar, + ...configuration.extra, }); writeFile(generatedType, configuration.output.path); diff --git a/src/templates/translations.mustache b/src/templates/translations.mustache index f2cd7eb..ff10a57 100644 --- a/src/templates/translations.mustache +++ b/src/templates/translations.mustache @@ -1,5 +1,5 @@ -/* eslint-disable prettier/prettier */ -// prettier-ignore +{{#eslintDisablePrettier}}/* eslint-disable prettier/prettier */ +{{/eslintDisablePrettier}}{{#prettierIgnore}}// prettier-ignore{{/prettierIgnore}} /** * Generated by i18n-type-generator @@ -9,7 +9,7 @@ declare module "translations" { type Translations = { {{#keys}} - "{{key}}": {{#interpolations.length}}{{OPEN_BRACE}}{{#interpolations}} {{name}}: {{type}}{{^last}};{{/last}} {{/interpolations}}{{CLOSE_BRACE}}{{/interpolations.length}}{{^interpolations.length}}undefined{{/interpolations.length}}; + "{{key}}": {{#interpolations.length}}{{OPEN_BRACE}}{{#interpolations}} {{name}}: {{#type}}{{value}}{{^last}} | {{/last}}{{/type}}{{^last}};{{/last}} {{/interpolations}}{{CLOSE_BRACE}}{{/interpolations.length}}{{^interpolations.length}}undefined{{/interpolations.length}}; {{/keys}} }; diff --git a/src/translation/generate-template-data.ts b/src/translation/generate-template-data.ts index a0540bf..b4c720a 100644 --- a/src/translation/generate-template-data.ts +++ b/src/translation/generate-template-data.ts @@ -67,17 +67,18 @@ function toTemplateData(entries: TranslationEntry[]): TranslationEntryTemplateDa } // Set the `last` property + // TODO: could be improved by recursively check if there is any array, and adding the "last" property accordingly. for (const entry of entryMap.values()) { - const lastInterpolationIndex = entry.interpolations.length - 1; - if (lastInterpolationIndex >= 0) { - entry.interpolations[lastInterpolationIndex].last = true; - } + entry.interpolations = entry.interpolations.map((it, index, array) => ({ + ...it, + last: index === array.length - 1, + })); for (const interpolation of entry.interpolations) { - const lastTypeIndex = interpolation.type.length - 1; - if (lastTypeIndex >= 0) { - interpolation.type[lastTypeIndex].last = true; - } + interpolation.type = interpolation.type.map((type, index, array) => ({ + ...type, + last: index === array.length - 1, + })); } } @@ -88,13 +89,8 @@ function mergeUniqueTypes( existingTypes: InterpolationTypeTemplateData[], newTypes: string[] ): InterpolationTypeTemplateData[] { - const existingTypeValues = new Set(existingTypes.map((t) => t.value)); - for (const newType of newTypes) { - if (!existingTypeValues.has(newType)) { - existingTypes.push({ value: newType }); - } - } - return existingTypes; + const existingTypeValues = new Set([...existingTypes.map((t) => t.value), ...newTypes]); + return Array.from(existingTypeValues).map((it) => ({ value: it })); } function processTranslation(