Skip to content

Commit

Permalink
- introduced JS.TypePredicate, JS.LiteralType
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Rodionov committed Nov 28, 2024
1 parent 78422d9 commit bc58313
Show file tree
Hide file tree
Showing 16 changed files with 632 additions and 6 deletions.
18 changes: 16 additions & 2 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,15 @@ export class JavaScriptParserVisitor {
}

visitTypePredicate(node: ts.TypePredicateNode) {
return this.visitUnknown(node);
return new JS.TypePredicate(
randomId(),
this.prefix(node),
Markers.EMPTY,
node.assertsModifier ? this.leftPadded(this.prefix(node.assertsModifier), true) : this.leftPadded(Space.EMPTY, false),
this.visit(node.parameterName),
node.type ? this.leftPadded(this.suffix(node.parameterName), this.convert(node.type)) : null,
this.mapType(node)
);
}

visitTypeReference(node: ts.TypeReferenceNode) {
Expand Down Expand Up @@ -1434,7 +1442,13 @@ export class JavaScriptParserVisitor {
}

visitLiteralType(node: ts.LiteralTypeNode) {
return this.visit(node.literal);
return new JS.LiteralType(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.literal),
this.mapType(node)!
);
}

visitNamedTupleMember(node: ts.NamedTupleMember) {
Expand Down
44 changes: 43 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, 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 {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, 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} 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 @@ -187,6 +187,15 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return jsBinary;
}

public visitLiteralType(literalType: LiteralType, ctx: ReceiverContext): J {
literalType = literalType.withId(ctx.receiveValue(literalType.id, ValueType.UUID)!);
literalType = literalType.withPrefix(ctx.receiveNode(literalType.prefix, receiveSpace)!);
literalType = literalType.withMarkers(ctx.receiveNode(literalType.markers, ctx.receiveMarkers)!);
literalType = literalType.withLiteral(ctx.receiveNode(literalType.literal, ctx.receiveTree)!);
literalType = literalType.withType(ctx.receiveValue(literalType.type, ValueType.Object)!);
return literalType;
}

public visitObjectBindingDeclarations(objectBindingDeclarations: ObjectBindingDeclarations, ctx: ReceiverContext): J {
objectBindingDeclarations = objectBindingDeclarations.withId(ctx.receiveValue(objectBindingDeclarations.id, ValueType.UUID)!);
objectBindingDeclarations = objectBindingDeclarations.withPrefix(ctx.receiveNode(objectBindingDeclarations.prefix, receiveSpace)!);
Expand Down Expand Up @@ -302,6 +311,17 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return typeOperator;
}

public visitTypePredicate(typePredicate: TypePredicate, ctx: ReceiverContext): J {
typePredicate = typePredicate.withId(ctx.receiveValue(typePredicate.id, ValueType.UUID)!);
typePredicate = typePredicate.withPrefix(ctx.receiveNode(typePredicate.prefix, receiveSpace)!);
typePredicate = typePredicate.withMarkers(ctx.receiveNode(typePredicate.markers, ctx.receiveMarkers)!);
typePredicate = typePredicate.padding.withAsserts(ctx.receiveNode(typePredicate.padding.asserts, leftPaddedValueReceiver(ValueType.Primitive))!);
typePredicate = typePredicate.withParameterName(ctx.receiveNode(typePredicate.parameterName, ctx.receiveTree)!);
typePredicate = typePredicate.padding.withExpression(ctx.receiveNode(typePredicate.padding.expression, receiveLeftPaddedTree));
typePredicate = typePredicate.withType(ctx.receiveValue(typePredicate.type, ValueType.Object));
return typePredicate;
}

