Skip to content

Commit

Permalink
add more bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Dec 4, 2024
1 parent 0caaba3 commit be79491
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 31 deletions.
38 changes: 19 additions & 19 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,12 @@ export class JavaScriptParserVisitor {
private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration
| ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration
| ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment) {
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression) {
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)
|| ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node)
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node)
|| ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node) || ts.isFunctionDeclaration(node)) {
|| ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node)
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
}
else if (ts.isExportAssignment(node)) {
Expand Down Expand Up @@ -1210,8 +1211,9 @@ export class JavaScriptParserVisitor {
this.prefix(node),
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
this.mapTypeParametersAsObject(node),
new JContainer(
this.prefix(node),
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p))),
Markers.EMPTY),
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
Expand All @@ -1225,8 +1227,9 @@ export class JavaScriptParserVisitor {
this.prefix(node),
Markers.EMPTY,
this.rightPadded(true, this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!)),
this.mapTypeParametersAsObject(node),
new JContainer(
this.prefix(node),
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p))),
Markers.EMPTY),
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
Expand Down Expand Up @@ -1826,7 +1829,7 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
[],
this.mapModifiers(node),
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!), !!node.asteriskToken),
this.leftPadded(node.asteriskToken ? this.prefix(node.asteriskToken) : Space.EMPTY, node.name ? this.visit(node.name) : new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null)),
this.mapTypeParametersAsObject(node),
Expand Down Expand Up @@ -3525,20 +3528,17 @@ export class JavaScriptParserVisitor {
}

private mapTypeParametersAsObject(node: ts.MethodDeclaration | ts.MethodSignature | ts.FunctionDeclaration
| ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration | ts.FunctionExpression | ts.ArrowFunction | ts.TypeAliasDeclaration) : J.TypeParameters | null {
if (!node.typeParameters) return null;

let ts_prefix: Space;
if (ts.isConstructSignatureDeclaration(node)) {
ts_prefix = this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!);
} else if (ts.isFunctionExpression(node)) {
ts_prefix = this.suffix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!);
} else if (ts.isTypeAliasDeclaration(node)) {
ts_prefix = this.suffix(node.name);
} else {
ts_prefix = node.questionToken ? this.suffix(node.questionToken) : node.name ? this.suffix(node.name) : Space.EMPTY;
}
return new J.TypeParameters(randomId(), ts_prefix, Markers.EMPTY, [], node.typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp))));
| ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration | ts.FunctionExpression | ts.ArrowFunction | ts.TypeAliasDeclaration | ts.FunctionTypeNode | ts.ConstructorTypeNode) : J.TypeParameters | null {
const typeParameters = node.typeParameters;
if (!typeParameters) return null;

return new J.TypeParameters(
randomId(),
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === typeParameters[0].pos) - 1)),
Markers.EMPTY,
[],
typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp)))
);
}

