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

fix parsing errors #159

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 26 additions & 41 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,42 +1526,13 @@ export class JavaScriptParserVisitor {
const prefix = this.prefix(node);
const typeArguments = node.typeArguments ? this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments) : null;

if (ts.isParenthesizedExpression(node.expression)) {
return new JS.JSMethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
null,
typeArguments,
this.convert(node.expression),
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
);
}

if (node.questionDotToken) {
return new JS.JSMethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
null,
typeArguments,
new JS.Unary(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.leftPadded(this.suffix(node.expression), JS.Unary.Type.QuestionDotWithDot),
this.visit(node.expression),
this.mapType(node)
),
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
);
}

let select: JRightPadded<J.Expression> | null;
let name: J.Identifier;
if (ts.isPropertyAccessExpression(node.expression)) {
let name: J.Identifier = new J.Identifier( randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null);

if (ts.isIdentifier(node.expression) && !node.questionDotToken) {
select = null;
name = this.convert(node.expression);
} else if (ts.isPropertyAccessExpression(node.expression)) {
select = this.rightPadded(
node.expression.questionDotToken ?
new JS.Unary(
Expand All @@ -1577,8 +1548,20 @@ export class JavaScriptParserVisitor {
);
name = this.convert(node.expression.name);
} else {
select = null;
name = this.convert(node.expression);
if (node.questionDotToken) {
select = this.rightPadded(new JS.Unary(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.leftPadded(this.suffix(node.expression), JS.Unary.Type.QuestionDotWithDot),
this.visit(node.expression),
this.mapType(node)
),
Space.EMPTY
)
} else {
select = this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
}
}

return new J.MethodInvocation(
Expand Down Expand Up @@ -1657,6 +1640,8 @@ export class JavaScriptParserVisitor {
}

visitArrowFunction(node: ts.ArrowFunction) {
const openParenToken = this.findChildNode(node, ts.SyntaxKind.OpenParenToken);
const isParenthesized = openParenToken != undefined;
return new JS.ArrowFunction(
randomId(),
this.prefix(node),
Expand All @@ -1666,13 +1651,13 @@ export class JavaScriptParserVisitor {
node.typeParameters ? this.mapTypeParametersAsObject(node) : null,
new Lambda.Parameters(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
isParenthesized ? this.prefix(openParenToken) : Space.EMPTY,
Markers.EMPTY,
true,
isParenthesized,
node.parameters.length > 0 ?
node.parameters.map(p => this.rightPadded(this.convert(p), this.suffix(p)))
.concat(node.parameters.hasTrailingComma ? this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)) : []) :
[this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!))] // to handle the case: (/*no*/) => ...
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),
Expand Down Expand Up @@ -2428,7 +2413,7 @@ export class JavaScriptParserVisitor {
this.prefix(node),
Markers.EMPTY,
this.mapModifiers(node),
this.visit(node.name!),
node.name ? this.visit(node.name) : null,
this.mapTypeParametersAsObject(node),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
this.mapTypeInfo(node),
Expand Down
27 changes: 1 addition & 26 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} 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 @@ -358,18 +358,6 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return jSMethodDeclaration;
}

public visitJSMethodInvocation(jSMethodInvocation: JSMethodInvocation, ctx: ReceiverContext): J {
jSMethodInvocation = jSMethodInvocation.withId(ctx.receiveValue(jSMethodInvocation.id, ValueType.UUID)!);
jSMethodInvocation = jSMethodInvocation.withPrefix(ctx.receiveNode(jSMethodInvocation.prefix, receiveSpace)!);
jSMethodInvocation = jSMethodInvocation.withMarkers(ctx.receiveNode(jSMethodInvocation.markers, ctx.receiveMarkers)!);
jSMethodInvocation = jSMethodInvocation.padding.withSelect(ctx.receiveNode(jSMethodInvocation.padding.select, receiveRightPaddedTree));
jSMethodInvocation = jSMethodInvocation.padding.withTypeParameters(ctx.receiveNode(jSMethodInvocation.padding.typeParameters, receiveContainer));
jSMethodInvocation = jSMethodInvocation.withName(ctx.receiveNode(jSMethodInvocation.name, ctx.receiveTree)!);
jSMethodInvocation = jSMethodInvocation.padding.withArguments(ctx.receiveNode(jSMethodInvocation.padding.arguments, receiveContainer)!);
jSMethodInvocation = jSMethodInvocation.withMethodType(ctx.receiveValue(jSMethodInvocation.methodType, ValueType.Object));
return jSMethodInvocation;
}

public visitJSForOfLoop(jSForOfLoop: JSForOfLoop, ctx: ReceiverContext): J {
jSForOfLoop = jSForOfLoop.withId(ctx.receiveValue(jSForOfLoop.id, ValueType.UUID)!);
jSForOfLoop = jSForOfLoop.withPrefix(ctx.receiveNode(jSForOfLoop.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1496,19 +1484,6 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$JSMethodInvocation") {
return new JSMethodInvocation(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree),
ctx.receiveNode<JContainer<Expression>>(null, receiveContainer),
ctx.receiveNode<Expression>(null, ctx.receiveTree)!,
ctx.receiveNode<JContainer<Expression>>(null, receiveContainer)!,
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.javascript.tree.JS$JSForOfLoop") {
return new JSForOfLoop(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
14 changes: 1 addition & 13 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -353,18 +353,6 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return jSMethodDeclaration;
}

public visitJSMethodInvocation(jSMethodInvocation: JSMethodInvocation, ctx: SenderContext): J {
ctx.sendValue(jSMethodInvocation, v => v.id, ValueType.UUID);
ctx.sendNode(jSMethodInvocation, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jSMethodInvocation, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jSMethodInvocation, v => v.padding.select, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(jSMethodInvocation, v => v.padding.typeParameters, Visitor.sendContainer(ValueType.Tree));
ctx.sendNode(jSMethodInvocation, v => v.name, ctx.sendTree);
ctx.sendNode(jSMethodInvocation, v => v.padding.arguments, Visitor.sendContainer(ValueType.Tree));
ctx.sendTypedValue(jSMethodInvocation, v => v.methodType, ValueType.Object);
return jSMethodInvocation;
}

public visitJSForOfLoop(jSForOfLoop: JSForOfLoop, ctx: SenderContext): J {
ctx.sendValue(jSForOfLoop, v => v.id, ValueType.UUID);
ctx.sendNode(jSForOfLoop, v => v.prefix, Visitor.sendSpace);
Expand Down
4 changes: 0 additions & 4 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ export namespace JsSpace {
NAMESPACE_DECLARATION_PREFIX,
JSMETHOD_DECLARATION_PREFIX,
FUNCTION_DECLARATION_PREFIX,
JSMETHOD_INVOCATION_PREFIX,
INTERSECTION_PREFIX,
TYPE_LITERAL_PREFIX,
TYPE_LITERAL_MEMBERS_PREFIX,
Expand Down Expand Up @@ -276,7 +275,6 @@ export namespace JsRightPadded {
JSVARIABLE_DECLARATIONS_VARIABLES,
NAMESPACE_DECLARATION_NAME,
INTERSECTION_TYPES,
JSMETHOD_INVOCATION_SELECT,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
JSFOR_IN_OF_LOOP_CONTROL_VARIABLE,
Expand All @@ -296,8 +294,6 @@ export namespace JsContainer {
JSMETHOD_DECLARATION_PARAMETERS,
JSMETHOD_DECLARATION_THROWZ,
FUNCTION_DECLARATION_PARAMETERS,
JSMETHOD_INVOCATION_TYPE_PARAMETERS,
JSMETHOD_INVOCATION_ARGUMENTS,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
ARRAY_BINDING_PATTERN_ELEMENTS,
Expand Down
132 changes: 0 additions & 132 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2987,138 +2987,6 @@ export class JSMethodDeclaration extends JSMixin(Object) implements Statement, T

}

@LstType("org.openrewrite.javascript.tree.JS$JSMethodInvocation")
export class JSMethodInvocation extends JSMixin(Object) implements Statement, TypedTree, MethodCall {
public constructor(id: UUID, prefix: Space, markers: Markers, select: JRightPadded<Expression> | null, typeParameters: JContainer<Expression> | null, name: Expression, _arguments: JContainer<Expression>, methodType: JavaType.Method | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._select = select;
this._typeParameters = typeParameters;
this._name = name;
this._arguments = _arguments;
this._methodType = methodType;
}

private readonly _id: UUID;

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

public withId(id: UUID): JSMethodInvocation {
return id === this._id ? this : new JSMethodInvocation(id, this._prefix, this._markers, this._select, this._typeParameters, this._name, this._arguments, this._methodType);
}

private readonly _prefix: Space;

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

public withPrefix(prefix: Space): JSMethodInvocation {
return prefix === this._prefix ? this : new JSMethodInvocation(this._id, prefix, this._markers, this._select, this._typeParameters, this._name, this._arguments, this._methodType);
}

private readonly _markers: Markers;

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

public withMarkers(markers: Markers): JSMethodInvocation {
return markers === this._markers ? this : new JSMethodInvocation(this._id, this._prefix, markers, this._select, this._typeParameters, this._name, this._arguments, this._methodType);
}

private readonly _select: JRightPadded<Expression> | null;

public get select(): Expression | null {
return this._select === null ? null : this._select.element;
}

public withSelect(select: Expression | null): JSMethodInvocation {
return this.padding.withSelect(JRightPadded.withElement(this._select, select));
}

private readonly _typeParameters: JContainer<Expression> | null;

public get typeParameters(): Expression[] | null {
return this._typeParameters === null ? null : this._typeParameters.elements;
}

public withTypeParameters(typeParameters: Expression[] | null): JSMethodInvocation {
return this.padding.withTypeParameters(JContainer.withElementsNullable(this._typeParameters, typeParameters));
}

private readonly _name: Expression;

public get name(): Expression {
return this._name;
}

public withName(name: Expression): JSMethodInvocation {
return name === this._name ? this : new JSMethodInvocation(this._id, this._prefix, this._markers, this._select, this._typeParameters, name, this._arguments, this._methodType);
}

private readonly _arguments: JContainer<Expression>;

public get arguments(): Expression[] {
return this._arguments.elements;
}

public withArguments(_arguments: Expression[]): JSMethodInvocation {
return this.padding.withArguments(JContainer.withElements(this._arguments, _arguments));
}

private readonly _methodType: JavaType.Method | null;

public get methodType(): JavaType.Method | null {
return this._methodType;
}

public withMethodType(methodType: JavaType.Method | null): JSMethodInvocation {
return methodType === this._methodType ? this : new JSMethodInvocation(this._id, this._prefix, this._markers, this._select, this._typeParameters, this._name, this._arguments, methodType);
}

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

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

public withType(type: JavaType): JSMethodInvocation {
return extensions.withJavaType(this, type);
}

get padding() {
const t = this;
return new class {
public get select(): JRightPadded<Expression> | null {
return t._select;
}
public withSelect(select: JRightPadded<Expression> | null): JSMethodInvocation {
return t._select === select ? t : new JSMethodInvocation(t._id, t._prefix, t._markers, select, t._typeParameters, t._name, t._arguments, t._methodType);
}
public get typeParameters(): JContainer<Expression> | null {
return t._typeParameters;
}
public withTypeParameters(typeParameters: JContainer<Expression> | null): JSMethodInvocation {
return t._typeParameters === typeParameters ? t : new JSMethodInvocation(t._id, t._prefix, t._markers, t._select, typeParameters, t._name, t._arguments, t._methodType);
}
public get arguments(): JContainer<Expression> {
return t._arguments;
}
public withArguments(_arguments: JContainer<Expression>): JSMethodInvocation {
return t._arguments === _arguments ? t : new JSMethodInvocation(t._id, t._prefix, t._markers, t._select, t._typeParameters, t._name, _arguments, t._methodType);
}
}
}

}

@LstType("org.openrewrite.javascript.tree.JS$JSForOfLoop")
export class JSForOfLoop extends JSMixin(Object) implements Loop {
public constructor(id: UUID, prefix: Space, markers: Markers, await: JLeftPadded<boolean>, control: JSForInOfLoopControl, body: JRightPadded<Statement>) {
Expand Down
Loading