Skip to content

[heft-localization-typings-plugin] Fix a few issues with the typings generator plugin. #4897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-localization-typings-plugin",
"comment": "Fix an issue where the `stringNamesToIgnore` option was ignored.",
"type": "patch"
}
],
"packageName": "@rushstack/heft-localization-typings-plugin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/localization-utilities",
"comment": "Fix an issue where `inferDefaultExportInterfaceNameFromFilename` did not apply.",
"type": "patch"
}
],
"packageName": "@rushstack/localization-utilities"
}
3 changes: 0 additions & 3 deletions common/config/subspaces/default/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@rushstack/heft": "^0.67.0"
},
"devDependencies": {
"@microsoft/api-extractor": "workspace:*",
"@rushstack/heft": "workspace:*",
"local-node-rig": "workspace:*",
"eslint": "~8.57.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class LocalizationTypingsPlugin implements IHeftTaskPlugin<ILocal
generatedTsFolder: `${slashNormalizedBuildFolderPath}/${generatedTsFolder ?? 'temp/loc-ts'}`,
terminal: logger.terminal,
ignoreString: stringNamesToIgnoreSet
? (stringName: string) => stringNamesToIgnoreSet.has(stringName)
? (filePath: string, stringName: string) => stringNamesToIgnoreSet.has(stringName)
: undefined,
secondaryGeneratedTsFolders
});
Expand Down
33 changes: 20 additions & 13 deletions libraries/localization-utilities/src/TypingsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import {
StringValuesTypingsGenerator,
type IStringValueTypings,
type IExportAsDefaultOptions,
type IStringValueTyping,
type ITypingsGeneratorBaseOptions
Expand Down Expand Up @@ -64,7 +65,11 @@ export class TypingsGenerator extends StringValuesTypingsGenerator {
super({
...options,
fileExtensions: ['.resx', '.resx.json', '.loc.json', '.resjson'],
parseAndGenerateTypings: (content: string, filePath: string, relativeFilePath: string) => {
parseAndGenerateTypings: (
content: string,
filePath: string,
relativeFilePath: string
): IStringValueTypings => {
const locFileData: ILocalizationFile = parseLocFile({
filePath,
content,
Expand All @@ -89,7 +94,6 @@ export class TypingsGenerator extends StringValuesTypingsGenerator {
});
}

let exportAsDefaultInterfaceName: string | undefined;
if (inferDefaultExportInterfaceNameFromFilename) {
const lastSlashIndex: number = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
let extensionIndex: number = filePath.lastIndexOf('.');
Expand All @@ -100,20 +104,23 @@ export class TypingsGenerator extends StringValuesTypingsGenerator {
const fileNameWithoutExtension: string = filePath.substring(lastSlashIndex + 1, extensionIndex);
const normalizedFileName: string = fileNameWithoutExtension.replace(/[^a-zA-Z0-9$_]/g, '');
const firstCharUpperCased: string = normalizedFileName.charAt(0).toUpperCase();
exportAsDefaultInterfaceName = `I${firstCharUpperCased}${normalizedFileName.slice(1)}`;
let interfaceName: string | undefined = `I${firstCharUpperCased}${normalizedFileName.slice(1)}`;

if (
!exportAsDefaultInterfaceName.endsWith('strings') &&
!exportAsDefaultInterfaceName.endsWith('Strings')
) {
exportAsDefaultInterfaceName += 'Strings';
if (!interfaceName.endsWith('strings') && !interfaceName.endsWith('Strings')) {
interfaceName += 'Strings';
}
}

return {
typings,
exportAsDefaultInterfaceName
};
return {
typings,
exportAsDefault: {
interfaceName
}
};
} else {
return {
typings
};
}
}
});
}
Expand Down
Loading