Skip to content

Commit

Permalink
fix ParenthesizedTypeTree usage and replace it with TypeTreeExpressio…
Browse files Browse the repository at this point in the history
…n where needed
  • Loading branch information
OlegDokuka committed Dec 10, 2024
1 parent 80f9ca6 commit f5f51b3
Show file tree
Hide file tree
Showing 18 changed files with 1,786 additions and 723 deletions.
2 changes: 1 addition & 1 deletion openrewrite/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ module.exports = {
},
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts'],
};
};
19 changes: 7 additions & 12 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import {binarySearch, compareTextSpans, getNextSibling, getPreviousSibling, TextSpan} from "./parserUtils";
import {JavaScriptTypeMapping} from "./typeMapping";
import path from "node:path";
import {ExpressionStatement} from ".";
import {ExpressionStatement, TypeTreeExpression} from ".";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -440,7 +440,8 @@ export class JavaScriptParserVisitor {
}
for (let heritageClause of node.heritageClauses) {
if (heritageClause.token == ts.SyntaxKind.ExtendsKeyword) {
return this.leftPadded(this.prefix(heritageClause.getFirstToken()!), this.visit(heritageClause.types[0]));
const typeTreeExpression = this.visit(heritageClause.types[0]);
return this.leftPadded(this.prefix(heritageClause.getFirstToken()!), new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, typeTreeExpression));
}
}
return null;
Expand Down Expand Up @@ -1810,18 +1811,12 @@ export class JavaScriptParserVisitor {
}

visitParenthesizedExpression(node: ts.ParenthesizedExpression) {
return new J.ParenthesizedTypeTree(
return new J.Parentheses(
randomId(),
Space.EMPTY,
this.prefix(node),
Markers.EMPTY,
[],
new J.Parentheses(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(this.convert(node.expression), this.prefix(node.getLastToken()!))
)
);
this.rightPadded(this.convert(node.expression), this.prefix(node.getLastToken()!))
)
}

visitFunctionExpression(node: ts.FunctionExpression) {
Expand Down
19 changes: 18 additions & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -603,6 +603,14 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return jsAssignmentOperation;
}

public visitTypeTreeExpression(typeTreeExpression: TypeTreeExpression, ctx: ReceiverContext): J {
typeTreeExpression = typeTreeExpression.withId(ctx.receiveValue(typeTreeExpression.id, ValueType.UUID)!);
typeTreeExpression = typeTreeExpression.withPrefix(ctx.receiveNode(typeTreeExpression.prefix, receiveSpace)!);
typeTreeExpression = typeTreeExpression.withMarkers(ctx.receiveNode(typeTreeExpression.markers, ctx.receiveMarkers)!);
typeTreeExpression = typeTreeExpression.withExpression(ctx.receiveNode(typeTreeExpression.expression, ctx.receiveTree)!);
return typeTreeExpression;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: ReceiverContext): J {
annotatedType = annotatedType.withId(ctx.receiveValue(annotatedType.id, ValueType.UUID)!);
annotatedType = annotatedType.withPrefix(ctx.receiveNode(annotatedType.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1905,6 +1913,15 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$TypeTreeExpression") {
return new TypeTreeExpression(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<Expression>(null, ctx.receiveTree)!
);
}

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
10 changes: 9 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Cursor, ListUtils, Tree} from '../../core';
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -598,6 +598,14 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return jsAssignmentOperation;
}

public visitTypeTreeExpression(typeTreeExpression: TypeTreeExpression, ctx: SenderContext): J {
ctx.sendValue(typeTreeExpression, v => v.id, ValueType.UUID);
ctx.sendNode(typeTreeExpression, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(typeTreeExpression, v => v.markers, ctx.sendMarkers);
ctx.sendNode(typeTreeExpression, v => v.expression, ctx.sendTree);
return typeTreeExpression;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: SenderContext): J {
ctx.sendValue(annotatedType, v => v.id, ValueType.UUID);
ctx.sendNode(annotatedType, v => v.prefix, Visitor.sendSpace);
Expand Down
1 change: 1 addition & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export namespace JsSpace {
INDEXED_ACCESS_TYPE_INDEX_TYPE_SUFFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT_SUFFIX,
JS_ASSIGNMENT_OPERATION_PREFIX,
TYPE_TREE_EXPRESSION_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down
64 changes: 64 additions & 0 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5286,3 +5286,67 @@ export namespace JsAssignmentOperation {
}

}

@LstType("org.openrewrite.javascript.tree.JS$TypeTreeExpression")
export class TypeTreeExpression extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, expression: Expression) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._expression = expression;
}

private readonly _id: UUID;

public get id(): UUID {
return this._id;
}

public withId(id: UUID): TypeTreeExpression {
return id === this._id ? this : new TypeTreeExpression(id, this._prefix, this._markers, this._expression);
}

private readonly _prefix: Space;

public get prefix(): Space {
return this._prefix;
}

public withPrefix(prefix: Space): TypeTreeExpression {
return prefix === this._prefix ? this : new TypeTreeExpression(this._id, prefix, this._markers, this._expression);
}

private readonly _markers: Markers;

public get markers(): Markers {
return this._markers;
}

public withMarkers(markers: Markers): TypeTreeExpression {
return markers === this._markers ? this : new TypeTreeExpression(this._id, this._prefix, markers, this._expression);
}

private readonly _expression: Expression;

public get expression(): Expression {
return this._expression;
}

public withExpression(expression: Expression): TypeTreeExpression {
return expression === this._expression ? this : new TypeTreeExpression(this._id, this._prefix, this._markers, expression);
}

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

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

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

}
15 changes: 14 additions & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as extensions from "./extensions";
import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core";
import {JS, isJavaScript, JsLeftPadded, JsRightPadded, JsContainer, JsSpace} from "./tree";
import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} from "./tree";
import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree";
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree";
import {JavaVisitor} from "../java";
import * as Java from "../java/tree";
Expand Down Expand Up @@ -842,6 +842,19 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
return jsAssignmentOperation;
}

public visitTypeTreeExpression(typeTreeExpression: TypeTreeExpression, p: P): J | null {
typeTreeExpression = typeTreeExpression.withPrefix(this.visitJsSpace(typeTreeExpression.prefix, JsSpace.Location.TYPE_TREE_EXPRESSION_PREFIX, p)!);
let tempExpression = this.visitExpression(typeTreeExpression, p) as Expression;
if (!(tempExpression instanceof TypeTreeExpression))
{
return tempExpression;
}
typeTreeExpression = tempExpression as TypeTreeExpression;
typeTreeExpression = typeTreeExpression.withMarkers(this.visitMarkers(typeTreeExpression.markers, p));
typeTreeExpression = typeTreeExpression.withExpression(this.visitAndCast(typeTreeExpression.expression, p)!);
return typeTreeExpression;
}

public visitJsLeftPadded<T>(left: JLeftPadded<T> | null, loc: JsLeftPadded.Location, p: P): JLeftPadded<T> {
return extensions.visitJsLeftPadded(this, left, loc, p);
}
Expand Down
Loading

0 comments on commit f5f51b3

Please sign in to comment.