Skip to content

Commit

Permalink
call expression refactored for select usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Rodionov committed Nov 25, 2024
1 parent a761a70 commit 6d37c55
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 64 deletions.
91 changes: 30 additions & 61 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,34 +1548,32 @@ export class JavaScriptParserVisitor {
);
name = this.convert(node.expression.name);
} else {
select = null;
name = this.convert(node.expression);
}

if (name instanceof J.Identifier) {
return new J.MethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
select,
typeArguments,
name,
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
)
} else {
return new JS.JSMethodInvocation(
randomId(),
prefix,
Markers.EMPTY,
select,
typeArguments,
name,
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
)
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(
randomId(),
prefix,
Markers.EMPTY,
select,
typeArguments,
name,
this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(-3)),
this.mapMethodType(node)
)
}

visitNewExpression(node: ts.NewExpression) {
Expand Down
19 changes: 19 additions & 0 deletions openrewrite/test/javascript/parser/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('call mapping', () => {
typeScript(`
const func = (message: string) => message;
const result1 = func/*a*/?./*b*/("TS"); // Invokes the function
const result2 = func/*a*/?./*b*/call("TS"); // Invokes the function
`)
);
});
Expand Down Expand Up @@ -121,6 +123,23 @@ describe('call mapping', () => {
);
});

test('call expression with mapping and ?.', () => {
rewriteRun(
//language=typescript
typeScript(`
type Operation = (a: number, b: number) => number;
// Define an object with methods accessed by string keys
const operations: { [key: string]: Operation } = {
add: (a, b) => a + b,
multiply: (a, b) => a * b,
};
// Access and call the "add" method using bracket notation
const result1 = operations["add"]?.(3, 4); // 3 + 4 = 7
`)
);
});

test('call expression with mapping adv', () => {
rewriteRun(
Expand Down
13 changes: 12 additions & 1 deletion openrewrite/test/javascript/parser/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,18 @@ describe('function mapping', () => {
typeScript(`
(function() {
console.log('IIFE');
})();
})/*a*/();
`)
);
});

test('immediately invoked anonymous function with ?.', () => {
rewriteRun(
//language=typescript
typeScript(`
(function() {
console.log('IIFE');
})/*a*/?./*b*/();
`)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,12 @@ public J visitMethodDeclaration(J.MethodDeclaration method, PrintOutputCapture<P
@Override
public J visitMethodInvocation(J.MethodInvocation method, PrintOutputCapture<P> p) {
beforeSyntax(method, Space.Location.METHOD_INVOCATION_PREFIX, p);
visitRightPadded(method.getPadding().getSelect(), JRightPadded.Location.METHOD_SELECT, ".", p);
visit(method.getName(), p);
if (method.getName().toString().isEmpty()) {
visitRightPadded(method.getPadding().getSelect(), JRightPadded.Location.METHOD_SELECT, p);
} else {
visitRightPadded(method.getPadding().getSelect(), JRightPadded.Location.METHOD_SELECT, ".", p);
visit(method.getName(), p);
}
visitContainer("<", method.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, ",", ">", p);
visitContainer("(", method.getPadding().getArguments(), JContainer.Location.METHOD_INVOCATION_ARGUMENTS, ",", ")", p);
afterSyntax(method, p);
Expand Down

0 comments on commit 6d37c55

Please sign in to comment.