Skip to content

Commit

Permalink
Refactored arrow function and fix errors (#169)
Browse files Browse the repository at this point in the history
* Refactored arrow function and fix errors
 - fix power binary "**"
- fix J.NewExpression with type arguments
- exclamation token for PropertyDeclaration

---------

Co-authored-by: Andrii Rodionov <[email protected]>
  • Loading branch information
arodionov and Andrii Rodionov authored Dec 12, 2024
1 parent 21ca3f6 commit 9c59f7e
Show file tree
Hide file tree
Showing 21 changed files with 327 additions and 86 deletions.
141 changes: 111 additions & 30 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,14 @@ export class JavaScriptParserVisitor {
return this.mapIdentifier(node, 'never');
}

visitSymbolKeyword(node: ts.Node) {
return this.mapIdentifier(node, 'symbol');
}

visitBigIntKeyword(node: ts.Node) {
return this.mapIdentifier(node, 'bigint');
}

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(
Expand Down Expand Up @@ -815,28 +823,54 @@ export class JavaScriptParserVisitor {
);
}

return new J.VariableDeclarations(
randomId(),
this.prefix(node),
Markers.EMPTY,
[], // no decorators allowed
this.mapModifiers(node),
this.mapTypeInfo(node),
null,
[],
[this.rightPadded(
new J.VariableDeclarations.NamedVariable(
randomId(),
this.prefix(node.name),
Markers.EMPTY,
this.visit(node.name),
[],
null,
this.mapVariableType(node)
),
Space.EMPTY
)]
);
const nameExpression = this.visit(node.name)

if (nameExpression instanceof J.Identifier) {
return new J.VariableDeclarations(
randomId(),
this.prefix(node),
Markers.EMPTY,
[], // no decorators allowed
this.mapModifiers(node),
this.mapTypeInfo(node),
null,
[],
[this.rightPadded(
new J.VariableDeclarations.NamedVariable(
randomId(),
this.prefix(node.name),
Markers.EMPTY,
nameExpression,
[],
null,
this.mapVariableType(node)
),
Space.EMPTY
)]
);
} else {
return new JS.JSVariableDeclarations(
randomId(),
this.prefix(node),
Markers.EMPTY,
[], // no decorators allowed
this.mapModifiers(node),
this.mapTypeInfo(node),
null,
[this.rightPadded(
new JS.JSVariableDeclarations.JSNamedVariable(
randomId(),
this.prefix(node.name),
Markers.EMPTY,
nameExpression,
[],
null,
this.mapVariableType(node)
),
Space.EMPTY
)]
);
}
}

visitPropertyDeclaration(node: ts.PropertyDeclaration) {
Expand Down Expand Up @@ -864,6 +898,37 @@ export class JavaScriptParserVisitor {
);
}

if (node.exclamationToken) {
return new JS.JSVariableDeclarations(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapDecorators(node),
this.mapModifiers(node),
this.mapTypeInfo(node),
null,
[this.rightPadded(
new JS.JSVariableDeclarations.JSNamedVariable(
randomId(),
this.prefix(node.name),
Markers.EMPTY,
new JS.Unary(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.leftPadded(this.suffix(node.name), JS.Unary.Type.Exclamation),
this.visit(node.name),
this.mapType(node)
),
[],
node.initializer ? this.leftPadded(this.prefix(node.getChildAt(node.getChildren().indexOf(node.initializer) - 1)), this.visit(node.initializer)) : null,
this.mapVariableType(node)
),
Space.EMPTY
)]
);
}

const nameExpression = this.visit(node.name)

if (nameExpression instanceof J.Identifier) {
Expand Down Expand Up @@ -1408,6 +1473,8 @@ export class JavaScriptParserVisitor {
return JS.TypeOperator.Type.KeyOf;
case ts.SyntaxKind.ReadonlyKeyword:
return JS.TypeOperator.Type.ReadOnly;
case ts.SyntaxKind.UniqueKeyword:
return JS.TypeOperator.Type.Unique;
}
}

Expand Down Expand Up @@ -1833,8 +1900,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 +1987,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 +2162,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 +2444,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 +2508,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 +2530,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 +2719,10 @@ export class JavaScriptParserVisitor {
}

visitDebuggerStatement(node: ts.DebuggerStatement) {
return this.visitUnknown(node);
return new ExpressionStatement(
randomId(),
this.mapIdentifier(node, 'debugger')
);
}

visitVariableDeclaration(node: ts.VariableDeclaration) {
Expand Down Expand Up @@ -2733,7 +2814,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
6 changes: 2 additions & 4 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -1349,8 +1348,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 Down
3 changes: 1 addition & 2 deletions openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
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 Down
1 change: 1 addition & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,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
Loading

0 comments on commit 9c59f7e

Please sign in to comment.