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

Refactored arrow function and fix errors #169

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 35 additions & 8 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1833,8 +1833,15 @@ export class JavaScriptParserVisitor {
Markers.EMPTY,
null,
Space.EMPTY,
this.visit(node.expression),
this.mapCommaSeparatedList(node.arguments ? node.getChildren(this.sourceFile).slice(2) : []),
node.typeArguments ? new J.ParameterizedType(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.visit(node.expression),
this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments),
null
): this.visit(node.expression),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
this.mapMethodType(node)
);
Expand Down Expand Up @@ -1913,8 +1920,7 @@ export class JavaScriptParserVisitor {
isParenthesized ? [this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!))] : [] // to handle the case: (/*no*/) => ...
),
this.mapTypeInfo(node),
this.prefix(node.equalsGreaterThanToken),
this.convert(node.body),
this.leftPadded(this.prefix(node.equalsGreaterThanToken), this.convert(node.body)),
this.mapType(node)
);
}
Expand Down Expand Up @@ -2089,6 +2095,9 @@ export class JavaScriptParserVisitor {
case ts.SyntaxKind.BarBarEqualsToken:
assignmentOperation = JS.JsAssignmentOperation.Type.Or;
break;
case ts.SyntaxKind.AsteriskAsteriskToken:
assignmentOperation = JS.JsAssignmentOperation.Type.Power;
break;
}

if (assignmentOperation !== undefined) {
Expand Down Expand Up @@ -2368,6 +2377,7 @@ export class JavaScriptParserVisitor {
}

visitSyntheticExpression(node: ts.SyntheticExpression) {
// SyntheticExpression is a special type of node used internally by the TypeScript compiler
return this.visitUnknown(node);
}

Expand Down Expand Up @@ -2431,6 +2441,7 @@ export class JavaScriptParserVisitor {

visitIfStatement(node: ts.IfStatement) {
const semicolonAfterThen = node.thenStatement.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken;
const semicolonAfterElse = node.elseStatement?.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken;
return new J.If(
randomId(),
this.prefix(node),
Expand All @@ -2452,8 +2463,8 @@ export class JavaScriptParserVisitor {
Markers.EMPTY,
this.rightPadded(
this.convert(node.elseStatement),
semicolonAfterThen ? this.prefix(node.elseStatement.getLastToken()!) : Space.EMPTY,
semicolonAfterThen ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
semicolonAfterElse ? this.prefix(node.elseStatement.getLastToken()!) : Space.EMPTY,
semicolonAfterElse ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
)
) : null
);
Expand Down Expand Up @@ -2641,7 +2652,23 @@ export class JavaScriptParserVisitor {
}

visitDebuggerStatement(node: ts.DebuggerStatement) {
return this.visitUnknown(node);
return new JS.DebuggerStatement(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(
new J.Literal(
randomId(),
this.prefix(node),
Markers.EMPTY,
null,
"debugger",
null,
this.mapPrimitiveType(node)
),
this.suffix(this.findChildNode(node, ts.SyntaxKind.DebuggerKeyword)!),
)
);
}

visitVariableDeclaration(node: ts.VariableDeclaration) {
Expand Down Expand Up @@ -2733,7 +2760,7 @@ export class JavaScriptParserVisitor {
);
}

private getParameterListNodes(node: ts.SignatureDeclarationBase, openToken : ts.SyntaxKind = ts.SyntaxKind.OpenParenToken) {
private getParameterListNodes(node: ts.SignatureDeclarationBase | ts.NewExpression, openToken : ts.SyntaxKind = ts.SyntaxKind.OpenParenToken) {
const children = node.getChildren(this.sourceFile);
for (let i = 0; i < children.length; i++) {
if (children[i].kind == openToken) {
Expand Down
25 changes: 20 additions & 5 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, TypeTreeExpression} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DebuggerStatement, 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 @@ -60,8 +60,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
arrowFunction = arrowFunction.withTypeParameters(ctx.receiveNode(arrowFunction.typeParameters, ctx.receiveTree));
arrowFunction = arrowFunction.withParameters(ctx.receiveNode(arrowFunction.parameters, ctx.receiveTree)!);
arrowFunction = arrowFunction.withReturnTypeExpression(ctx.receiveNode(arrowFunction.returnTypeExpression, ctx.receiveTree));
arrowFunction = arrowFunction.withArrow(ctx.receiveNode(arrowFunction.arrow, receiveSpace)!);
arrowFunction = arrowFunction.withBody(ctx.receiveNode(arrowFunction.body, ctx.receiveTree)!);
arrowFunction = arrowFunction.padding.withBody(ctx.receiveNode(arrowFunction.padding.body, receiveLeftPaddedTree)!);
arrowFunction = arrowFunction.withType(ctx.receiveValue(arrowFunction.type, ValueType.Object));
return arrowFunction;
}
Expand All @@ -85,6 +84,14 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return conditionalType;
}

public visitDebuggerStatement(debuggerStatement: DebuggerStatement, ctx: ReceiverContext): J {
debuggerStatement = debuggerStatement.withId(ctx.receiveValue(debuggerStatement.id, ValueType.UUID)!);
debuggerStatement = debuggerStatement.withPrefix(ctx.receiveNode(debuggerStatement.prefix, receiveSpace)!);
debuggerStatement = debuggerStatement.withMarkers(ctx.receiveNode(debuggerStatement.markers, ctx.receiveMarkers)!);
debuggerStatement = debuggerStatement.padding.withDebugger(ctx.receiveNode(debuggerStatement.padding.debugger, receiveRightPaddedTree)!);
return debuggerStatement;
}

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 @@ -1349,8 +1356,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode<Java.TypeParameters>(null, ctx.receiveTree),
ctx.receiveNode<Java.Lambda.Parameters>(null, ctx.receiveTree)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree),
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode<J>(null, ctx.receiveTree)!,
ctx.receiveNode<JLeftPadded<J>>(null, receiveLeftPaddedTree)!,
ctx.receiveValue(null, ValueType.Object)
);
}
Expand All @@ -1376,6 +1382,15 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$DefaultType") {
return new DefaultType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
13 changes: 10 additions & 3 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, TypeTreeExpression} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DebuggerStatement, 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 @@ -55,8 +55,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendNode(arrowFunction, v => v.typeParameters, ctx.sendTree);
ctx.sendNode(arrowFunction, v => v.parameters, ctx.sendTree);
ctx.sendNode(arrowFunction, v => v.returnTypeExpression, ctx.sendTree);
ctx.sendNode(arrowFunction, v => v.arrow, Visitor.sendSpace);
ctx.sendNode(arrowFunction, v => v.body, ctx.sendTree);
ctx.sendNode(arrowFunction, v => v.padding.body, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendTypedValue(arrowFunction, v => v.type, ValueType.Object);
return arrowFunction;
}
Expand All @@ -80,6 +79,14 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return conditionalType;
}

