Skip to content

Commit

Permalink
add js assigment/binary ops & add bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Dec 5, 2024
1 parent 3be388d commit 2ff6cef
Show file tree
Hide file tree
Showing 19 changed files with 1,059 additions and 11 deletions.
46 changes: 39 additions & 7 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,8 @@ export class JavaScriptParserVisitor {
this.mapMethodType(node)
);
}

if (ts.isComputedPropertyName(node.name)) {
const name = node.name ? this.visit(node.name) : this.mapIdentifier(node, "");
if (!(name instanceof J.Identifier)) {
return new JS.JSMethodDeclaration(
randomId(),
this.prefix(node),
Expand All @@ -998,7 +998,7 @@ export class JavaScriptParserVisitor {
this.mapModifiers(node),
this.mapTypeParametersAsObject(node),
this.mapTypeInfo(node),
this.convert(node.name),
name,
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
Expand All @@ -1016,7 +1016,7 @@ export class JavaScriptParserVisitor {
this.mapTypeParametersAsObject(node),
this.mapTypeInfo(node),
new J.MethodDeclaration.IdentifierWithAnnotations(
node.name ? this.visit(node.name) : this.mapIdentifier(node, ""),
name,
[]
),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
Expand Down Expand Up @@ -1991,6 +1991,12 @@ export class JavaScriptParserVisitor {
case ts.SyntaxKind.QuestionQuestionToken:
binaryOperator = JS.JsBinary.Type.QuestionQuestion;
break;
case ts.SyntaxKind.InKeyword:
binaryOperator = JS.JsBinary.Type.In;
break;
case ts.SyntaxKind.CommaToken:
binaryOperator = JS.JsBinary.Type.Comma;
break;
}

if (binaryOperator !== undefined) {
Expand Down Expand Up @@ -2019,7 +2025,33 @@ export class JavaScriptParserVisitor {

binaryOperator = this.mapBinaryOperator(node);
if (binaryOperator === undefined) {
const assignmentOperation = this.mapAssignmentOperation(node);
let assignmentOperation;

switch (node.operatorToken.kind) {
case ts.SyntaxKind.QuestionQuestionEqualsToken:
assignmentOperation = JS.JsAssignmentOperation.Type.QuestionQuestion;
break;
case ts.SyntaxKind.AmpersandAmpersandEqualsToken:
assignmentOperation = JS.JsAssignmentOperation.Type.And;
break;
case ts.SyntaxKind.BarBarEqualsToken:
assignmentOperation = JS.JsAssignmentOperation.Type.Or;
break;
}

if (assignmentOperation !== undefined) {
return new JS.JsAssignmentOperation(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.left),
this.leftPadded(this.prefix(node.operatorToken), assignmentOperation),
this.convert(node.right),
this.mapType(node)
)
}

assignmentOperation = this.mapAssignmentOperation(node);
if (assignmentOperation === undefined) {
return this.visitUnknown(node);
}
Expand Down Expand Up @@ -2418,9 +2450,9 @@ export class JavaScriptParserVisitor {
(ts.isVariableDeclarationList(node.initializer) ? this.rightPadded(this.visit(node.initializer), Space.EMPTY) :
this.rightPadded(new ExpressionStatement(randomId(), this.visit(node.initializer)), this.suffix(node.initializer.getLastToken()!))) :
this.rightPadded(this.newJEmpty(), this.suffix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!))], // to handle for (/*_*/; ; );
node.condition ? this.rightPadded(this.convert(node.condition), this.suffix(node.condition.getLastToken()!)) :
node.condition ? this.rightPadded(ts.isStatement(node.condition) ? this.visit(node.condition) : new ExpressionStatement(randomId(), this.visit(node.condition)), this.suffix(node.condition.getLastToken()!)) :
this.rightPadded(this.newJEmpty(), this.suffix(this.findChildNode(node, ts.SyntaxKind.SemicolonToken)!)), // to handle for ( ;/*_*/; );
[node.incrementor ? this.rightPadded(this.convert(node.incrementor), this.suffix(node.incrementor.getLastToken()!)) :
[node.incrementor ? this.rightPadded(ts.isStatement(node.incrementor) ? this.visit(node.incrementor) : new ExpressionStatement(randomId(), this.visit(node.incrementor)), this.suffix(node.incrementor.getLastToken()!)) :
this.rightPadded(this.newJEmpty(this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)), Space.EMPTY)], // to handle for ( ; ;/*_*/);
),
this.rightPadded(
Expand Down
25 changes: 24 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} 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} 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 @@ -592,6 +592,17 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return indexType;
}

public visitJsAssignmentOperation(jsAssignmentOperation: JsAssignmentOperation, ctx: ReceiverContext): J {
jsAssignmentOperation = jsAssignmentOperation.withId(ctx.receiveValue(jsAssignmentOperation.id, ValueType.UUID)!);
jsAssignmentOperation = jsAssignmentOperation.withPrefix(ctx.receiveNode(jsAssignmentOperation.prefix, receiveSpace)!);
jsAssignmentOperation = jsAssignmentOperation.withMarkers(ctx.receiveNode(jsAssignmentOperation.markers, ctx.receiveMarkers)!);
jsAssignmentOperation = jsAssignmentOperation.withVariable(ctx.receiveNode(jsAssignmentOperation.variable, ctx.receiveTree)!);
jsAssignmentOperation = jsAssignmentOperation.padding.withOperator(ctx.receiveNode(jsAssignmentOperation.padding.operator, leftPaddedValueReceiver(ValueType.Enum))!);
jsAssignmentOperation = jsAssignmentOperation.withAssignment(ctx.receiveNode(jsAssignmentOperation.assignment, ctx.receiveTree)!);
jsAssignmentOperation = jsAssignmentOperation.withType(ctx.receiveValue(jsAssignmentOperation.type, ValueType.Object));
return jsAssignmentOperation;
}

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 @@ -1882,6 +1893,18 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
13 changes: 12 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} 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} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -587,6 +587,17 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return indexType;
}

