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

Implemented template expression and conditional type #160

Merged
merged 4 commits into from
Nov 27, 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
94 changes: 74 additions & 20 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ export class JavaScriptParserVisitor {
return this.mapLiteral(node, null);
}

private mapLiteral(node: ts.LiteralExpression | ts.TrueLiteral | ts.FalseLiteral | ts.NullLiteral | ts.Identifier, value: any): J.Literal {
private mapLiteral(node: ts.LiteralExpression | ts.TrueLiteral | ts.FalseLiteral | ts.NullLiteral | ts.Identifier
| ts.TemplateHead | ts.TemplateMiddle | ts.TemplateTail, value: any): J.Literal {
return new J.Literal(
randomId(),
this.prefix(node),
Expand Down Expand Up @@ -567,15 +568,15 @@ export class JavaScriptParserVisitor {
}

visitTemplateHead(node: ts.TemplateHead) {
return this.visitUnknown(node);
return this.mapLiteral(node, node.text);
}

visitTemplateMiddle(node: ts.TemplateMiddle) {
return this.visitUnknown(node);
return this.mapLiteral(node, node.text);
}

visitTemplateTail(node: ts.TemplateTail) {
return this.visitUnknown(node);
return this.mapLiteral(node, node.text);
}

visitIdentifier(node: ts.Identifier) {
Expand Down Expand Up @@ -1328,7 +1329,28 @@ export class JavaScriptParserVisitor {
}

visitConditionalType(node: ts.ConditionalTypeNode) {
return this.visitUnknown(node);
return new JS.ConditionalType(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.checkType),
new JContainer(
this.prefix(this.findChildNode(node, ts.SyntaxKind.ExtendsKeyword)!),
[this.rightPadded(
new J.Ternary(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.convert(node.extendsType),
this.leftPadded(this.suffix(node.extendsType), this.convert(node.trueType)),
this.leftPadded(this.suffix(node.trueType), this.convert(node.falseType)),
this.mapType(node)),
Space.EMPTY
)],
Markers.EMPTY
),
this.mapType(node)
);
}

visitInferType(node: ts.InferTypeNode) {
Expand Down Expand Up @@ -1379,11 +1401,24 @@ export class JavaScriptParserVisitor {
}

visitTemplateLiteralType(node: ts.TemplateLiteralTypeNode) {
return this.visitUnknown(node);
return new JS.TemplateExpression(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.head),
node.templateSpans.map(s => this.rightPadded(this.visit(s), this.suffix(s))),
this.mapType(node)
)
}

visitTemplateLiteralTypeSpan(node: ts.TemplateLiteralTypeSpan) {
return this.visitUnknown(node);
return new JS.TemplateExpression.TemplateSpan(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.type),
this.visit(node.literal)
)
}

visitImportType(node: ts.ImportTypeNode) {
Expand Down Expand Up @@ -1591,7 +1626,15 @@ export class JavaScriptParserVisitor {
}

visitTaggedTemplateExpression(node: ts.TaggedTemplateExpression) {
return this.visitUnknown(node);
return new JS.TaggedTemplateExpression(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(this.visit(node.tag), this.suffix(node.tag)),
node.typeArguments ? this.mapTypeArguments(Space.EMPTY, node.typeArguments) : null,
this.visit(node.template),
this.mapType(node)
)
}

visitTypeAssertionExpression(node: ts.TypeAssertion) {
Expand Down Expand Up @@ -1959,7 +2002,14 @@ export class JavaScriptParserVisitor {
}

visitTemplateExpression(node: ts.TemplateExpression) {
return this.visitUnknown(node);
return new JS.TemplateExpression(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.head),
node.templateSpans.map(s => this.rightPadded(this.visit(s), this.suffix(s))),
this.mapType(node)
)
}

visitYieldExpression(node: ts.YieldExpression) {
Expand Down Expand Up @@ -2029,16 +2079,14 @@ export class JavaScriptParserVisitor {

visitExpressionWithTypeArguments(node: ts.ExpressionWithTypeArguments) {
if (node.typeArguments) {
if (node.typeArguments) {
return new J.ParameterizedType(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.expression),
this.mapTypeArguments(this.suffix(node.expression), node.typeArguments),
this.mapType(node)
)
}
return new JS.ExpressionWithTypeArguments(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.expression),
this.mapTypeArguments(this.suffix(node.expression), node.typeArguments),
this.mapType(node)
)
}
return this.visit(node.expression);
}
Expand Down Expand Up @@ -2079,7 +2127,13 @@ export class JavaScriptParserVisitor {
}

visitTemplateSpan(node: ts.TemplateSpan) {
return this.visitUnknown(node);
return new JS.TemplateExpression.TemplateSpan(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.expression),
this.visit(node.literal)
)
}

visitSemicolonClassElement(node: ts.SemicolonClassElement) {
Expand Down
99 changes: 80 additions & 19 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, 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, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} 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 @@ -75,6 +75,16 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return await;
}

public visitConditionalType(conditionalType: ConditionalType, ctx: ReceiverContext): J {
conditionalType = conditionalType.withId(ctx.receiveValue(conditionalType.id, ValueType.UUID)!);
conditionalType = conditionalType.withPrefix(ctx.receiveNode(conditionalType.prefix, receiveSpace)!);
conditionalType = conditionalType.withMarkers(ctx.receiveNode(conditionalType.markers, ctx.receiveMarkers)!);
conditionalType = conditionalType.withCheckType(ctx.receiveNode(conditionalType.checkType, ctx.receiveTree)!);
conditionalType = conditionalType.padding.withCondition(ctx.receiveNode(conditionalType.padding.condition, receiveContainer)!);
conditionalType = conditionalType.withType(ctx.receiveValue(conditionalType.type, ValueType.Object));
return conditionalType;
}

public visitDefaultType(defaultType: DefaultType, ctx: ReceiverContext): J {
defaultType = defaultType.withId(ctx.receiveValue(defaultType.id, ValueType.UUID)!);
defaultType = defaultType.withPrefix(ctx.receiveNode(defaultType.prefix, receiveSpace)!);
Expand Down Expand Up @@ -112,6 +122,16 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return expressionStatement;
}

public visitExpressionWithTypeArguments(expressionWithTypeArguments: ExpressionWithTypeArguments, ctx: ReceiverContext): J {
expressionWithTypeArguments = expressionWithTypeArguments.withId(ctx.receiveValue(expressionWithTypeArguments.id, ValueType.UUID)!);
expressionWithTypeArguments = expressionWithTypeArguments.withPrefix(ctx.receiveNode(expressionWithTypeArguments.prefix, receiveSpace)!);
expressionWithTypeArguments = expressionWithTypeArguments.withMarkers(ctx.receiveNode(expressionWithTypeArguments.markers, ctx.receiveMarkers)!);
expressionWithTypeArguments = expressionWithTypeArguments.withClazz(ctx.receiveNode(expressionWithTypeArguments.clazz, ctx.receiveTree)!);
expressionWithTypeArguments = expressionWithTypeArguments.padding.withTypeArguments(ctx.receiveNode(expressionWithTypeArguments.padding.typeArguments, receiveContainer));
expressionWithTypeArguments = expressionWithTypeArguments.withType(ctx.receiveValue(expressionWithTypeArguments.type, ValueType.Object));
return expressionWithTypeArguments;
}

public visitFunctionType(functionType: FunctionType, ctx: ReceiverContext): J {
functionType = functionType.withId(ctx.receiveValue(functionType.id, ValueType.UUID)!);
functionType = functionType.withPrefix(ctx.receiveNode(functionType.prefix, receiveSpace)!);
Expand Down Expand Up @@ -195,25 +215,34 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return statementExpression;
}

public visitTaggedTemplateExpression(taggedTemplateExpression: TaggedTemplateExpression, ctx: ReceiverContext): J {
taggedTemplateExpression = taggedTemplateExpression.withId(ctx.receiveValue(taggedTemplateExpression.id, ValueType.UUID)!);
taggedTemplateExpression = taggedTemplateExpression.withPrefix(ctx.receiveNode(taggedTemplateExpression.prefix, receiveSpace)!);
taggedTemplateExpression = taggedTemplateExpression.withMarkers(ctx.receiveNode(taggedTemplateExpression.markers, ctx.receiveMarkers)!);
taggedTemplateExpression = taggedTemplateExpression.padding.withTag(ctx.receiveNode(taggedTemplateExpression.padding.tag, receiveRightPaddedTree));
taggedTemplateExpression = taggedTemplateExpression.padding.withTypeArguments(ctx.receiveNode(taggedTemplateExpression.padding.typeArguments, receiveContainer));
taggedTemplateExpression = taggedTemplateExpression.withTemplateExpression(ctx.receiveNode(taggedTemplateExpression.templateExpression, ctx.receiveTree)!);
taggedTemplateExpression = taggedTemplateExpression.withType(ctx.receiveValue(taggedTemplateExpression.type, ValueType.Object));
return taggedTemplateExpression;
}

public visitTemplateExpression(templateExpression: TemplateExpression, ctx: ReceiverContext): J {
templateExpression = templateExpression.withId(ctx.receiveValue(templateExpression.id, ValueType.UUID)!);
templateExpression = templateExpression.withPrefix(ctx.receiveNode(templateExpression.prefix, receiveSpace)!);
templateExpression = templateExpression.withMarkers(ctx.receiveNode(templateExpression.markers, ctx.receiveMarkers)!);
templateExpression = templateExpression.withDelimiter(ctx.receiveValue(templateExpression.delimiter, ValueType.Primitive)!);
templateExpression = templateExpression.padding.withTag(ctx.receiveNode(templateExpression.padding.tag, receiveRightPaddedTree));
templateExpression = templateExpression.withStrings(ctx.receiveNodes(templateExpression.strings, ctx.receiveTree)!);
templateExpression = templateExpression.withHead(ctx.receiveNode(templateExpression.head, ctx.receiveTree)!);
templateExpression = templateExpression.padding.withTemplateSpans(ctx.receiveNodes(templateExpression.padding.templateSpans, receiveRightPaddedTree)!);
templateExpression = templateExpression.withType(ctx.receiveValue(templateExpression.type, ValueType.Object));
return templateExpression;
}

public visitTemplateExpressionValue(value: TemplateExpression.Value, ctx: ReceiverContext): J {
value = value.withId(ctx.receiveValue(value.id, ValueType.UUID)!);
value = value.withPrefix(ctx.receiveNode(value.prefix, receiveSpace)!);
value = value.withMarkers(ctx.receiveNode(value.markers, ctx.receiveMarkers)!);
value = value.withTree(ctx.receiveNode(value.tree, ctx.receiveTree)!);
value = value.withAfter(ctx.receiveNode(value.after, receiveSpace)!);
value = value.withEnclosedInBraces(ctx.receiveValue(value.enclosedInBraces, ValueType.Primitive)!);
return value;
public visitTemplateExpressionTemplateSpan(templateSpan: TemplateExpression.TemplateSpan, ctx: ReceiverContext): J {
templateSpan = templateSpan.withId(ctx.receiveValue(templateSpan.id, ValueType.UUID)!);
templateSpan = templateSpan.withPrefix(ctx.receiveNode(templateSpan.prefix, receiveSpace)!);
templateSpan = templateSpan.withMarkers(ctx.receiveNode(templateSpan.markers, ctx.receiveMarkers)!);
templateSpan = templateSpan.withExpression(ctx.receiveNode(templateSpan.expression, ctx.receiveTree)!);
templateSpan = templateSpan.withTail(ctx.receiveNode(templateSpan.tail, ctx.receiveTree)!);
return templateSpan;
}

public visitTuple(tuple: Tuple, ctx: ReceiverContext): J {
Expand Down Expand Up @@ -1173,6 +1202,17 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$DefaultType") {
return new DefaultType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down Expand Up @@ -1214,6 +1254,17 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$FunctionType") {
return new FunctionType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down Expand Up @@ -1305,26 +1356,36 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$TemplateExpression") {
return new TemplateExpression(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveValue(null, ValueType.Primitive)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree),
ctx.receiveNodes<J>(null, ctx.receiveTree)!,
ctx.receiveNode<Java.Literal>(null, ctx.receiveTree)!,
ctx.receiveNodes(null, receiveRightPaddedTree)!,
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.javascript.tree.JS$TemplateExpression$Value") {
return new TemplateExpression.Value(
if (type === "org.openrewrite.javascript.tree.JS$TemplateExpression$TemplateSpan") {
return new TemplateExpression.TemplateSpan(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<J>(null, ctx.receiveTree)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveValue(null, ValueType.Primitive)!
ctx.receiveNode<Java.Literal>(null, ctx.receiveTree)!
);
}

Expand Down
Loading