-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rosetta): fix usage of Builders in Java
* Builders were never used for structs, even though that's required. * The integrated class+struct builder wasn't always used for class translations, even if it was available. Rectify both of these issues. Relates to #2984.
- Loading branch information
Showing
9 changed files
with
84 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as ts from 'typescript'; | ||
import { inferredTypeOfExpression, mapElementType } from '../typescript/types'; | ||
import { hasFlag } from './jsii-utils'; | ||
|
||
|
||
|
||
|
||
export type ObjectLiteralAnalysis = ( | ||
{ readonly kind: 'struct', readonly type: ts.Type } | ||
| { readonly kind: 'map', readonly elementType?: ts.Type }); | ||
|
||
|
||
export function analyzeObjectLiteral(typeChecker: ts.TypeChecker, node: ts.ObjectLiteralExpression): ObjectLiteralAnalysis { | ||
const type = inferredTypeOfExpression(typeChecker, node); | ||
if (!type) { return { kind: 'map' }}; | ||
|
||
if (hasFlag(type.flags, ts.TypeFlags.Any)) { | ||
// The type checker by itself won't tell us the difference between an `any` that | ||
// was literally declared as a type in the code, vs an `any` it assumes because it | ||
// can't find a function's type declaration. | ||
// | ||
// Search for the function's declaration and only if we can't find it, | ||
// the type is actually unknown (otherwise it's a literal 'any'). | ||
const call = findEnclosingCallExpression(node); | ||
const signature = call ? typeChecker.getResolvedSignature(call) : undefined; | ||
if (!signature?.declaration) { | ||
|
||
return { kind: 'map', elementType: mapElementType(type, }; | ||
} | ||
} | ||
} | ||
|
||
function findEnclosingCallExpression(node?: ts.Node): ts.CallLikeExpression | undefined { | ||
while (node) { | ||
if (ts.isCallLikeExpression(node)) { | ||
return node; | ||
} | ||
node = node.parent; | ||
} | ||
|
||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
packages/jsii-rosetta/test/translations/structs/optional_known_struct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
new Vpc(this, "Something", new VpcProps() | ||
.argument(5)); | ||
new Vpc(this, "Something", VpcProps.builder() | ||
.argument(5) | ||
.build()); |
5 changes: 3 additions & 2 deletions
5
packages/jsii-rosetta/test/translations/structs/struct_starting_with_i.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
new Integration(this, "Something", new IntegrationOptions() | ||
.argument(5)); | ||
new Integration(this, "Something", IntegrationOptions.builder() | ||
.argument(5) | ||
.build()); |
5 changes: 3 additions & 2 deletions
5
packages/jsii-rosetta/test/translations/structs/var_new_class_known_struct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Vpc vpc = new Vpc(this, "Something", new VpcProps() | ||
.argument(5)); | ||
Object vpc = Vpc.Builder.create(this, "Something") | ||
.argument(5) | ||
.build(); |