Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ParenthesizedTypeTree usage and replace it with TypeTreeExpression where needed #168

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
};
};
34 changes: 13 additions & 21 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 expression = this.visit(heritageClause.types[0]);
return this.leftPadded(this.prefix(heritageClause.getFirstToken()!), new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, expression));
}
}
return null;
Expand Down Expand Up @@ -1425,15 +1426,12 @@ export class JavaScriptParserVisitor {
this.prefix(node),
Markers.EMPTY,
this.convert(node.objectType),
this.rightPadded(
new JS.IndexedAccessType.IndexType(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBracketToken)!),
Markers.EMPTY,
this.rightPadded(this.convert(node.indexType), this.suffix(node.indexType)),
this.mapType(node.indexType)
),
this.suffix(this.findChildNode(node, ts.SyntaxKind.CloseBracketToken)!)
new JS.IndexedAccessType.IndexType(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBracketToken)!),
Markers.EMPTY,
this.rightPadded(this.convert(node.indexType), this.suffix(node.indexType)),
this.mapType(node.indexType)
),
this.mapType(node)
);
Expand Down Expand Up @@ -1870,18 +1868,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
23 changes: 20 additions & 3 deletions 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, MappedType, 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, MappedType, 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 @@ -610,7 +610,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
indexedAccessType = indexedAccessType.withPrefix(ctx.receiveNode(indexedAccessType.prefix, receiveSpace)!);
indexedAccessType = indexedAccessType.withMarkers(ctx.receiveNode(indexedAccessType.markers, ctx.receiveMarkers)!);
indexedAccessType = indexedAccessType.withObjectType(ctx.receiveNode(indexedAccessType.objectType, ctx.receiveTree)!);
indexedAccessType = indexedAccessType.padding.withIndexType(ctx.receiveNode(indexedAccessType.padding.indexType, receiveRightPaddedTree)!);
indexedAccessType = indexedAccessType.withIndexType(ctx.receiveNode(indexedAccessType.indexType, ctx.receiveTree)!);
indexedAccessType = indexedAccessType.withType(ctx.receiveValue(indexedAccessType.type, ValueType.Object));
return indexedAccessType;
}
Expand All @@ -635,6 +635,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 @@ -1945,7 +1953,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree)!,
ctx.receiveNode<JRightPadded<TypeTree>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree)!,
ctx.receiveValue(null, ValueType.Object)
);
}
Expand All @@ -1972,6 +1980,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
12 changes: 10 additions & 2 deletions 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, MappedType, 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, MappedType, 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 @@ -605,7 +605,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendNode(indexedAccessType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(indexedAccessType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(indexedAccessType, v => v.objectType, ctx.sendTree);
ctx.sendNode(indexedAccessType, v => v.padding.indexType, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(indexedAccessType, v => v.indexType, ctx.sendTree);
ctx.sendTypedValue(indexedAccessType, v => v.type, ValueType.Object);
return indexedAccessType;
}
Expand All @@ -630,6 +630,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 @@ -259,6 +259,7 @@ export namespace JsSpace {
MAPPED_TYPE_PREFIX,
MAPPED_TYPE_KEYS_REMAPPING_PREFIX,
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX,
TYPE_TREE_EXPRESSION_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down
84 changes: 68 additions & 16 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5331,7 +5331,7 @@ export class ExportSpecifier extends JSMixin(Object) implements Expression, Type

@LstType("org.openrewrite.javascript.tree.JS$IndexedAccessType")
export class IndexedAccessType extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, objectType: TypeTree, indexType: JRightPadded<TypeTree>, _type: JavaType | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, objectType: TypeTree, indexType: TypeTree, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -5381,14 +5381,14 @@ export class IndexedAccessType extends JSMixin(Object) implements Expression, Ty
return objectType === this._objectType ? this : new IndexedAccessType(this._id, this._prefix, this._markers, objectType, this._indexType, this._type);
}

private readonly _indexType: JRightPadded<TypeTree>;
private readonly _indexType: TypeTree;

public get indexType(): TypeTree {
return this._indexType.element;
return this._indexType;
}

public withIndexType(indexType: TypeTree): IndexedAccessType {
return this.padding.withIndexType(this._indexType.withElement(indexType));
return indexType === this._indexType ? this : new IndexedAccessType(this._id, this._prefix, this._markers, this._objectType, indexType, this._type);
}

private readonly _type: JavaType | null;
Expand All @@ -5405,18 +5405,6 @@ export class IndexedAccessType extends JSMixin(Object) implements Expression, Ty
return v.visitIndexedAccessType(this, p);
}

get padding() {
const t = this;
return new class {
public get indexType(): JRightPadded<TypeTree> {
return t._indexType;
}
public withIndexType(indexType: JRightPadded<TypeTree>): IndexedAccessType {
return t._indexType === indexType ? t : new IndexedAccessType(t._id, t._prefix, t._markers, t._objectType, indexType, t._type);
}
}
}

}

export namespace IndexedAccessType {
Expand Down Expand Up @@ -5611,3 +5599,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);
}

}
Loading
Loading