Skip to content

Commit

Permalink
add parser fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Nov 22, 2024
1 parent 32b5410 commit 5e6df87
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
57 changes: 49 additions & 8 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
import * as J from '../java';
import {
FieldAccess,
JavaType,
JContainer,
JLeftPadded,
Expand Down Expand Up @@ -1518,12 +1519,18 @@ export class JavaScriptParserVisitor {
}

visitParenthesizedExpression(node: ts.ParenthesizedExpression) {
return new J.Parentheses(
return new J.ParenthesizedTypeTree(
randomId(),
this.prefix(node),
Space.EMPTY,
Markers.EMPTY,
this.rightPadded(this.convert(node.expression), this.prefix(node.getLastToken()!))
)
[],
new J.Parentheses(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(this.convert(node.expression), this.prefix(node.getLastToken()!))
)
);
}

visitFunctionExpression(node: ts.FunctionExpression) {
Expand Down Expand Up @@ -2437,9 +2444,11 @@ export class JavaScriptParserVisitor {
keywordType
),
this.rightPadded(
new J.FieldAccess(
(body.name instanceof J.FieldAccess)
? this.remapFieldAccess(body.name, node.name)
: new J.FieldAccess(
randomId(),
this.prefix(node),
Space.EMPTY,
Markers.EMPTY,
this.visit(node.name),
new J.JLeftPadded(
Expand All @@ -2449,7 +2458,7 @@ export class JavaScriptParserVisitor {
),
null
),
Space.EMPTY
body.padding.name.after
),
body.body
);
Expand All @@ -2463,12 +2472,44 @@ export class JavaScriptParserVisitor {
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
keywordType
),
this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess
this.rightPadded(this.convert(node.name), this.suffix(node.name)), // J.FieldAccess
body // J.Block
);
}
}

private remapFieldAccess(fa: FieldAccess, name: ts.ModuleName): FieldAccess {
if (fa.target instanceof J.Identifier) {
return new J.FieldAccess(
randomId(),
Space.EMPTY,
Markers.EMPTY,
new J.FieldAccess(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.visit(name),
this.leftPadded(
this.suffix(name),
fa.target
),
null
),
fa.padding.name,
null
);
}

return new J.FieldAccess(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.remapFieldAccess(fa.target as FieldAccess, name),
fa.padding.name,
null
);
}

visitModuleBlock(node: ts.ModuleBlock) {
return new J.Block(
randomId(),
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!
);
}
Expand Down
24 changes: 12 additions & 12 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ export class PropertyAssignment extends JSMixin(Object) implements Statement, Ty

@LstType("org.openrewrite.javascript.tree.JS$ScopedVariableDeclarations")
export class ScopedVariableDeclarations extends JSMixin(Object) implements Statement {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], scope: JLeftPadded<ScopedVariableDeclarations.Scope> | null, variables: JRightPadded<Expression>[]) {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], scope: JLeftPadded<ScopedVariableDeclarations.Scope> | null, variables: JRightPadded<J>[]) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -1572,13 +1572,13 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State
return this.padding.withScope(JLeftPadded.withElement(this._scope, scope));
}

private readonly _variables: JRightPadded<Expression>[];
private readonly _variables: JRightPadded<J>[];

