Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.
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
6 changes: 2 additions & 4 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1408,8 +1408,7 @@ export class JavaScriptParserVisitor {
: node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p)))
.concat(node.parameters.hasTrailingComma ? this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)) : []),
Markers.EMPTY),
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
this.convert(node.type),
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!), this.convert(node.type)),
null);
}

Expand All @@ -1428,8 +1427,7 @@ export class JavaScriptParserVisitor {
: node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p)))
.concat(node.parameters.hasTrailingComma ? this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)) : []),
Markers.EMPTY),
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
this.convert(node.type),
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!), this.convert(node.type)),
null);
}

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 @@ -148,8 +148,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
functionType = functionType.padding.withConstructorType(ctx.receiveNode(functionType.padding.constructorType, leftPaddedValueReceiver(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)!);
functionType = functionType.padding.withReturnType(ctx.receiveNode(functionType.padding.returnType, receiveLeftPaddedTree)!);
functionType = functionType.withType(ctx.receiveValue(functionType.type, ValueType.Object));
return functionType;
}
Expand Down Expand Up @@ -1551,8 +1550,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode<JLeftPadded<boolean>>(null, leftPaddedValueReceiver(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)!,
ctx.receiveNode<JLeftPadded<Expression>>(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 @@ -143,8 +143,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendNode(functionType, v => v.padding.constructorType, Visitor.sendLeftPadded(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);
ctx.sendNode(functionType, v => v.padding.returnType, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendTypedValue(functionType, v => v.type, ValueType.Object);
return functionType;
}
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 @@ -315,6 +315,7 @@ export namespace JsLeftPadded {
ARROW_FUNCTION_BODY,
YIELD_DELEGATED,
FUNCTION_TYPE_CONSTRUCTOR_TYPE,
FUNCTION_TYPE_RETURN_TYPE,
JSTRY_FINALLIE,
IMPORT_ATTRIBUTE_VALUE,
JS_IMPORT_MODULE_SPECIFIER,
Expand Down
41 changes: 18 additions & 23 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ 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, modifiers: Java.Modifier[], constructorType: JLeftPadded<boolean>, typeParameters: Java.TypeParameters | null, parameters: JContainer<Statement>, arrow: Space, returnType: Expression, _type: JavaType | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], constructorType: JLeftPadded<boolean>, typeParameters: Java.TypeParameters | null, parameters: JContainer<Statement>, returnType: JLeftPadded<Expression>, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
Expand All @@ -983,7 +983,6 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
this._constructorType = constructorType;
this._typeParameters = typeParameters;
this._parameters = parameters;
this._arrow = arrow;
this._returnType = returnType;
this._type = _type;
}
Expand All @@ -995,7 +994,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._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
return id === this._id ? this : new FunctionType(id, this._prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._returnType, this._type);
}

private readonly _prefix: Space;
Expand All @@ -1005,7 +1004,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._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
return prefix === this._prefix ? this : new FunctionType(this._id, prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._returnType, this._type);
}

private readonly _markers: Markers;
Expand All @@ -1015,7 +1014,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._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
return markers === this._markers ? this : new FunctionType(this._id, this._prefix, markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._returnType, this._type);
}

private readonly _modifiers: Java.Modifier[];
Expand All @@ -1025,7 +1024,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

public withModifiers(modifiers: Java.Modifier[]): FunctionType {
return modifiers === this._modifiers ? this : new FunctionType(this._id, this._prefix, this._markers, modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type);
return modifiers === this._modifiers ? this : new FunctionType(this._id, this._prefix, this._markers, modifiers, this._constructorType, this._typeParameters, this._parameters, this._returnType, this._type);
}

private readonly _constructorType: JLeftPadded<boolean>;
Expand All @@ -1045,7 +1044,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
}

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

private readonly _parameters: JContainer<Statement>;
Expand All @@ -1058,24 +1057,14 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
return this.padding.withParameters(JContainer.withElements(this._parameters, parameters));
}

private readonly _arrow: Space;