private mapTypeParametersList(typeParamsNodeArray: ts.NodeArray<ts.TypeParameterDeclaration>) : JRightPadded<J.TypeParameter>[] {
Expand Down
2 changes: 2 additions & 0 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
functionType = functionType.withPrefix(ctx.receiveNode(functionType.prefix, receiveSpace)!);
functionType = functionType.withMarkers(ctx.receiveNode(functionType.markers, ctx.receiveMarkers)!);
functionType = functionType.padding.withConstructorType(ctx.receiveNode(functionType.padding.constructorType, rightPaddedValueReceiver(ValueType.Primitive))!);
functionType = functionType.withTypeParameters(ctx.receiveNode(functionType.typeParameters, ctx.receiveTree));
functionType = functionType.padding.withParameters(ctx.receiveNode(functionType.padding.parameters, receiveContainer)!);
functionType = functionType.withArrow(ctx.receiveNode(functionType.arrow, receiveSpace)!);
functionType = functionType.withReturnType(ctx.receiveNode(functionType.returnType, ctx.receiveTree)!);
Expand Down Expand Up @@ -1382,6 +1383,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<boolean>>(null, rightPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<Java.TypeParameters>(null, ctx.receiveTree),
ctx.receiveNode<JContainer<Statement>>(null, receiveContainer)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode<Expression>(null, ctx.receiveTree)!,
Expand Down
1 change: 1 addition & 0 deletions openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendNode(functionType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(functionType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(functionType, v => v.padding.constructorType, Visitor.sendRightPadded(ValueType.Primitive));
ctx.sendNode(functionType, v => v.typeParameters, ctx.sendTree);
ctx.sendNode(functionType, v => v.padding.parameters, Visitor.sendContainer(ValueType.Tree));
ctx.sendNode(functionType, v => v.arrow, Visitor.sendSpace);
ctx.sendNode(functionType, v => v.returnType, ctx.sendTree);
Expand Down
29 changes: 20 additions & 9 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,13 @@ export class ExpressionWithTypeArguments extends JSMixin(Object) implements Type

@LstType("org.openrewrite.javascript.tree.JS$FunctionType")
export class FunctionType extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, constructorType: JRightPadded<boolean>, parameters: JContainer<Statement>, arrow: Space, returnType: Expression, _type: JavaType | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, constructorType: JRightPadded<boolean>, typeParameters: Java.TypeParameters | null, parameters: JContainer<Statement>, arrow: Space, returnType: Expression, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._constructorType = constructorType;
this._typeParameters = typeParameters;
this._parameters = parameters;
this._arrow = arrow;
this._returnType = returnType;
Expand All @@ -913,7 +914,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withId(id: UUID): FunctionType {
return id === this._id ? this : new FunctionType(id, this._prefix, this._markers, this._constructorType, this._parameters, this._arrow, this._returnType, this._type);
return id === this._id ? this : new FunctionType(id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
}

private readonly _prefix: Space;
Expand All @@ -923,7 +924,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withPrefix(prefix: Space): FunctionType {
return prefix === this._prefix ? this : new FunctionType(this._id, prefix, this._markers, this._constructorType, this._parameters, this._arrow, this._returnType, this._type);
return prefix === this._prefix ? this : new FunctionType(this._id, prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
}

private readonly _markers: Markers;
Expand All @@ -933,7 +934,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withMarkers(markers: Markers): FunctionType {
return markers === this._markers ? this : new FunctionType(this._id, this._prefix, markers, this._constructorType, this._parameters, this._arrow, this._returnType, this._type);
return markers === this._markers ? this : new FunctionType(this._id, this._prefix, markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
}

private readonly _constructorType: JRightPadded<boolean>;
Expand All @@ -946,6 +947,16 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
return this.padding.withConstructorType(this._constructorType.withElement(constructorType));
}

private readonly _typeParameters: Java.TypeParameters | null;

public get typeParameters(): Java.TypeParameters | null {
return this._typeParameters;
}

public withTypeParameters(typeParameters: Java.TypeParameters | null): FunctionType {
return typeParameters === this._typeParameters ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, typeParameters, this._parameters, this._arrow, this._returnType, this._type);
}

private readonly _parameters: JContainer<Statement>;

public get parameters(): Statement[] {
Expand All @@ -963,7 +974,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withArrow(arrow: Space): FunctionType {
return arrow === this._arrow ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._parameters, arrow, this._returnType, this._type);
return arrow === this._arrow ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, arrow, this._returnType, this._type);
}

private readonly _returnType: Expression;
Expand All @@ -973,7 +984,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withReturnType(returnType: Expression): FunctionType {
return returnType === this._returnType ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._parameters, this._arrow, returnType, this._type);
return returnType === this._returnType ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, returnType, this._type);
}

private readonly _type: JavaType | null;
Expand All @@ -983,7 +994,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withType(_type: JavaType | null): FunctionType {
return _type === this._type ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._parameters, this._arrow, this._returnType, _type);
return _type === this._type ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, _type);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
Expand All @@ -997,13 +1008,13 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
return t._constructorType;
}
public withConstructorType(constructorType: JRightPadded<boolean>): FunctionType {
return t._constructorType === constructorType ? t : new FunctionType(t._id, t._prefix, t._markers, constructorType, t._parameters, t._arrow, t._returnType, t._type);
return t._constructorType === constructorType ? t : new FunctionType(t._id, t._prefix, t._markers, constructorType, t._typeParameters, t._parameters, t._arrow, t._returnType, t._type);
}
public get parameters(): JContainer<Statement> {
return t._parameters;
}
public withParameters(parameters: JContainer<Statement>): FunctionType {
return t._parameters === parameters ? t : new FunctionType(t._id, t._prefix, t._markers, t._constructorType, parameters, t._arrow, t._returnType, t._type);
return t._parameters === parameters ? t : new FunctionType(t._id, t._prefix, t._markers, t._constructorType, t._typeParameters, parameters, t._arrow, t._returnType, t._type);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
functionType = tempExpression as FunctionType;
functionType = functionType.withMarkers(this.visitMarkers(functionType.markers, p));
functionType = functionType.padding.withConstructorType(this.visitJsRightPadded(functionType.padding.constructorType, JsRightPadded.Location.FUNCTION_TYPE_CONSTRUCTOR_TYPE, p)!);
functionType = functionType.withTypeParameters(this.visitAndCast(functionType.typeParameters, p));
functionType = functionType.padding.withParameters(this.visitJsContainer(functionType.padding.parameters, JsContainer.Location.FUNCTION_TYPE_PARAMETERS, p)!);
functionType = functionType.withArrow(this.visitJsSpace(functionType.arrow, JsSpace.Location.FUNCTION_TYPE_ARROW, p)!);
functionType = functionType.withReturnType(this.visitAndCast(functionType.returnType, p)!);
Expand Down
Loading

0 comments on commit be79491

Please sign in to comment.