public get variables(): Expression[] {
public get variables(): J[] {
return JRightPadded.getElements(this._variables);
}

public withVariables(variables: Expression[]): ScopedVariableDeclarations {
public withVariables(variables: J[]): ScopedVariableDeclarations {
return this.padding.withVariables(JRightPadded.withElements(this._variables, variables));
}

Expand All @@ -1595,10 +1595,10 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State
public withScope(scope: JLeftPadded<ScopedVariableDeclarations.Scope> | null): ScopedVariableDeclarations {
return t._scope === scope ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, scope, t._variables);
}
public get variables(): JRightPadded<Expression>[] {
public get variables(): JRightPadded<J>[] {
return t._variables;
}
public withVariables(variables: JRightPadded<Expression>[]): ScopedVariableDeclarations {
public withVariables(variables: JRightPadded<J>[]): ScopedVariableDeclarations {
return t._variables === variables ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, t._scope, variables);
}
}
Expand Down Expand Up @@ -3352,7 +3352,7 @@ export class JSForInLoop extends JSMixin(Object) implements Loop {

@LstType("org.openrewrite.javascript.tree.JS$JSForInOfLoopControl")
export class JSForInOfLoopControl extends JSMixin(Object) {
public constructor(id: UUID, prefix: Space, markers: Markers, variable: JRightPadded<Expression>, iterable: JRightPadded<Expression>) {
public constructor(id: UUID, prefix: Space, markers: Markers, variable: JRightPadded<Statement>, iterable: JRightPadded<Expression>) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -3391,13 +3391,13 @@ export class JSForInOfLoopControl extends JSMixin(Object) {
return markers === this._markers ? this : new JSForInOfLoopControl(this._id, this._prefix, markers, this._variable, this._iterable);
}

private readonly _variable: JRightPadded<Expression>;
private readonly _variable: JRightPadded<Statement>;

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

public withVariable(variable: Expression): JSForInOfLoopControl {
public withVariable(variable: Statement): JSForInOfLoopControl {
return this.padding.withVariable(this._variable.withElement(variable));
}

Expand All @@ -3418,10 +3418,10 @@ export class JSForInOfLoopControl extends JSMixin(Object) {
get padding() {
const t = this;
return new class {
public get variable(): JRightPadded<Expression> {
public get variable(): JRightPadded<Statement> {
return t._variable;
}
public withVariable(variable: JRightPadded<Expression>): JSForInOfLoopControl {
public withVariable(variable: JRightPadded<Statement>): JSForInOfLoopControl {
return t._variable === variable ? t : new JSForInOfLoopControl(t._id, t._prefix, t._markers, variable, t._iterable);
}
public get iterable(): JRightPadded<Expression> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
package org.openrewrite.javascript.remote;

import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.marker.Markers;
import org.openrewrite.tree.*;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.*;
import org.openrewrite.javascript.JavaScriptIsoVisitor;
import org.openrewrite.javascript.tree.JS;
import org.openrewrite.javascript.tree.*;

import java.util.List;

Expand Down Expand Up @@ -160,7 +163,7 @@ public JS.PropertyAssignment visitPropertyAssignment(JS.PropertyAssignment prope
@Override
public JS.ScopedVariableDeclarations visitScopedVariableDeclarations(JS.ScopedVariableDeclarations scopedVariableDeclarations, P p) {
ListUtils.map(scopedVariableDeclarations.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p));
ListUtils.map(scopedVariableDeclarations.getVariables(), el -> visitAndValidate(el, Expression.class, p));
ListUtils.map(scopedVariableDeclarations.getVariables(), el -> visitAndValidate(el, J.class, p));
return scopedVariableDeclarations;
}

Expand Down Expand Up @@ -285,6 +288,27 @@ public JS.JSMethodInvocation visitJSMethodInvocation(JS.JSMethodInvocation jSMet
return jSMethodInvocation;
}

@Override
public JS.JSForOfLoop visitJSForOfLoop(JS.JSForOfLoop jSForOfLoop, P p) {
visitAndValidate(jSForOfLoop.getControl(), JS.JSForInOfLoopControl.class, p);
visitAndValidate(jSForOfLoop.getBody(), Statement.class, p);
return jSForOfLoop;
}

@Override
public JS.JSForInLoop visitJSForInLoop(JS.JSForInLoop jSForInLoop, P p) {
visitAndValidate(jSForInLoop.getControl(), JS.JSForInOfLoopControl.class, p);
visitAndValidate(jSForInLoop.getBody(), Statement.class, p);
return jSForInLoop;
}

@Override
public JS.JSForInOfLoopControl visitJSForInOfLoopControl(JS.JSForInOfLoopControl jSForInOfLoopControl, P p) {
visitAndValidate(jSForInOfLoopControl.getVariable(), Statement.class, p);
visitAndValidate(jSForInOfLoopControl.getIterable(), Expression.class, p);
return jSForInOfLoopControl;
}

@Override
public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, P p) {
ListUtils.map(namespaceDeclaration.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p));
Expand Down Expand Up @@ -349,7 +373,7 @@ public J.ArrayType visitArrayType(J.ArrayType arrayType, P p) {
@Override
public J.Assert visitAssert(J.Assert assert_, P p) {
visitAndValidate(assert_.getCondition(), Expression.class, p);
visitAndValidate(assert_.getDetail().getElement(), Expression.class, p);
visitAndValidate(assert_.getDetail() != null ? assert_.getDetail().getElement() : null, Expression.class, p);
return assert_;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1462,13 +1462,13 @@ public ScopedVariableDeclarations withScope(@Nullable Scope scope) {
return getPadding().withScope(JLeftPadded.withElement(this.scope, scope));
}

List<JRightPadded<Expression>> variables;
List<JRightPadded<J>> variables;

public List<Expression> getVariables() {
public List<J> getVariables() {
return JRightPadded.getElements(variables);
}

public ScopedVariableDeclarations withVariables(List<Expression> variables) {
public ScopedVariableDeclarations withVariables(List<J> variables) {
return getPadding().withVariables(JRightPadded.withElements(this.variables, variables));
}

Expand Down Expand Up @@ -1507,11 +1507,11 @@ public enum Scope {
public static class Padding {
private final ScopedVariableDeclarations t;

public List<JRightPadded<Expression>> getVariables() {
public List<JRightPadded<J>> getVariables() {
return t.variables;
}

public ScopedVariableDeclarations withVariables(List<JRightPadded<Expression>> variables) {
public ScopedVariableDeclarations withVariables(List<JRightPadded<J>> variables) {
return t.variables == variables ? t : new ScopedVariableDeclarations(t.id, t.prefix, t.markers, t.modifiers, t.scope, variables);
}

Expand Down Expand Up @@ -3101,13 +3101,13 @@ final class JSForInOfLoopControl implements JS {
@Getter
Markers markers;

JRightPadded<Expression> variable;
JRightPadded<Statement> variable;

public Expression getVariable() {
public Statement getVariable() {
return variable.getElement();
}

public JSForInOfLoopControl withVariable(Expression variable) {
public JSForInOfLoopControl withVariable(Statement variable) {
return getPadding().withVariable(this.variable.withElement(variable));
}

Expand Down Expand Up @@ -3150,11 +3150,11 @@ public String toString() {
public static class Padding {
private final JSForInOfLoopControl t;

public JRightPadded<Expression> getVariable() {
public JRightPadded<Statement> getVariable() {
return t.variable;
}

public JSForInOfLoopControl withVariable(JRightPadded<Expression> variable) {
public JSForInOfLoopControl withVariable(JRightPadded<Statement> variable) {
return t.variable == variable ? t : new JSForInOfLoopControl(t.id, t.prefix, t.markers, variable, t.iterable);
}

Expand Down

0 comments on commit 5e6df87

Please sign in to comment.