Skip to content

Commit

Permalink
feat(config): Add prettier ignore and eslint disable prettier rule co…
Browse files Browse the repository at this point in the history
…nfig
  • Loading branch information
fdrault committed Aug 5, 2024
1 parent e19b1ba commit d9be264
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 30 deletions.
9 changes: 0 additions & 9 deletions i18n-type.config.json

This file was deleted.

4 changes: 4 additions & 0 deletions src/command/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function init() {
},
},
],
extra: {
prettierIgnore: true,
eslintDisablePrettier: false,
},
};
console.log(`Initialize config file ${configurationFilename}\n`);
writeDefaultConfiguration(defaultConfiguration, configurationFilename);
Expand Down
4 changes: 4 additions & 0 deletions src/config/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ export interface Configuration {
path: string;
};
rules: Rule[];
extra?: {
prettierIgnore: boolean;
eslintDisablePrettier: boolean;
};
}
5 changes: 2 additions & 3 deletions src/templates/generate-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -41,6 +39,7 @@ export function generateType(configuration: Configuration) {
const generatedType = Mustache.render(template, {
keys: templateData,
...utilityChar,
...configuration.extra,
});

writeFile(generatedType, configuration.output.path);
Expand Down
6 changes: 3 additions & 3 deletions src/templates/translations.mustache
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}}
};

Expand Down
26 changes: 11 additions & 15 deletions src/translation/generate-template-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
}
}

Expand All @@ -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(
Expand Down

0 comments on commit d9be264

Please sign in to comment.