public visitDebuggerStatement(debuggerStatement: DebuggerStatement, ctx: SenderContext): J {
ctx.sendValue(debuggerStatement, v => v.id, ValueType.UUID);
ctx.sendNode(debuggerStatement, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(debuggerStatement, v => v.markers, ctx.sendMarkers);
ctx.sendNode(debuggerStatement, v => v.padding.debugger, Visitor.sendRightPadded(ValueType.Tree));
return debuggerStatement;
}

public visitDefaultType(defaultType: DefaultType, ctx: SenderContext): J {
ctx.sendValue(defaultType, v => v.id, ValueType.UUID);
ctx.sendNode(defaultType, v => v.prefix, Visitor.sendSpace);
Expand Down
3 changes: 3 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export namespace JsSpace {
MAPPED_TYPE_KEYS_REMAPPING_PREFIX,
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX,
TYPE_TREE_EXPRESSION_PREFIX,
DEBUGGER_STATEMENT_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down Expand Up @@ -302,6 +303,7 @@ export namespace JsLeftPadded {
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE_TYPE,
MAPPED_TYPE_HAS_READONLY,
MAPPED_TYPE_HAS_QUESTION_TOKEN,
ARROW_FUNCTION_BODY,
}
}
export namespace JsRightPadded {
Expand Down Expand Up @@ -330,6 +332,7 @@ export namespace JsRightPadded {
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT,
MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER,
MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE,
DEBUGGER_STATEMENT_DEBUGGER,
}
}
export namespace JsContainer {
Expand Down
Loading
Loading