public visitJsAssignmentOperation(jsAssignmentOperation: JsAssignmentOperation, ctx: SenderContext): J {
ctx.sendValue(jsAssignmentOperation, v => v.id, ValueType.UUID);
ctx.sendNode(jsAssignmentOperation, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jsAssignmentOperation, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jsAssignmentOperation, v => v.variable, ctx.sendTree);
ctx.sendNode(jsAssignmentOperation, v => v.padding.operator, Visitor.sendLeftPadded(ValueType.Enum));
ctx.sendNode(jsAssignmentOperation, v => v.assignment, ctx.sendTree);
ctx.sendTypedValue(jsAssignmentOperation, v => v.type, ValueType.Object);
return jsAssignmentOperation;
}

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
2 changes: 2 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export namespace JsSpace {
INDEXED_ACCESS_TYPE_INDEX_TYPE_PREFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_SUFFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT_SUFFIX,
JS_ASSIGNMENT_OPERATION_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down Expand Up @@ -291,6 +292,7 @@ export namespace JsLeftPadded {
EXPORT_DECLARATION_MODULE_SPECIFIER,
FUNCTION_DECLARATION_ASTERISK_TOKEN,
FUNCTION_DECLARATION_NAME,
JS_ASSIGNMENT_OPERATION_OPERATOR,
}
}
export namespace JsRightPadded {
Expand Down
112 changes: 112 additions & 0 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ export namespace JsBinary {
IdentityNotEquals = 2,
In = 3,
QuestionQuestion = 4,
Comma = 5,

}

Expand Down Expand Up @@ -5174,3 +5175,114 @@ export namespace IndexedAccessType {
}

}

@LstType("org.openrewrite.javascript.tree.JS$JsAssignmentOperation")
export class JsAssignmentOperation extends JSMixin(Object) implements Statement, Expression, TypedTree {
public constructor(id: UUID, prefix: Space, markers: Markers, variable: Expression, operator: JLeftPadded<JsAssignmentOperation.Type>, assignment: Expression, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._variable = variable;
this._operator = operator;
this._assignment = assignment;
this._type = _type;
}

private readonly _id: UUID;

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

public withId(id: UUID): JsAssignmentOperation {
return id === this._id ? this : new JsAssignmentOperation(id, this._prefix, this._markers, this._variable, this._operator, this._assignment, this._type);
}

private readonly _prefix: Space;

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

public withPrefix(prefix: Space): JsAssignmentOperation {
return prefix === this._prefix ? this : new JsAssignmentOperation(this._id, prefix, this._markers, this._variable, this._operator, this._assignment, this._type);
}

private readonly _markers: Markers;

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

public withMarkers(markers: Markers): JsAssignmentOperation {
return markers === this._markers ? this : new JsAssignmentOperation(this._id, this._prefix, markers, this._variable, this._operator, this._assignment, this._type);
}

private readonly _variable: Expression;

public get variable(): Expression {
return this._variable;
}

public withVariable(variable: Expression): JsAssignmentOperation {
return variable === this._variable ? this : new JsAssignmentOperation(this._id, this._prefix, this._markers, variable, this._operator, this._assignment, this._type);
}

private readonly _operator: JLeftPadded<JsAssignmentOperation.Type>;

public get operator(): JsAssignmentOperation.Type {
return this._operator.element;
}

public withOperator(operator: JsAssignmentOperation.Type): JsAssignmentOperation {
return this.padding.withOperator(this._operator.withElement(operator));
}

private readonly _assignment: Expression;

public get assignment(): Expression {
return this._assignment;
}

public withAssignment(assignment: Expression): JsAssignmentOperation {
return assignment === this._assignment ? this : new JsAssignmentOperation(this._id, this._prefix, this._markers, this._variable, this._operator, assignment, this._type);
}

private readonly _type: JavaType | null;

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

public withType(_type: JavaType | null): JsAssignmentOperation {
return _type === this._type ? this : new JsAssignmentOperation(this._id, this._prefix, this._markers, this._variable, this._operator, this._assignment, _type);
}

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

get padding() {
const t = this;
return new class {
public get operator(): JLeftPadded<JsAssignmentOperation.Type> {
return t._operator;
}
public withOperator(operator: JLeftPadded<JsAssignmentOperation.Type>): JsAssignmentOperation {
return t._operator === operator ? t : new JsAssignmentOperation(t._id, t._prefix, t._markers, t._variable, operator, t._assignment, t._type);
}
}
}

}

export namespace JsAssignmentOperation {
export enum Type {
QuestionQuestion = 0,
And = 1,
Or = 2,

}

}
Loading

0 comments on commit 2ff6cef

Please sign in to comment.