public visitJsUnary(unary: Unary, ctx: ReceiverContext): J {
unary = unary.withId(ctx.receiveValue(unary.id, ValueType.UUID)!);
unary = unary.withPrefix(ctx.receiveNode(unary.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1334,6 +1354,16 @@ class Factory implements ReceiverFactory {
);
}

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

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

if (type === "org.openrewrite.javascript.tree.JS$TypePredicate") {
return new TypePredicate(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JLeftPadded<boolean>>(null, leftPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<Java.Identifier>(null, ctx.receiveTree)!,
ctx.receiveNode<JLeftPadded<Expression>>(null, receiveLeftPaddedTree),
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.javascript.tree.JS$Unary") {
return new Unary(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
22 changes: 21 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, 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 {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, 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} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -182,6 +182,15 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return jsBinary;
}

public visitLiteralType(literalType: LiteralType, ctx: SenderContext): J {
ctx.sendValue(literalType, v => v.id, ValueType.UUID);
ctx.sendNode(literalType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(literalType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(literalType, v => v.literal, ctx.sendTree);
ctx.sendTypedValue(literalType, v => v.type, ValueType.Object);
return literalType;
}

public visitObjectBindingDeclarations(objectBindingDeclarations: ObjectBindingDeclarations, ctx: SenderContext): J {
ctx.sendValue(objectBindingDeclarations, v => v.id, ValueType.UUID);
ctx.sendNode(objectBindingDeclarations, v => v.prefix, Visitor.sendSpace);
Expand Down Expand Up @@ -297,6 +306,17 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return typeOperator;
}

public visitTypePredicate(typePredicate: TypePredicate, ctx: SenderContext): J {
ctx.sendValue(typePredicate, v => v.id, ValueType.UUID);
ctx.sendNode(typePredicate, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(typePredicate, v => v.markers, ctx.sendMarkers);
ctx.sendNode(typePredicate, v => v.padding.asserts, Visitor.sendLeftPadded(ValueType.Primitive));
ctx.sendNode(typePredicate, v => v.parameterName, ctx.sendTree);
ctx.sendNode(typePredicate, v => v.padding.expression, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendTypedValue(typePredicate, v => v.type, ValueType.Object);
return typePredicate;
}

public visitJsUnary(unary: Unary, ctx: SenderContext): J {
ctx.sendValue(unary, v => v.id, ValueType.UUID);
ctx.sendNode(unary, v => v.prefix, Visitor.sendSpace);
Expand Down
4 changes: 4 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ export namespace JsSpace {
TAGGED_TEMPLATE_EXPRESSION_PREFIX,
CONDITIONAL_TYPE_PREFIX,
INFER_TYPE_PREFIX,
TYPE_PREDICATE_PREFIX,
LITERAL_TYPE_PREFIX,
}
}
export namespace JsLeftPadded {
Expand All @@ -268,6 +270,8 @@ export namespace JsLeftPadded {
JSFOR_OF_LOOP_AWAIT,
BINDING_ELEMENT_INITIALIZER,
INFER_TYPE_TYPE_PARAMETER,
TYPE_PREDICATE_ASSERTS,
TYPE_PREDICATE_EXPRESSION,
}
}
export namespace JsRightPadded {
Expand Down
174 changes: 174 additions & 0 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,73 @@ export namespace JsBinary {

}

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

private readonly _id: UUID;

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

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

private readonly _prefix: Space;

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

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

private readonly _markers: Markers;

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

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

private readonly _literal: Expression;

public get literal(): Expression {
return this._literal;
}

public withLiteral(literal: Expression): LiteralType {
return literal === this._literal ? this : new LiteralType(this._id, this._prefix, this._markers, literal, this._type);
}

private readonly _type: JavaType;

public get type(): JavaType {
return this._type;
}

public withType(_type: JavaType): LiteralType {
return _type === this._type ? this : new LiteralType(this._id, this._prefix, this._markers, this._literal, _type);
}

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

}

@LstType("org.openrewrite.javascript.tree.JS$ObjectBindingDeclarations")
export class ObjectBindingDeclarations extends JSMixin(Object) implements Expression, TypedTree {
public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeExpression: TypeTree | null, bindings: JContainer<BindingElement>, initializer: JLeftPadded<Expression> | null) {
Expand Down Expand Up @@ -2446,6 +2513,113 @@ export namespace TypeOperator {

}

@LstType("org.openrewrite.javascript.tree.JS$TypePredicate")
export class TypePredicate extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, asserts: JLeftPadded<boolean>, parameterName: Java.Identifier, expression: JLeftPadded<Expression> | null, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._asserts = asserts;
this._parameterName = parameterName;
this._expression = expression;
this._type = _type;
}

private readonly _id: UUID;

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

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

private readonly _prefix: Space;

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

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

private readonly _markers: Markers;

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

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

private readonly _asserts: JLeftPadded<boolean>;

public get asserts(): boolean {
return this._asserts.element;
}

public withAsserts(asserts: boolean): TypePredicate {
return this.padding.withAsserts(this._asserts.withElement(asserts));
}

private readonly _parameterName: Java.Identifier;

public get parameterName(): Java.Identifier {
return this._parameterName;
}

public withParameterName(parameterName: Java.Identifier): TypePredicate {
return parameterName === this._parameterName ? this : new TypePredicate(this._id, this._prefix, this._markers, this._asserts, parameterName, this._expression, this._type);
}

private readonly _expression: JLeftPadded<Expression> | null;

public get expression(): Expression | null {
return this._expression === null ? null : this._expression.element;
}

public withExpression(expression: Expression | null): TypePredicate {
return this.padding.withExpression(JLeftPadded.withElement(this._expression, expression));
}

private readonly _type: JavaType | null;

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

public withType(_type: JavaType | null): TypePredicate {
return _type === this._type ? this : new TypePredicate(this._id, this._prefix, this._markers, this._asserts, this._parameterName, this._expression, _type);
}

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

get padding() {
const t = this;
return new class {
public get asserts(): JLeftPadded<boolean> {
return t._asserts;
}
public withAsserts(asserts: JLeftPadded<boolean>): TypePredicate {
return t._asserts === asserts ? t : new TypePredicate(t._id, t._prefix, t._markers, asserts, t._parameterName, t._expression, t._type);
}
public get expression(): JLeftPadded<Expression> | null {
return t._expression;
}
public withExpression(expression: JLeftPadded<Expression> | null): TypePredicate {
return t._expression === expression ? t : new TypePredicate(t._id, t._prefix, t._markers, t._asserts, t._parameterName, expression, t._type);
}
}
}

}

@LstType("org.openrewrite.javascript.tree.JS$Unary")
export class Unary extends JSMixin(Object) implements Statement, Expression, TypedTree {
public constructor(id: UUID, prefix: Space, markers: Markers, operator: JLeftPadded<Unary.Type>, expression: Expression, _type: JavaType | null) {
Expand Down
Loading

0 comments on commit bc58313

Please sign in to comment.