public get arrow(): Space {
return this._arrow;
}

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

private readonly _returnType: Expression;
private readonly _returnType: JLeftPadded<Expression>;

public get returnType(): Expression {
return this._returnType;
return this._returnType.element;
}

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

private readonly _type: JavaType | null;
Expand All @@ -1085,7 +1074,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._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, _type);
return _type === this._type ? this : new FunctionType(this._id, this._prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._returnType, _type);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
Expand All @@ -1099,13 +1088,19 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre
return t._constructorType;
}
public withConstructorType(constructorType: JLeftPadded<boolean>): FunctionType {
return t._constructorType === constructorType ? t : new FunctionType(t._id, t._prefix, t._markers, t._modifiers, constructorType, t._typeParameters, t._parameters, t._arrow, t._returnType, t._type);
return t._constructorType === constructorType ? t : new FunctionType(t._id, t._prefix, t._markers, t._modifiers, constructorType, t._typeParameters, t._parameters, 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._modifiers, t._constructorType, t._typeParameters, parameters, t._arrow, t._returnType, t._type);
return t._parameters === parameters ? t : new FunctionType(t._id, t._prefix, t._markers, t._modifiers, t._constructorType, t._typeParameters, parameters, t._returnType, t._type);
}
public get returnType(): JLeftPadded<Expression> {
return t._returnType;
}
public withReturnType(returnType: JLeftPadded<Expression>): FunctionType {
return t._returnType === returnType ? t : new FunctionType(t._id, t._prefix, t._markers, t._modifiers, t._constructorType, t._typeParameters, t._parameters, returnType, t._type);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
functionType = functionType.padding.withConstructorType(this.visitJsLeftPadded(functionType.padding.constructorType, JsLeftPadded.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)!);
functionType = functionType.padding.withReturnType(this.visitJsLeftPadded(functionType.padding.returnType, JsLeftPadded.Location.FUNCTION_TYPE_RETURN_TYPE, p)!);
return functionType;
}

Expand Down
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/parser/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('function mapping', () => {
);
});

