Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 5e6df87

Browse files
committed
add parser fixes
1 parent 32b5410 commit 5e6df87

File tree

5 files changed

+100
-35
lines changed

5 files changed

+100
-35
lines changed

openrewrite/src/javascript/parser.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import * as J from '../java';
33
import {
4+
FieldAccess,
45
JavaType,
56
JContainer,
67
JLeftPadded,
@@ -1518,12 +1519,18 @@ export class JavaScriptParserVisitor {
15181519
}
15191520

15201521
visitParenthesizedExpression(node: ts.ParenthesizedExpression) {
1521-
return new J.Parentheses(
1522+
return new J.ParenthesizedTypeTree(
15221523
randomId(),
1523-
this.prefix(node),
1524+
Space.EMPTY,
15241525
Markers.EMPTY,
1525-
this.rightPadded(this.convert(node.expression), this.prefix(node.getLastToken()!))
1526-
)
1526+
[],
1527+
new J.Parentheses(
1528+
randomId(),
1529+
this.prefix(node),
1530+
Markers.EMPTY,
1531+
this.rightPadded(this.convert(node.expression), this.prefix(node.getLastToken()!))
1532+
)
1533+
);
15271534
}
15281535

15291536
visitFunctionExpression(node: ts.FunctionExpression) {
@@ -2437,9 +2444,11 @@ export class JavaScriptParserVisitor {
24372444
keywordType
24382445
),
24392446
this.rightPadded(
2440-
new J.FieldAccess(
2447+
(body.name instanceof J.FieldAccess)
2448+
? this.remapFieldAccess(body.name, node.name)
2449+
: new J.FieldAccess(
24412450
randomId(),
2442-
this.prefix(node),
2451+
Space.EMPTY,
24432452
Markers.EMPTY,
24442453
this.visit(node.name),
24452454
new J.JLeftPadded(
@@ -2449,7 +2458,7 @@ export class JavaScriptParserVisitor {
24492458
),
24502459
null
24512460
),
2452-
Space.EMPTY
2461+
body.padding.name.after
24532462
),
24542463
body.body
24552464
);
@@ -2463,12 +2472,44 @@ export class JavaScriptParserVisitor {
24632472
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
24642473
keywordType
24652474
),
2466-
this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess
2475+
this.rightPadded(this.convert(node.name), this.suffix(node.name)), // J.FieldAccess
24672476
body // J.Block
24682477
);
24692478
}
24702479
}
24712480

