Skip to content

Commit

Permalink
fix(rosetta): enum resolution breaks for properties
Browse files Browse the repository at this point in the history
On some examples in the CDK repository, Rosetta breaks in the
`resolveEnumLiteral()` function. The error is `Cannot read property
'kind' of undefined` and occurs somewhere deep in the call stack of
TypeScript.

It occurs when an enum is being passed as a struct property.
  • Loading branch information
rix0rrr committed Nov 19, 2021
1 parent 251f56d commit 61747b6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/jsii-rosetta/lib/jsii/jsii-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ export function resolveEnumLiteral(typeChecker: ts.TypeChecker, type: ts.Type) {
return type;
}

const parentDeclaration = type.symbol.declarations?.[0]?.parent;
return fmap(parentDeclaration, typeChecker.getTypeAtLocation) ?? type;
return typeChecker.getBaseTypeOfLiteralType(type);
}

export function resolvedSymbolAtLocation(typeChecker: ts.TypeChecker, node: ts.Node) {
Expand Down
43 changes: 42 additions & 1 deletion packages/jsii-rosetta/test/jsii-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ describe('no submodule', () => {
beforeAll(async () => {
module = await TestJsiiModule.fromSource(
{
'index.ts': `export enum MyEnum { OPTION_A = 'a', OPTION_B = 'b' }`,
'index.ts': `export enum MyEnum { OPTION_A = 'a', OPTION_B = 'b' }
export interface MyProps { readonly prop: MyEnum }
export function myFun(props: MyProps) {
Array.isArray(props);
}
`,
},
{
name: 'my_assembly',
Expand Down Expand Up @@ -273,6 +280,40 @@ describe('no submodule', () => {
]);
});
});

describe('implicit import', () => {
let trans: TranslatedSnippet;
beforeAll(() => {
trans = module.translateHere(
`import * as masm from 'my_assembly';
masm.myFun({ prop: masm.MyEnum.OPTION_A });
`,
);
});

test('to Python', () => {
expectTranslation(trans, TargetLanguage.PYTHON, [
'import example_test_demo as masm',
'masm.my_fun(prop=masm.MyEnum.OPTION_A)',
]);
});

test('to Java', () => {
// eslint-disable-next-line prettier/prettier
expectTranslation(trans, TargetLanguage.JAVA, [
'import example.test.demo.*;',
'myFun(MyProps.builder().prop(MyEnum.OPTION_A).build());',
]);
});

test('to C#', () => {
// eslint-disable-next-line prettier/prettier
expectTranslation(trans, TargetLanguage.CSHARP, [
'using Example.Test.Demo;',
'MyFun(new MyProps { Prop = MyEnum.OPTION_A });',
]);
});
});
});
});

Expand Down

0 comments on commit 61747b6

Please sign in to comment.