test('parameter with trailing comma', () => {
test('function with type params', () => {
rewriteRun(
//language=typescript
typeScript(`
Expand Down
12 changes: 12 additions & 0 deletions openrewrite/test/javascript/parser/variableDeclarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ describe('variable declaration mapping', () => {
`)
);
});

test.skip('variable declaration with decorator', () => {
rewriteRun(
//language=typescript
typeScript(`
export namespace process {
// @ts-ignore: decorator
@lazy export const platform = "wasm";
}
`)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ public JS.FunctionType visitFunctionType(JS.FunctionType functionType, ReceiverC
functionType = functionType.getPadding().withConstructorType(ctx.receiveNonNullNode(functionType.getPadding().getConstructorType(), leftPaddedValueReceiver(java.lang.Boolean.class)));
functionType = functionType.withTypeParameters(ctx.receiveNode(functionType.getTypeParameters(), ctx::receiveTree));
functionType = functionType.getPadding().withParameters(ctx.receiveNonNullNode(functionType.getPadding().getParameters(), JavaScriptReceiver::receiveContainer));
functionType = functionType.withArrow(ctx.receiveNonNullNode(functionType.getArrow(), JavaScriptReceiver::receiveSpace));
functionType = functionType.withReturnType(ctx.receiveNonNullNode(functionType.getReturnType(), ctx::receiveTree));
functionType = functionType.getPadding().withReturnType(ctx.receiveNonNullNode(functionType.getPadding().getReturnType(), JavaScriptReceiver::receiveLeftPaddedTree));
functionType = functionType.withType(ctx.receiveValue(functionType.getType(), JavaType.class));
return functionType;
}
Expand Down Expand Up @@ -1859,8 +1858,7 @@ private static JS.FunctionType createJSFunctionType(ReceiverContext ctx) {
ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree),
ctx.receiveValue(null, JavaType.class)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ public JS.FunctionType visitFunctionType(JS.FunctionType functionType, SenderCon
ctx.sendNode(functionType, e -> e.getPadding().getConstructorType(), JavaScriptSender::sendLeftPadded);
ctx.sendNode(functionType, JS.FunctionType::getTypeParameters, ctx::sendTree);
ctx.sendNode(functionType, e -> e.getPadding().getParameters(), JavaScriptSender::sendContainer);
ctx.sendNode(functionType, JS.FunctionType::getArrow, JavaScriptSender::sendSpace);
ctx.sendNode(functionType, JS.FunctionType::getReturnType, ctx::sendTree);
ctx.sendNode(functionType, e -> e.getPadding().getReturnType(), JavaScriptSender::sendLeftPadded);
ctx.sendTypedValue(functionType, JS.FunctionType::getType);
return functionType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ public J visitFunctionType(JS.FunctionType functionType, P p) {
f = f.withModifiers(Objects.requireNonNull(ListUtils.map(f.getModifiers(), e -> visitAndCast(e, p))));
f = f.getPadding().withConstructorType(Objects.requireNonNull(visitLeftPadded(f.getPadding().getConstructorType(), JsLeftPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, p)));
f = f.withTypeParameters(visitAndCast(f.getTypeParameters(), p));
f = f.getPadding().withParameters(Objects.requireNonNull(visitContainer(f.getPadding().getParameters(), JContainer.Location.LANGUAGE_EXTENSION, p)));
f = f.getPadding().withParameters(Objects.requireNonNull(visitContainer(f.getPadding().getParameters(), JsContainer.Location.FUNCTION_TYPE_PARAMETERS, p)));
f = f.withParameters(Objects.requireNonNull(ListUtils.map(f.getParameters(), e -> visitAndCast(e, p))));
f = f.withArrow(visitSpace(f.getArrow(), JsSpace.Location.FUNCTION_TYPE_ARROW_PREFIX, p));
f = f.withReturnType(Objects.requireNonNull(visitAndCast(f.getReturnType(), p)));
f = f.getPadding().withReturnType((Objects.requireNonNull(visitLeftPadded(f.getPadding().getReturnType(), JsLeftPadded.Location.FUNCTION_TYPE_RETURN_TYPE, p))));
f = f.withType(visitType(f.getType(), p));
return f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public J visitExport(JS.Export export, PrintOutputCapture<P> p) {
p.append("export");

boolean printBrackets = export.getPadding().getExports() != null && export.getPadding().getExports().getMarkers().findFirst(Braces.class).isPresent();
visitContainer(printBrackets ? "{" : "", export.getPadding().getExports(), JsContainer.Location.FUNCTION_TYPE_PARAMETER, ",", printBrackets ? "}" : "", p);
visitContainer(printBrackets ? "{" : "", export.getPadding().getExports(), JsContainer.Location.FUNCTION_TYPE_PARAMETERS, ",", printBrackets ? "}" : "", p);

if (export.getFrom() != null) {
visitSpace(export.getFrom(), Space.Location.LANGUAGE_EXTENSION, p);
Expand Down Expand Up @@ -220,10 +220,9 @@ public J visitFunctionType(JS.FunctionType functionType, PrintOutputCapture<P> p
visitRightPadded(typeParameters.getPadding().getTypeParameters(), JRightPadded.Location.TYPE_PARAMETER, ",", p);
p.append(">");
}
visitContainer("(", functionType.getPadding().getParameters(), JsContainer.Location.FUNCTION_TYPE_PARAMETER, ",", ")", p);
visitSpace(functionType.getArrow(), JsSpace.Location.FUNCTION_TYPE_ARROW_PREFIX, p);
p.append("=>");
visit(functionType.getReturnType(), p);
visitContainer("(", functionType.getPadding().getParameters(), JsContainer.Location.FUNCTION_TYPE_PARAMETERS, ",", ")", p);
visitLeftPadded("=>", functionType.getPadding().getReturnType(), JsLeftPadded.Location.FUNCTION_TYPE_RETURN_TYPE, p);

afterSyntax(functionType, p);
return functionType;
}
Expand Down
Loading