2481+
private remapFieldAccess(fa: FieldAccess, name: ts.ModuleName): FieldAccess {
2482+
if (fa.target instanceof J.Identifier) {
2483+
return new J.FieldAccess(
2484+
randomId(),
2485+
Space.EMPTY,
2486+
Markers.EMPTY,
2487+
new J.FieldAccess(
2488+
randomId(),
2489+
Space.EMPTY,
2490+
Markers.EMPTY,
2491+
this.visit(name),
2492+
this.leftPadded(
2493+
this.suffix(name),
2494+
fa.target
2495+
),
2496+
null
2497+
),
2498+
fa.padding.name,
2499+
null
2500+
);
2501+
}
2502+
2503+
return new J.FieldAccess(
2504+
randomId(),
2505+
Space.EMPTY,
2506+
Markers.EMPTY,
2507+
this.remapFieldAccess(fa.target as FieldAccess, name),
2508+
fa.padding.name,
2509+
null
2510+
);
2511+
}
2512+
24722513
visitModuleBlock(node: ts.ModuleBlock) {
24732514
return new J.Block(
24742515
randomId(),

openrewrite/src/javascript/remote/receiver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ class Factory implements ReceiverFactory {
15211521
ctx.receiveValue(null, ValueType.UUID)!,
15221522
ctx.receiveNode(null, receiveSpace)!,
15231523
ctx.receiveNode(null, ctx.receiveMarkers)!,
1524-
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
1524+
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!,
15251525
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!
15261526
);
15271527
}

openrewrite/src/javascript/tree/tree.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ export class PropertyAssignment extends JSMixin(Object) implements Statement, Ty
15121512

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

1575-
private readonly _variables: JRightPadded<Expression>[];
1575+
private readonly _variables: JRightPadded<J>[];
15761576

1577-
public get variables(): Expression[] {
1577+
public get variables(): J[] {
15781578
return JRightPadded.getElements(this._variables);
15791579
}
15801580

1581-
public withVariables(variables: Expression[]): ScopedVariableDeclarations {
1581+
public withVariables(variables: J[]): ScopedVariableDeclarations {
15821582
return this.padding.withVariables(JRightPadded.withElements(this._variables, variables));
15831583
}
15841584

@@ -1595,10 +1595,10 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State
15951595
public withScope(scope: JLeftPadded<ScopedVariableDeclarations.Scope> | null): ScopedVariableDeclarations {
15961596
return t._scope === scope ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, scope, t._variables);
15971597
}
1598-
public get variables(): JRightPadded<Expression>[] {
1598+
public get variables(): JRightPadded<J>[] {
15991599
return t._variables;
16001600
}
1601-
public withVariables(variables: JRightPadded<Expression>[]): ScopedVariableDeclarations {
1601+
public withVariables(variables: JRightPadded<J>[]): ScopedVariableDeclarations {
16021602
return t._variables === variables ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, t._scope, variables);
16031603
}
16041604
}
@@ -3352,7 +3352,7 @@ export class JSForInLoop extends JSMixin(Object) implements Loop {
33523352

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

3394-
private readonly _variable: JRightPadded<Expression>;
3394+
private readonly _variable: JRightPadded<Statement>;
33953395

3396-
public get variable(): Expression {
3396+
public get variable(): Statement {
33973397
return this._variable.element;
33983398
}
33993399

3400-
public withVariable(variable: Expression): JSForInOfLoopControl {
3400+
public withVariable(variable: Statement): JSForInOfLoopControl {
34013401
return this.padding.withVariable(this._variable.withElement(variable));
34023402
}
34033403

@@ -3418,10 +3418,10 @@ export class JSForInOfLoopControl extends JSMixin(Object) {
34183418
get padding() {
34193419
const t = this;
34203420
return new class {
3421-
public get variable(): JRightPadded<Expression> {
3421+
public get variable(): JRightPadded<Statement> {
34223422
return t._variable;
34233423
}
3424-
public withVariable(variable: JRightPadded<Expression>): JSForInOfLoopControl {
3424+
public withVariable(variable: JRightPadded<Statement>): JSForInOfLoopControl {
34253425
return t._variable === variable ? t : new JSForInOfLoopControl(t._id, t._prefix, t._markers, variable, t._iterable);
34263426
}
34273427
public get iterable(): JRightPadded<Expression> {

rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
package org.openrewrite.javascript.remote;
2424

2525
import org.jspecify.annotations.Nullable;
26-
import org.openrewrite.Tree;
26+
import org.openrewrite.*;
2727
import org.openrewrite.internal.ListUtils;
28+
import org.openrewrite.marker.Markers;
29+
import org.openrewrite.tree.*;
30+
import org.openrewrite.java.JavaVisitor;
2831
import org.openrewrite.java.tree.*;
2932
import org.openrewrite.javascript.JavaScriptIsoVisitor;
30-
import org.openrewrite.javascript.tree.JS;
33+
import org.openrewrite.javascript.tree.*;
3134

3235
import java.util.List;
3336

@@ -160,7 +163,7 @@ public JS.PropertyAssignment visitPropertyAssignment(JS.PropertyAssignment prope
160163
@Override
161164
public JS.ScopedVariableDeclarations visitScopedVariableDeclarations(JS.ScopedVariableDeclarations scopedVariableDeclarations, P p) {
162165
ListUtils.map(scopedVariableDeclarations.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p));
163-
ListUtils.map(scopedVariableDeclarations.getVariables(), el -> visitAndValidate(el, Expression.class, p));
166+
ListUtils.map(scopedVariableDeclarations.getVariables(), el -> visitAndValidate(el, J.class, p));
164167
return scopedVariableDeclarations;
165168
}
166169

@@ -285,6 +288,27 @@ public JS.JSMethodInvocation visitJSMethodInvocation(JS.JSMethodInvocation jSMet
285288
return jSMethodInvocation;
286289
}
287290

291+
@Override
292+
public JS.JSForOfLoop visitJSForOfLoop(JS.JSForOfLoop jSForOfLoop, P p) {
293+
visitAndValidate(jSForOfLoop.getControl(), JS.JSForInOfLoopControl.class, p);
294+
visitAndValidate(jSForOfLoop.getBody(), Statement.class, p);
295+
return jSForOfLoop;
296+
}
297+
298+
@Override
299+
public JS.JSForInLoop visitJSForInLoop(JS.JSForInLoop jSForInLoop, P p) {
300+
visitAndValidate(jSForInLoop.getControl(), JS.JSForInOfLoopControl.class, p);
301+
visitAndValidate(jSForInLoop.getBody(), Statement.class, p);
302+
return jSForInLoop;
303+
}
304+
305+
@Override
306+
public JS.JSForInOfLoopControl visitJSForInOfLoopControl(JS.JSForInOfLoopControl jSForInOfLoopControl, P p) {
307+
visitAndValidate(jSForInOfLoopControl.getVariable(), Statement.class, p);
308+
visitAndValidate(jSForInOfLoopControl.getIterable(), Expression.class, p);
309+
return jSForInOfLoopControl;
310+
}
311+
288312
@Override
289313
public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, P p) {
290314
ListUtils.map(namespaceDeclaration.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p));
@@ -349,7 +373,7 @@ public J.ArrayType visitArrayType(J.ArrayType arrayType, P p) {
349373
@Override
350374
public J.Assert visitAssert(J.Assert assert_, P p) {
351375
visitAndValidate(assert_.getCondition(), Expression.class, p);
352-
visitAndValidate(assert_.getDetail().getElement(), Expression.class, p);
376+
visitAndValidate(assert_.getDetail() != null ? assert_.getDetail().getElement() : null, Expression.class, p);
353377
return assert_;
354378
}
355379

rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,13 +1462,13 @@ public ScopedVariableDeclarations withScope(@Nullable Scope scope) {
14621462
return getPadding().withScope(JLeftPadded.withElement(this.scope, scope));
14631463
}
14641464

1465-
List<JRightPadded<Expression>> variables;
1465+
List<JRightPadded<J>> variables;
14661466

1467-
public List<Expression> getVariables() {
1467+
public List<J> getVariables() {
14681468
return JRightPadded.getElements(variables);
14691469
}
14701470

1471-
public ScopedVariableDeclarations withVariables(List<Expression> variables) {
1471+
public ScopedVariableDeclarations withVariables(List<J> variables) {
14721472
return getPadding().withVariables(JRightPadded.withElements(this.variables, variables));
14731473
}
14741474

@@ -1507,11 +1507,11 @@ public enum Scope {
15071507
public static class Padding {
15081508
private final ScopedVariableDeclarations t;
15091509

1510-
public List<JRightPadded<Expression>> getVariables() {
1510+
public List<JRightPadded<J>> getVariables() {
15111511
return t.variables;
15121512
}
15131513

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

@@ -3101,13 +3101,13 @@ final class JSForInOfLoopControl implements JS {
31013101
@Getter
31023102
Markers markers;
31033103

3104-
JRightPadded<Expression> variable;
3104+
JRightPadded<Statement> variable;
31053105

3106-
public Expression getVariable() {
3106+
public Statement getVariable() {
31073107
return variable.getElement();
31083108
}
31093109

3110-
public JSForInOfLoopControl withVariable(Expression variable) {
3110+
public JSForInOfLoopControl withVariable(Statement variable) {
31113111
return getPadding().withVariable(this.variable.withElement(variable));
31123112
}
31133113

@@ -3150,11 +3150,11 @@ public String toString() {
31503150
public static class Padding {
31513151
private final JSForInOfLoopControl t;
31523152

3153-
public JRightPadded<Expression> getVariable() {
3153+
public JRightPadded<Statement> getVariable() {
31543154
return t.variable;
31553155
}
31563156

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

0 commit comments

Comments
 (0)