Skip to content

Commit

Permalink
Support union types
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 29, 2024
1 parent 095ff5d commit f5a36f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
14 changes: 14 additions & 0 deletions openrewrite/src/java/tree/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ export namespace JavaType {
export class Class extends FullyQualified {
}

@LstType("org.openrewrite.java.tree.JavaType$MultiCatch")
export class Union implements JavaType {
private _types: JavaType[] | null = null;

get types(): JavaType[] {
return this._types!;
}

unsafeSet(types: JavaType[]): this {
this._types = types;
return this;
}
}

@LstType("org.openrewrite.java.tree.JavaType$ShallowClass")
export class ShallowClass extends FullyQualified {
}
Expand Down
33 changes: 21 additions & 12 deletions openrewrite/src/javascript/typeMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ export class JavaScriptTypeMapping {
type = this.checker.getTypeFromTypeNode(node);
}

if (type) {
const signature = this.checker.typeToString(type);
const existing = this.typeCache.get(signature);
if (existing) {
return existing;
}
const result = this.createType(node, type, signature);
this.typeCache.set(signature, result);
return result;
}
return type ? this.getType(type) : null;
}

return null;
private getType(type: ts.Type) {
const signature = this.checker.typeToString(type);
const existing = this.typeCache.get(signature);
if (existing) {
return existing;
}
const result = this.createType(type, signature);
this.typeCache.set(signature, result);
return result;
}

primitiveType(node: ts.Node): JavaType.Primitive {
Expand All @@ -51,7 +51,7 @@ export class JavaScriptTypeMapping {
return null;
}

private createType(node: ts.Node, type: ts.Type, signature: string): JavaType {
private createType(type: ts.Type, signature: string): JavaType {
if (type.isLiteral()) {
if (type.isNumberLiteral()) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Double);
Expand All @@ -76,6 +76,15 @@ export class JavaScriptTypeMapping {
return JavaType.Primitive.of(JavaType.PrimitiveKind.String);
}

if (type.isUnion()) {
let result = new JavaType.Union();
this.typeCache.set(signature, result);

const types = type.types.map(t => this.getType(t));
result.unsafeSet(types);
return result;
}

// if (ts.isRegularExpressionLiteral(node)) {
// return JavaType.Primitive.of(JavaType.PrimitiveKind.String);
// }
Expand Down

0 comments on commit f5a36f7

Please sign in to comment.