Skip to content

Commit

Permalink
Implemented ConstructorType and TypeQuery
Browse files Browse the repository at this point in the history
- added custom class JS.TypeQuery
- added for TypeParameter bounds and default type support
  • Loading branch information
Andrii Rodionov committed Nov 22, 2024
1 parent 5e6df87 commit 46969d3
Show file tree
Hide file tree
Showing 24 changed files with 576 additions and 35 deletions.
52 changes: 43 additions & 9 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ export class JavaScriptParserVisitor {
return this.mapIdentifier(node, 'any');
}

visitObjectKeyword(node: ts.Node) {
return this.mapIdentifier(node, 'object');
}

visitUnknownKeyword(node: ts.Node) {
return this.mapIdentifier(node, 'unknown');
}
Expand Down Expand Up @@ -628,18 +632,20 @@ export class JavaScriptParserVisitor {
}

visitTypeParameter(node: ts.TypeParameterDeclaration) {
if (node.constraint || (node.modifiers && node.modifiers.length) || node.default) {
return this.visitUnknown(node);
}

return new J.TypeParameter(
randomId(),
this.prefix(node),
Markers.EMPTY,
[],
[],
this.visit(node.name),
null
(node.constraint || node.default) ?
new JContainer(
this.prefix(this.findChildNode(node, ts.SyntaxKind.ExtendsKeyword) ?? this.findChildNode(node, ts.SyntaxKind.EqualsToken)!),
[node.constraint ? this.rightPadded(this.visit(node.constraint), this.suffix(node.constraint)) : this.rightPadded(this.newJEmpty(), Space.EMPTY),
node.default ? this.rightPadded(this.visit(node.default), this.suffix(node.default)) : this.rightPadded(this.newJEmpty(), Space.EMPTY)],
Markers.EMPTY)
: null
);
}

Expand Down Expand Up @@ -668,6 +674,8 @@ export class JavaScriptParserVisitor {
);
}

// TODO should process node.dotDotDotToken

const nameExpression = this.visit(node.name)

if (nameExpression instanceof J.Identifier) {
Expand Down Expand Up @@ -1164,21 +1172,39 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
new JContainer(
this.prefix(node),
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p))),
Markers.EMPTY),
this.prefix(node.getChildren().find(v => v.kind === ts.SyntaxKind.EqualsGreaterThanToken)!),
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
this.convert(node.type),
null);
}

visitConstructorType(node: ts.ConstructorTypeNode) {
return this.visitUnknown(node);
return new JS.FunctionType(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(true, this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!)),
new JContainer(
this.prefix(node),
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p))),
Markers.EMPTY),
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
this.convert(node.type),
null);
}

visitTypeQuery(node: ts.TypeQueryNode) {
return this.visitUnknown(node);
return new JS.TypeQuery(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.exprName),
this.mapType(node)
)
}

visitTypeLiteral(node: ts.TypeLiteralNode) {
Expand All @@ -1203,7 +1229,15 @@ export class JavaScriptParserVisitor {
}

visitArrayType(node: ts.ArrayTypeNode) {
return this.visitUnknown(node);
return new J.ArrayType(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.elementType),
null,
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBracketToken)!), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseBracketToken)!) ),
this.mapType(node)!
)
}

visitTupleType(node: ts.TupleTypeNode) {
Expand Down
23 changes: 22 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} 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 @@ -116,6 +116,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
functionType = functionType.withId(ctx.receiveValue(functionType.id, ValueType.UUID)!);
functionType = functionType.withPrefix(ctx.receiveNode(functionType.prefix, receiveSpace)!);
functionType = functionType.withMarkers(ctx.receiveNode(functionType.markers, ctx.receiveMarkers)!);
functionType = functionType.padding.withConstructorType(ctx.receiveNode(functionType.padding.constructorType, rightPaddedValueReceiver(ValueType.Primitive))!);
functionType = functionType.padding.withParameters(ctx.receiveNode(functionType.padding.parameters, receiveContainer)!);
functionType = functionType.withArrow(ctx.receiveNode(functionType.arrow, receiveSpace)!);
functionType = functionType.withReturnType(ctx.receiveNode(functionType.returnType, ctx.receiveTree)!);
Expand Down Expand Up @@ -258,6 +259,15 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return typeOf;
}

public visitTypeQuery(typeQuery: TypeQuery, ctx: ReceiverContext): J {
typeQuery = typeQuery.withId(ctx.receiveValue(typeQuery.id, ValueType.UUID)!);
typeQuery = typeQuery.withPrefix(ctx.receiveNode(typeQuery.prefix, receiveSpace)!);
typeQuery = typeQuery.withMarkers(ctx.receiveNode(typeQuery.markers, ctx.receiveMarkers)!);
typeQuery = typeQuery.withTypeExpression(ctx.receiveNode(typeQuery.typeExpression, ctx.receiveTree)!);
typeQuery = typeQuery.withType(ctx.receiveValue(typeQuery.type, ValueType.Object));
return typeQuery;
}

public visitTypeOperator(typeOperator: TypeOperator, ctx: ReceiverContext): J {
typeOperator = typeOperator.withId(ctx.receiveValue(typeOperator.id, ValueType.UUID)!);
typeOperator = typeOperator.withPrefix(ctx.receiveNode(typeOperator.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1214,6 +1224,7 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<boolean>>(null, rightPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<JContainer<Statement>>(null, receiveContainer)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode<Expression>(null, ctx.receiveTree)!,
Expand Down Expand Up @@ -1369,6 +1380,16 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$TypeOperator") {
return new TypeOperator(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
12 changes: 11 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -111,6 +111,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendValue(functionType, v => v.id, ValueType.UUID);
ctx.sendNode(functionType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(functionType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(functionType, v => v.padding.constructorType, Visitor.sendRightPadded(ValueType.Primitive));
ctx.sendNode(functionType, v => v.padding.parameters, Visitor.sendContainer(ValueType.Tree));
ctx.sendNode(functionType, v => v.arrow, Visitor.sendSpace);
ctx.sendNode(functionType, v => v.returnType, ctx.sendTree);
Expand Down Expand Up @@ -253,6 +254,15 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return typeOf;
}

public visitTypeQuery(typeQuery: TypeQuery, ctx: SenderContext): J {
ctx.sendValue(typeQuery, v => v.id, ValueType.UUID);
ctx.sendNode(typeQuery, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(typeQuery, v => v.markers, ctx.sendMarkers);
ctx.sendNode(typeQuery, v => v.typeExpression, ctx.sendTree);
ctx.sendTypedValue(typeQuery, v => v.type, ValueType.Object);
return typeQuery;
}

public visitTypeOperator(typeOperator: TypeOperator, ctx: SenderContext): J {
ctx.sendValue(typeOperator, v => v.id, ValueType.UUID);
ctx.sendNode(typeOperator, v => v.prefix, Visitor.sendSpace);
Expand Down
3 changes: 0 additions & 3 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ export namespace JsRightPadded {
JSMETHOD_INVOCATION_SELECT,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
JSFOR_OF_LOOP_AWAIT,
JSFOR_OF_LOOP_INITIALIZER,
JSFOR_OF_LOOP_ITERABLE,
JSFOR_IN_OF_LOOP_CONTROL_VARIABLE,
JSFOR_IN_OF_LOOP_CONTROL_ITERABLE,
JSFOR_OF_LOOP_BODY,
Expand Down
Loading

0 comments on commit 46969d3

Please sign in to comment.