Skip to content

Commit

Permalink
Fixed missed optional tokens in yield and method declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Rodionov committed Dec 12, 2024
1 parent 711bc16 commit d62a55d
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 58 deletions.
21 changes: 17 additions & 4 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,20 @@ export class JavaScriptParserVisitor {
}

visitMethodDeclaration(node: ts.MethodDeclaration) {
if (node.questionToken) {
if (node.questionToken || node.asteriskToken) {
let methodName = node.questionToken ? this.getOptionalUnary(node) : this.visit(node.name);

if (node.asteriskToken) {
methodName = new JS.Unary(
randomId(),
this.prefix(node.asteriskToken),
Markers.EMPTY,
this.leftPadded(this.prefix(node.name), JS.Unary.Type.Asterisk),
methodName,
this.mapType(node)
)
}

return new JS.JSMethodDeclaration(
randomId(),
this.prefix(node),
Expand All @@ -1046,7 +1059,7 @@ export class JavaScriptParserVisitor {
this.mapModifiers(node),
this.mapTypeParametersAsObject(node),
this.mapTypeInfo(node),
this.getOptionalUnary(node),
methodName,
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
Expand Down Expand Up @@ -2335,7 +2348,7 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
false,
node.asteriskToken ? this.leftPadded(this.prefix(node.asteriskToken), true) : this.leftPadded(Space.EMPTY, false),
node.expression ? this.visit(node.expression) : null,
this.mapType(node)
);
Expand Down Expand Up @@ -2809,7 +2822,7 @@ export class JavaScriptParserVisitor {
this.mapTypeParametersAsObject(node),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
this.mapTypeInfo(node),
this.convert(node.body!),
node.body ? this.convert(node.body) : null,
this.mapMethodType(node)
);
}
Expand Down
8 changes: 4 additions & 4 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
_yield = _yield.withId(ctx.receiveValue(_yield.id, ValueType.UUID)!);
_yield = _yield.withPrefix(ctx.receiveNode(_yield.prefix, receiveSpace)!);
_yield = _yield.withMarkers(ctx.receiveNode(_yield.markers, ctx.receiveMarkers)!);
_yield = _yield.withDelegated(ctx.receiveValue(_yield.delegated, ValueType.Primitive)!);
_yield = _yield.padding.withDelegated(ctx.receiveNode(_yield.padding.delegated, leftPaddedValueReceiver(ValueType.Primitive))!);
_yield = _yield.withExpression(ctx.receiveNode(_yield.expression, ctx.receiveTree));
_yield = _yield.withType(ctx.receiveValue(_yield.type, ValueType.Object));
return _yield;
Expand Down Expand Up @@ -519,7 +519,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
functionDeclaration = functionDeclaration.withTypeParameters(ctx.receiveNode(functionDeclaration.typeParameters, ctx.receiveTree));
functionDeclaration = functionDeclaration.padding.withParameters(ctx.receiveNode(functionDeclaration.padding.parameters, receiveContainer)!);
functionDeclaration = functionDeclaration.withReturnTypeExpression(ctx.receiveNode(functionDeclaration.returnTypeExpression, ctx.receiveTree));
functionDeclaration = functionDeclaration.withBody(ctx.receiveNode(functionDeclaration.body, ctx.receiveTree)!);
functionDeclaration = functionDeclaration.withBody(ctx.receiveNode(functionDeclaration.body, ctx.receiveTree));
functionDeclaration = functionDeclaration.withType(ctx.receiveValue(functionDeclaration.type, ValueType.Object));
return functionDeclaration;
}
Expand Down Expand Up @@ -1740,7 +1740,7 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveValue(null, ValueType.Primitive)!,
ctx.receiveNode<JLeftPadded<boolean>>(null, leftPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<Expression>(null, ctx.receiveTree),
ctx.receiveValue(null, ValueType.Object)
);
Expand Down Expand Up @@ -1852,7 +1852,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode<Java.TypeParameters>(null, ctx.receiveTree),
ctx.receiveNode<JContainer<Statement>>(null, receiveContainer)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree),
ctx.receiveNode<J>(null, ctx.receiveTree)!,
ctx.receiveNode<J>(null, ctx.receiveTree),
ctx.receiveValue(null, ValueType.Object)
);
}
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendValue(_yield, v => v.id, ValueType.UUID);
ctx.sendNode(_yield, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(_yield, v => v.markers, ctx.sendMarkers);
ctx.sendValue(_yield, v => v.delegated, ValueType.Primitive);
ctx.sendNode(_yield, v => v.padding.delegated, Visitor.sendLeftPadded(ValueType.Primitive));
ctx.sendNode(_yield, v => v.expression, ctx.sendTree);
ctx.sendTypedValue(_yield, v => v.type, ValueType.Object);
return _yield;
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 @@ -303,6 +303,7 @@ export namespace JsLeftPadded {
MAPPED_TYPE_HAS_READONLY,
MAPPED_TYPE_HAS_QUESTION_TOKEN,
ARROW_FUNCTION_BODY,
YIELD_DELEGATED,
}
}
export namespace JsRightPadded {
Expand Down
29 changes: 21 additions & 8 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3270,6 +3270,7 @@ export namespace Unary {
Exclamation = 2,
QuestionDot = 3,
QuestionDotWithDot = 4,
Asterisk = 5,

}

Expand Down Expand Up @@ -3499,7 +3500,7 @@ export class Void extends JSMixin(Object) implements Expression, Statement {

@LstType("org.openrewrite.javascript.tree.JS$Yield")
export class Yield extends JSMixin(Object) implements Expression {
public constructor(id: UUID, prefix: Space, markers: Markers, delegated: boolean, expression: Expression | null, _type: JavaType | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, delegated: JLeftPadded<boolean>, expression: Expression | null, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -3539,14 +3540,14 @@ export class Yield extends JSMixin(Object) implements Expression {
return markers === this._markers ? this : new Yield(this._id, this._prefix, markers, this._delegated, this._expression, this._type);
}

private readonly _delegated: boolean;
private readonly _delegated: JLeftPadded<boolean>;

public get delegated(): boolean {
return this._delegated;
return this._delegated.element;
}

public withDelegated(delegated: boolean): Yield {
return delegated === this._delegated ? this : new Yield(this._id, this._prefix, this._markers, delegated, this._expression, this._type);
return this.padding.withDelegated(this._delegated.withElement(delegated));
}

private readonly _expression: Expression | null;
Expand All @@ -3573,6 +3574,18 @@ export class Yield extends JSMixin(Object) implements Expression {
return v.visitJsYield(this, p);
}

get padding() {
const t = this;
return new class {
public get delegated(): JLeftPadded<boolean> {
return t._delegated;
}
public withDelegated(delegated: JLeftPadded<boolean>): Yield {
return t._delegated === delegated ? t : new Yield(t._id, t._prefix, t._markers, delegated, t._expression, t._type);
}
}
}

}

@LstType("org.openrewrite.javascript.tree.JS$TypeInfo")
Expand Down Expand Up @@ -4442,7 +4455,7 @@ export namespace NamespaceDeclaration {

@LstType("org.openrewrite.javascript.tree.JS$FunctionDeclaration")
export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], asteriskToken: JLeftPadded<boolean>, name: JLeftPadded<Java.Identifier>, typeParameters: Java.TypeParameters | null, parameters: JContainer<Statement>, returnTypeExpression: TypeTree | null, body: J, _type: JavaType | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], asteriskToken: JLeftPadded<boolean>, name: JLeftPadded<Java.Identifier>, typeParameters: Java.TypeParameters | null, parameters: JContainer<Statement>, returnTypeExpression: TypeTree | null, body: J | null, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -4547,13 +4560,13 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E
return returnTypeExpression === this._returnTypeExpression ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, returnTypeExpression, this._body, this._type);
}

