Skip to content
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

chore(enum-updater): add enum updater tool #33681

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update spacing for code updates
  • Loading branch information
paulhcsun committed Feb 27, 2025
commit e464a45fbace061d1eea3666de9079680630191d
13 changes: 12 additions & 1 deletion tools/@aws-cdk/construct-metadata-updater/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
// import { EnumLikeUpdater } from './metadata-updater';
import { EnumLikeUpdater } from './metadata-updater';
// TODO: Uncomment out the rest of the updaters and tweak the import before raising a PR.
// If you're reading this on GitHub, someone forgot to double-check!
export function main() {
const dir = '../../../../packages/'

// new ConstructsUpdater(dir).execute();
// console.log('Constructs updater finished.');
// new ConstructsUpdater(dir).execute();
// console.log('Constructs updater finished.');

// new PropertyUpdater(dir).execute();
// console.log('Property updater finished.')
// new PropertyUpdater(dir).execute();
// console.log('Property updater finished.')

// new EnumsUpdater(dir).execute();
// console.log('Enums updater finished.');
// new EnumsUpdater(dir).execute();
// console.log('Enums updater finished.');

// new MethodsUpdater(dir).execute();
// console.log('Methods updater finished.');
// new MethodsUpdater(dir).execute();
// console.log('Methods updater finished.');

// new EnumLikeUpdater(dir).TEST();
// // console.log('Enum-like updater finished.');

new EnumLikeUpdater(dir).TEST();
// console.log('Enum-like updater finished.');
}
50 changes: 37 additions & 13 deletions tools/@aws-cdk/construct-metadata-updater/lib/metadata-updater.ts
Original file line number Diff line number Diff line change
@@ -840,7 +840,7 @@ export class EnumLikeUpdater extends MetadataUpdater {
}

/**
* Update a single enum-like value
* Update a single enum value
* @param enumName The enum name
* @param missingValue The dictionary from the `missing-values.json` file
* containing the cdk_path and missing_values for the enum
@@ -860,24 +860,48 @@ export class EnumLikeUpdater extends MetadataUpdater {

console.log(`=====\nUpdating enum ${enumName} in ${missingValue['cdk_path']} \n=====`);

// Get the new enum values to add (no need to worry about sorting or checking existing values)
const newEnumValues = missingValue['missing_values'];

// Add each new enum value at the end of the enum
for (const enumVal of newEnumValues) {
// Format the enum constant name (uppercase and replace hyphens or periods with underscores)
// Get the full text of the enum
let enumText = enumDeclaration.getFullText();

// Get just the enum body (everything between the curly braces)
const enumBodyStart = enumText.indexOf('{') + 1;
const enumBodyEnd = enumText.lastIndexOf('}');
const enumBody = enumText.substring(enumBodyStart, enumBodyEnd);

// Check for double line breaks only in the enum body
const hasDoubleLineBreaks = enumBody.includes('\n\n');

// Find the position to insert new members (just before the closing brace)
const insertPosition = enumText.lastIndexOf('}');

// Prepare the text to insert - only add initial newline if enum uses double line breaks
let textToInsert = hasDoubleLineBreaks ? '\n' : '';

newEnumValues.forEach((enumVal: string, index: number) => {
const enumConstantName = enumVal.toUpperCase().replace(/[^A-Z0-9]+/g, '_').replace(/_+$/, '');

textToInsert += ` /**\n * [PLACEHOLDER FOR: TO BE FILLED OUT]\n */\n`;
textToInsert += ` ${enumConstantName} = '${enumVal}'`;

// Add a comma and appropriate newlines after each member
textToInsert += ',';

// Add newlines after each member except the last one
if (index < newEnumValues.length - 1) {
textToInsert += hasDoubleLineBreaks ? '\n\n' : '\n';
}
});

// Add the missing enum value with a placeholder doc comment
const newEnumMember = enumDeclaration.addMember({
name: enumConstantName,
initializer: `'${enumVal}'`, // Ensure the value is a string literal
});
// Add final newline before the closing brace
textToInsert += '\n';

newEnumMember.addJsDoc("\n[PLACEHOLDER FOR: TO BE FILLED OUT]"); // Add temp docstring comment
// Insert the new text
enumText = enumText.slice(0, insertPosition) + textToInsert + enumText.slice(insertPosition);

console.log(`=====\nAdded missing enum-like value ${enumVal} to ${enumName}\n=====`);
}
// Set the full text of the enum
enumDeclaration.replaceWithText(enumText);

// Write the updated file back to disk
sourceFile.saveSync();