Skip to content

Commit

Permalink
More work on JS
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 12, 2024
1 parent 3be93fa commit 83f40e7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 15 deletions.
36 changes: 35 additions & 1 deletion openrewrite/src/javascript/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {J, JavaType} from "../java";
import {J, JavaType, JavaVisitor, JContainer, JLeftPadded, JRightPadded, Space} from "../java";
import * as java_extensions from "../java/extensions";
import {JavaScriptVisitor} from "./visitor";
import {JsContainer, JsLeftPadded, JsRightPadded, JsSpace} from "./support_types";

export function getJavaType<T extends J>(expr: T): JavaType | null {
// FIXME implement for JS types
Expand All @@ -10,3 +12,35 @@ export function withJavaType<T>(expr: T, type: JavaType): T {
// FIXME implement for JS types
return java_extensions.withJavaType(expr, type);
}

export function visitSpace<P>(v: JavaScriptVisitor<P>, space: Space | null, loc: Space.Location | JsSpace.Location, p: P): Space {
// FIXME this won't quite work
if (Object.values(Space.Location).includes(loc as any)) {
return java_extensions.visitSpace(v, space, loc as Space.Location, p);
}
return space!;
}

export function visitContainer<P, T>(v: JavaScriptVisitor<P>, container: JContainer<T> | null, loc: JContainer.Location | JsContainer.Location, p: P): JContainer<T> {
// FIXME this won't quite work
if (Object.values(JContainer.Location).includes(loc as any)) {
return java_extensions.visitContainer(v, container, loc as JContainer.Location, p);
}
return container!;
}

export function visitLeftPadded<P, T>(v: JavaScriptVisitor<P>, left: JLeftPadded<T> | null, loc: JLeftPadded.Location | JsLeftPadded.Location, p: P): JLeftPadded<T> {
// FIXME this won't quite work
if (Object.values(JLeftPadded.Location).includes(loc as any)) {
return java_extensions.visitLeftPadded(v, left, loc as JLeftPadded.Location, p);
}
return left!;
}

export function visitRightPadded<P, T>(v: JavaScriptVisitor<P>, right: JRightPadded<T> | null, loc: JRightPadded.Location | JsRightPadded.Location, p: P): JRightPadded<T> {
// FIXME this won't quite work
if (Object.values(JRightPadded.Location).includes(loc as any)) {
return java_extensions.visitRightPadded(v, right, loc as JRightPadded.Location, p);
}
return right!;
}
46 changes: 46 additions & 0 deletions openrewrite/src/javascript/support_types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
export namespace JsSpace {
export enum Location {
ALIAS_PREFIX,
ARROW_FUNCTION_PREFIX,
ARROW_FUNCTION_ARROW,
DEFAULT_TYPE_PREFIX,
DEFAULT_TYPE_BEFORE_EQUALS,
DELETE_PREFIX,
EXPORT_PREFIX,
EXPORT_FROM,
FUNCTION_TYPE_PREFIX,
FUNCTION_TYPE_ARROW,
JS_IMPORT_PREFIX,
JS_IMPORT_FROM,
JS_BINARY_PREFIX,
JS_OPERATOR_PREFIX,
OBJECT_BINDING_DECLARATIONS_PREFIX,
OBJECT_BINDING_DECLARATIONS_BINDING_PREFIX,
OBJECT_BINDING_DECLARATIONS_BINDING_AFTER_VARARG,
TEMPLATE_EXPRESSION_PREFIX,
TEMPLATE_EXPRESSION_VALUE_PREFIX,
TEMPLATE_EXPRESSION_VALUE_AFTER,
TUPLE_PREFIX,
TYPE_DECLARATION_PREFIX,
TYPE_OPERATOR_PREFIX,
UNARY_PREFIX,
UNION_PREFIX,
}
}
export namespace JsLeftPadded {
export enum Location {
EXPORT_INITIALIZER,
JS_IMPORT_INITIALIZER,
JS_BINARY_OPERATOR,
JS_OPERATOR_OPERATOR,
OBJECT_BINDING_DECLARATIONS_INITIALIZER,
OBJECT_BINDING_DECLARATIONS_BINDING_DIMENSIONS_AFTER_NAME,
OBJECT_BINDING_DECLARATIONS_BINDING_INITIALIZER,
TYPE_DECLARATION_INITIALIZER,
TYPE_OPERATOR_EXPRESSION,
UNARY_OPERATOR,
}
}
export namespace JsRightPadded {
export enum Location {
ALIAS_PROPERTY_NAME,
JS_IMPORT_NAME,
OBJECT_BINDING_DECLARATIONS_BINDING_PROPERTY_NAME,
TEMPLATE_EXPRESSION_TAG,
UNION_TYPES,
COMPILATION_UNIT_STATEMENTS,
}
}
export namespace JsContainer {
export enum Location {
EXPORT_EXPORTS,
FUNCTION_TYPE_PARAMETERS,
JS_IMPORT_IMPORTS,
OBJECT_BINDING_DECLARATIONS_BINDINGS,
TUPLE_ELEMENTS,
}
}
4 changes: 2 additions & 2 deletions openrewrite/src/javascript/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export namespace JS {
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
return v.visitCompilationUnit(this, p);
return v.visitJsCompilationUnit(this, p);
}

get padding() {
Expand Down Expand Up @@ -1990,7 +1990,7 @@ export namespace JS {
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
return v.visitUnary(this, p);
return v.visitJsUnary(this, p);
}

get padding() {
Expand Down
28 changes: 16 additions & 12 deletions openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
return sourceFile instanceof JS;
}

public visitCompilationUnit(compilationUnit: JS.CompilationUnit, p: P): J | null {
public visitJsCompilationUnit(compilationUnit: JS.JsCompilationUnit, p: P): J | null {
compilationUnit = compilationUnit.withPrefix(this.visitSpace(compilationUnit.prefix, Space.Location.COMPILATION_UNIT_PREFIX, p)!);
compilationUnit = compilationUnit.withMarkers(this.visitMarkers(compilationUnit.markers, p));
compilationUnit = compilationUnit.padding.withImports(ListUtils.map(compilationUnit.padding.imports, el => this.visitRightPadded(el, JsRightPadded.Location.COMPILATION_UNIT_IMPORTS, p)));
compilationUnit = compilationUnit.padding.withImports(ListUtils.map(compilationUnit.padding.imports, el => this.visitRightPadded(el, JRightPadded.Location.IMPORT, p)));
compilationUnit = compilationUnit.padding.withStatements(ListUtils.map(compilationUnit.padding.statements, el => this.visitRightPadded(el, JsRightPadded.Location.COMPILATION_UNIT_STATEMENTS, p)));
compilationUnit = compilationUnit.withEof(this.visitSpace(compilationUnit.eof, JsSpace.Location.COMPILATION_UNIT_EOF, p)!);
compilationUnit = compilationUnit.withEof(this.visitSpace(compilationUnit.eof, Space.Location.COMPILATION_UNIT_EOF, p)!);
return compilationUnit;
}

Expand Down Expand Up @@ -283,20 +283,20 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
return typeOperator;
}

public visitUnary(unary: JS.Unary, p: P): J | null {
public visitJsUnary(unary: JS.JsUnary, p: P): J | null {
unary = unary.withPrefix(this.visitSpace(unary.prefix, JsSpace.Location.UNARY_PREFIX, p)!);
let tempStatement = this.visitStatement(unary, p) as Statement & J;
if (!(tempStatement instanceof JS.Unary))
{
return tempStatement;
}
unary = tempStatement as JS.Unary;
unary = tempStatement as unknown as JS.JsUnary;
let tempExpression = this.visitExpression(unary, p) as Expression & J;
if (!(tempExpression instanceof JS.Unary))
{
return tempExpression;
}
unary = tempExpression as JS.Unary;
unary = tempExpression as unknown as JS.JsUnary;
unary = unary.withMarkers(this.visitMarkers(unary.markers, p));
unary = unary.padding.withOperator(this.visitLeftPadded(unary.padding.operator, JsLeftPadded.Location.UNARY_OPERATOR, p)!);
unary = unary.withExpression(this.visitAndCast(unary.expression, p)!);
Expand All @@ -316,16 +316,20 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
return union;
}

public visitJsLeftPadded(jsLeftPadded: JsLeftPadded, p: P): JsLeftPadded | null {
return jsLeftPadded;
public visitContainer<T>(container: JContainer<T> | null, loc: JContainer.Location | JsContainer.Location, p: P) {
return extensions.visitContainer(this, container, loc, p);
}

public visitJsRightPadded(jsRightPadded: JsRightPadded, p: P): JsRightPadded | null {
return jsRightPadded;
public visitLeftPadded<T>(left: JLeftPadded<T> | null, loc: JLeftPadded.Location | JsLeftPadded.Location, p: P) {
return extensions.visitLeftPadded(this, left, loc, p);
}

public visitJsContainer(jsContainer: JsContainer, p: P): JsContainer | null {
return jsContainer;
public visitRightPadded<T>(right: JRightPadded<T> | null, loc: JRightPadded.Location | JsRightPadded.Location, p: P) {
return extensions.visitRightPadded(this, right, loc, p);
}

public visitSpace(space: Space | null, loc: Space.Location | JsSpace.Location, p: P): Space {
return extensions.visitSpace(this, space, loc, p);
}

}

0 comments on commit 83f40e7

Please sign in to comment.