private readonly _body: J;
private readonly _body: J | null;

public get body(): J {
public get body(): J | null {
return this._body;
}

public withBody(body: J): FunctionDeclaration {
public withBody(body: J | null): FunctionDeclaration {
return body === this._body ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, body, this._type);
}

Expand Down
3 changes: 2 additions & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
}
_yield = tempExpression as Yield;
_yield = _yield.withMarkers(this.visitMarkers(_yield.markers, p));
_yield = _yield.padding.withDelegated(this.visitJsLeftPadded(_yield.padding.delegated, JsLeftPadded.Location.YIELD_DELEGATED, p)!);
_yield = _yield.withExpression(this.visitAndCast(_yield.expression, p));
return _yield;
}
Expand Down Expand Up @@ -715,7 +716,7 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
functionDeclaration = functionDeclaration.withTypeParameters(this.visitAndCast(functionDeclaration.typeParameters, p));
functionDeclaration = functionDeclaration.padding.withParameters(this.visitJsContainer(functionDeclaration.padding.parameters, JsContainer.Location.FUNCTION_DECLARATION_PARAMETERS, p)!);
functionDeclaration = functionDeclaration.withReturnTypeExpression(this.visitAndCast(functionDeclaration.returnTypeExpression, p));
functionDeclaration = functionDeclaration.withBody(this.visitAndCast(functionDeclaration.body, p)!);
functionDeclaration = functionDeclaration.withBody(this.visitAndCast(functionDeclaration.body, p));
return functionDeclaration;
}

