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

fix(rosetta): python identifiers mishandle identifier translations with capital letters #4644

Merged
merged 3 commits into from
Oct 11, 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
4 changes: 2 additions & 2 deletions packages/jsii-rosetta/lib/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class PythonVisitor extends DefaultVisitor<PythonLanguageContext> {
* Bump this when you change something in the implementation to invalidate
* existing cached translations.
*/
public static readonly VERSION = '2';
public static readonly VERSION = '3';

public readonly language = TargetLanguage.PYTHON;
public readonly defaultContext = {};
Expand Down Expand Up @@ -818,7 +818,7 @@ function mangleIdentifier(originalIdentifier: string) {
return originalIdentifier;
}
// Turn into snake-case
const cased = originalIdentifier.replace(/[^A-Z][A-Z]/g, (m) => `${m[0].slice(0, 1)}_${m.slice(1).toLowerCase()}`);
const cased = originalIdentifier.replace(/[A-Z]/g, (m) => `_${m.toLowerCase()}`);
return IDENTIFIER_KEYWORDS.includes(cased) ? `${cased}_` : cased;
}

Expand Down
30 changes: 30 additions & 0 deletions packages/jsii-rosetta/test/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,33 @@ test('declarations are translated correctly in all jsii languages', () => {
assembly.cleanup();
}
});

test('handling capital letters in identifiers', () => {
// Create an assembly in a temp directory
const assembly = TestJsiiModule.fromSource(
{
'index.ts': `
export interface InterfaceA {
readonly sizeInMBs: Number;
}
`,
},
{
name: 'my_assembly',
jsii: DUMMY_JSII_CONFIG,
},
);
try {
const ts = assembly.translateHere(
["import * as masm from 'my_assembly';", 'declare let intA: masm.InterfaceA;', 'intA = { sizeInMBs: 3 };'].join('\n'),
);

expect(ts.get(TargetLanguage.PYTHON)?.source).toEqual(
['import example_test_demo as masm', '# int_a: masm.InterfaceA', 'int_a = masm.InterfaceA(size_in_m_bs=3)'].join('\n'),
);
expect(ts.get(TargetLanguage.JAVA)?.source).toEqual(['import example.test.demo.*;', 'InterfaceA intA;', 'intA = InterfaceA.builder().sizeInMBs(3).build();'].join('\n'));
expect(ts.get(TargetLanguage.CSHARP)?.source).toEqual(['using Example.Test.Demo;', 'InterfaceA intA;', 'intA = new InterfaceA { SizeInMBs = 3 };'].join('\n'));
} finally {
assembly.cleanup();
}
});
Loading