Skip to content

Commit

Permalink
Implement some more runtime stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 12, 2024
1 parent abed961 commit c2295c0
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 11 deletions.
6 changes: 3 additions & 3 deletions openrewrite/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export class ListUtils {
static map<T>(ls: T[] | null, map: (item: T) => T): T[] {
static map<T>(ls: T[] | null, map: (item: T) => T | null): T[] {
if (ls === null || ls === undefined || ls.length === 0) {
return ls as T[];
}

let newLs: T[] = ls;
let newLs: (T | null)[] = ls;
let nullEncountered = false;

for (let i = 0; i < ls.length; i++) {
Expand All @@ -23,6 +23,6 @@ export class ListUtils {
newLs = newLs.filter(item => item !== null && item !== undefined);
}

return newLs;
return newLs as T[];
}
}
51 changes: 48 additions & 3 deletions openrewrite/src/java/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Space,
} from "./support_types";
import {J} from "./tree";
import {Cursor, ListUtils} from "../core";

export function getJavaType<T extends J>(expr: T): JavaType | null {
if (expr instanceof J.AnnotatedType) {
Expand Down Expand Up @@ -43,13 +44,57 @@ export function visitSpace<P>(v: JavaVisitor<P>, space: Space | null, loc: Space
}

export function visitContainer<P, T>(v: JavaVisitor<P>, container: JContainer<T> | null, loc: JContainer.Location, p: P) {
return container!;
if (container == null)
return null;

v.cursor = new Cursor(v.cursor, container);

let before = v.visitSpace(container.before, JContainer.Location.beforeLocation(loc), p);
let js = ListUtils.map(container.padding.elements, t => v.visitRightPadded(t, JContainer.Location.elementLocation(loc), p));

v.cursor = v.cursor.parent!;

return js == container.padding.elements && before == container.before ?
container :
JContainer.build(before!, js, container.markers);
}

export function visitLeftPadded<P, T>(v: JavaVisitor<P>, left: JLeftPadded<T> | null, loc: JLeftPadded.Location, p: P) {
return left!;
if (left == null)
return null;

v.cursor = new Cursor(v.cursor, left);

let before = v.visitSpace(left.before, JLeftPadded.Location.beforeLocation(loc), p)!;
let t = left.element;
if (left.element instanceof J)
t = v.visit(t as J, p) as T;
v.cursor = v.cursor.parent!;

// If nothing changed leave AST node the same
if (left.element === t && before == left.before) {
return left;
}

return t == null ? null : new JLeftPadded<T>(before, t, left.markers);
}

export function visitRightPadded<P, T>(v: JavaVisitor<P>, right: JRightPadded<T> | null, loc: JRightPadded.Location, p: P) {
return right!;
if (right == null)
return null;

let t = right.element;
if (t instanceof J) {
v.cursor = new Cursor(v.cursor, right);
t = v.visit(t as J, p) as T;
v.cursor = v.cursor.parent!;
}

if (t == null)
return null;

right = right.withElement(t);
right = right.withAfter(v.visitSpace(right.after, JRightPadded.Location.afterLocation(loc), p)!);
right = right.withMarkers(v.visitMarkers(right.markers, p));
return right;
}
29 changes: 28 additions & 1 deletion openrewrite/src/java/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface JavaSourceFile extends SourceFile {
}

export interface Expression extends Tree {
get type(): JavaType | null;

withType(type: JavaType | null): TypedTree;
}

export interface Statement extends Tree {
Expand Down Expand Up @@ -193,7 +196,7 @@ export class JContainer<T> {
this._markers = markers;
}

static build<J2 extends J>(before: Space, elements: JRightPadded<J2>[], markers: Markers): JContainer<J2> {
static build<J2>(before: Space, elements: JRightPadded<J2>[], markers: Markers): JContainer<J2> {
return new JContainer(before, elements, markers);
}

Expand Down Expand Up @@ -455,6 +458,13 @@ export namespace JRightPadded {
TYPE_PARAMETER,
WHILE_BODY,
}

export namespace Location {
export function afterLocation(location: Location): Space.Location {
// FIXME
return null!;
}
}
}

export namespace JLeftPadded {
Expand All @@ -479,6 +489,13 @@ export namespace JLeftPadded {
WHILE_CONDITION,
WILDCARD_BOUND,
}

export namespace Location {
export function beforeLocation(location: Location): Space.Location {
// FIXME
return null!;
}
}
}

export namespace JContainer {
Expand All @@ -498,5 +515,15 @@ export namespace JContainer {
TRY_RESOURCES,
TYPE_BOUNDS,
TYPE_PARAMETERS,
}
export namespace Location {
export function beforeLocation(location: Location): Space.Location {
// FIXME
return null!;
}
export function elementLocation(location: Location): JRightPadded.Location {
// FIXME
return null!;
}
}
}
24 changes: 24 additions & 0 deletions openrewrite/src/java/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ export namespace J {
return v.visitAnnotation(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): J.Annotation {
return extensions.withJavaType(this, type);
}

get padding() {
const t = this;
return new class {
Expand Down Expand Up @@ -4497,6 +4505,14 @@ export namespace J {
return v.visitParentheses(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): J.Parentheses<J2> {
return extensions.withJavaType(this, type);
}

get padding() {
const t = this;
return new class {
Expand Down Expand Up @@ -4564,6 +4580,14 @@ export namespace J {
return v.visitControlParentheses(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): J.ControlParentheses<J2> {
return extensions.withJavaType(this, type);
}

get padding() {
const t = this;
return new class {
Expand Down
5 changes: 5 additions & 0 deletions openrewrite/src/java/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export namespace JavaType {
}

export class Variable implements JavaType {
type: JavaType = null!;

withType(type: JavaType): Variable {
return null!;
}
}

export class Method implements JavaType {
Expand Down
34 changes: 32 additions & 2 deletions openrewrite/src/javascript/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@ import {J, JavaType, JavaVisitor, JContainer, JLeftPadded, JRightPadded, Space}
import * as java_extensions from "../java/extensions";
import {JavaScriptVisitor} from "./visitor";
import {JsContainer, JsLeftPadded, JsRightPadded, JsSpace} from "./support_types";
import {JS} from "./tree";

export function getJavaType<T extends J>(expr: T): JavaType | null {
// FIXME implement for JS types
if (expr instanceof JS.Alias) {
return expr.propertyName.type;
} else if (expr instanceof JS.ExpressionStatement) {
return expr.expression.type;
} else if (expr instanceof JS.ObjectBindingDeclarations) {
return expr.typeExpression != null ? expr.typeExpression.type : null;
} else if (expr instanceof JS.ObjectBindingDeclarations.Binding) {
return expr.variableType != null ? expr.variableType.type : null;
} else if (expr instanceof JS.StatementExpression) {
return null;
} else if (expr instanceof JS.TypeDeclaration) {
return expr.javaType;
} else if (expr instanceof JS.TypeOperator) {
return expr.expression.type;
}
return java_extensions.getJavaType(expr);
}

export function withJavaType<T>(expr: T, type: JavaType): T {
// FIXME implement for JS types
if (expr instanceof JS.Alias) {
return expr.withPropertyName(expr.propertyName.withType(type)) as T;
} else if (expr instanceof JS.ExpressionStatement) {
return expr.withExpression(expr.expression.withType(type)) as T;
} else if (expr instanceof JS.ObjectBindingDeclarations) {
return (expr.typeExpression != null ? expr.withTypeExpression(expr.typeExpression.withType(type)) : null) as T;
} else if (expr instanceof JS.ObjectBindingDeclarations.Binding) {
return (expr.variableType != null ? expr.withVariableType(expr.variableType.withType(type)) : null) as T;
} else if (expr instanceof JS.StatementExpression) {
return expr as T;
} else if (expr instanceof JS.TypeDeclaration) {
// FIXME we should rename the `javaType` field to `type`
throw new Error("Cannot set `javaType` field");
} else if (expr instanceof JS.TypeOperator) {
return expr.withExpression(expr.expression.withType(type)) as T;
}
return java_extensions.withJavaType(expr, type);
}

Expand Down
28 changes: 26 additions & 2 deletions openrewrite/src/javascript/tree.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// noinspection JSUnusedGlobalSymbols

import * as extensions from "./extensions";
import {} from "./support_types";
import {JsLeftPadded, JsRightPadded, JsContainer, JsSpace} from "./support_types";
import {JavaScriptVisitor} from "./visitor";
import {UUID, Checksum, FileAttributes, SourceFile, Tree, TreeVisitor, Markers, Cursor, PrintOutputCapture, PrinterFactory} from "../core";
import {Expression, J, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypedTree, TypeTree} from "../java";
Expand Down Expand Up @@ -248,6 +248,14 @@ export namespace JS {
return v.visitAlias(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): JS.Alias {
return extensions.withJavaType(this, type);
}

get padding() {
const t = this;
return new class {
Expand Down Expand Up @@ -665,7 +673,7 @@ export namespace JS {
}

public withMarkers(markers: Markers): ExpressionStatement {
return this.withExpression(this._expression.withMarkers(markers));
return this.withExpression(this._expression.withMarkers(markers) as Expression);
}

private readonly _expression: Expression;
Expand All @@ -682,6 +690,14 @@ export namespace JS {
return v.visitExpressionStatement(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): JS.ExpressionStatement {
return extensions.withJavaType(this, type);
}

}

export class FunctionType extends JS implements Expression, TypeTree {
Expand Down Expand Up @@ -1437,6 +1453,14 @@ export namespace JS {
return v.visitStatementExpression(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): JS.StatementExpression {
return extensions.withJavaType(this, type);
}

}

export class TemplateExpression extends JS implements Statement, Expression {
Expand Down

0 comments on commit c2295c0

Please sign in to comment.