Expand Down
13 changes: 12 additions & 1 deletion openrewrite/test/javascript/parser/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ describe('class mapping', () => {
);
});


test('new class with type arguments', () => {
rewriteRun(
//language=typescript
Expand All @@ -490,4 +489,16 @@ describe('class mapping', () => {
);
});

test('static method with asterisk', () => {
rewriteRun(
//language=typescript
typeScript(`
class A {
public static/*a*/*/*b*/getMarkdownRestSnippets?(document: TextDocument): Generator<Range> {
}
}
`)
);
});

});
9 changes: 9 additions & 0 deletions openrewrite/test/javascript/parser/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,13 @@ describe('function mapping', () => {
`)
);
});

test('empty body function', () => {
rewriteRun(
//language=typescript
typeScript(`
export function getHeader(headers: ResponseHeaders, name: string): ResponseHeaderValue;
`)
);
});
});
20 changes: 20 additions & 0 deletions openrewrite/test/javascript/parser/yield.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ describe('yield mapping', () => {
typeScript('yield* other')
);
});

test('yield expression', () => {
rewriteRun(
//language=typescript
typeScript(`
DenseMatrix.prototype[Symbol.iterator] = function* () {
const recurse = function* (value, index) {
if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
yield* recurse(value[i], index.concat(i))
}
} else {
yield ({value, index})
}
}
yield/*a*/* /*b*/recurse(this._data, [])
}
`)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public JS.Yield visitYield(JS.Yield yield, ReceiverContext ctx) {
yield = yield.withId(ctx.receiveNonNullValue(yield.getId(), UUID.class));
yield = yield.withPrefix(ctx.receiveNonNullNode(yield.getPrefix(), JavaScriptReceiver::receiveSpace));
yield = yield.withMarkers(ctx.receiveNonNullNode(yield.getMarkers(), ctx::receiveMarkers));
yield = yield.withDelegated(ctx.receiveNonNullValue(yield.isDelegated(), boolean.class));
yield = yield.getPadding().withDelegated(ctx.receiveNonNullNode(yield.getPadding().getDelegated(), leftPaddedValueReceiver(java.lang.Boolean.class)));
yield = yield.withExpression(ctx.receiveNode(yield.getExpression(), ctx::receiveTree));
yield = yield.withType(ctx.receiveValue(yield.getType(), JavaType.class));
return yield;
Expand Down Expand Up @@ -614,7 +614,7 @@ public JS.FunctionDeclaration visitFunctionDeclaration(JS.FunctionDeclaration fu
functionDeclaration = functionDeclaration.withTypeParameters(ctx.receiveNode(functionDeclaration.getTypeParameters(), ctx::receiveTree));
functionDeclaration = functionDeclaration.getPadding().withParameters(ctx.receiveNonNullNode(functionDeclaration.getPadding().getParameters(), JavaScriptReceiver::receiveContainer));
functionDeclaration = functionDeclaration.withReturnTypeExpression(ctx.receiveNode(functionDeclaration.getReturnTypeExpression(), ctx::receiveTree));
functionDeclaration = functionDeclaration.withBody(ctx.receiveNonNullNode(functionDeclaration.getBody(), ctx::receiveTree));
functionDeclaration = functionDeclaration.withBody(ctx.receiveNode(functionDeclaration.getBody(), ctx::receiveTree));
functionDeclaration = functionDeclaration.withType(ctx.receiveValue(functionDeclaration.getType(), JavaType.class));
return functionDeclaration;
}
Expand Down Expand Up @@ -2015,7 +2015,7 @@ private static JS.Yield createJSYield(ReceiverContext ctx) {
ctx.receiveNonNullValue(null, UUID.class),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullValue(null, boolean.class),
ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveValue(null, JavaType.class)
);
Expand Down Expand Up @@ -2127,7 +2127,7 @@ private static JS.FunctionDeclaration createJSFunctionDeclaration(ReceiverContex
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, ctx::receiveTree),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveValue(null, JavaType.class)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public JS.Yield visitYield(JS.Yield yield, SenderContext ctx) {
ctx.sendValue(yield, JS.Yield::getId);
ctx.sendNode(yield, JS.Yield::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(yield, JS.Yield::getMarkers, ctx::sendMarkers);
ctx.sendValue(yield, JS.Yield::isDelegated);
ctx.sendNode(yield, e -> e.getPadding().getDelegated(), JavaScriptSender::sendLeftPadded);
ctx.sendNode(yield, JS.Yield::getExpression, ctx::sendTree);
ctx.sendTypedValue(yield, JS.Yield::getType);
return yield;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public JS.FunctionDeclaration visitFunctionDeclaration(JS.FunctionDeclaration fu
visitAndValidate(functionDeclaration.getTypeParameters(), J.TypeParameters.class, p);
visitAndValidate(functionDeclaration.getParameters(), Statement.class, p);
visitAndValidate(functionDeclaration.getReturnTypeExpression(), TypeTree.class, p);
visitAndValidateNonNull(functionDeclaration.getBody(), J.class, p);
visitAndValidate(functionDeclaration.getBody(), J.class, p);
return functionDeclaration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ public J visitYield(JS.Yield yield, P p) {
} else {
y = (JS.Yield) temp;
}
y = y.getPadding().withDelegated(this.visitLeftPadded(y.getPadding().getDelegated(), JsLeftPadded.Location.JS_YIELD_DELEGATED, p));
y = y.withExpression(visitAndCast(y.getExpression(), p));
return y;
}
Expand Down Expand Up @@ -981,7 +982,7 @@ public J visitFunctionDeclaration(JS.FunctionDeclaration functionDeclaration, P
f = f.withTypeParameters(visitAndCast(f.getTypeParameters(), p));
f = f.getPadding().withParameters(Objects.requireNonNull(visitContainer(f.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, p)));
f = f.withReturnTypeExpression(visitAndCast(f.getReturnTypeExpression(), p));
f = f.withBody(Objects.requireNonNull(visitAndCast(f.getBody(), p)));
f = f.withBody(visitAndCast(f.getBody(), p));
f = f.withType(visitType(f.getType(), p));
return f;
}
Expand Down
Loading

0 comments on commit d62a55d

Please sign in to comment.