Skip to content

Commit

Permalink
Merge pull request #76 from openrewrite/polish
Browse files Browse the repository at this point in the history
Replaced uses of stream() with for loops.
  • Loading branch information
traceyyoshima authored Nov 15, 2023
2 parents 16aceb3 + c58f6ae commit e8375f0
Showing 1 changed file with 74 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.tree.Space.EMPTY;

Expand Down Expand Up @@ -244,13 +243,13 @@ private J.NewArray visitArrayLiteralExpression(TSCNode node) {
this::visitNode,
true
);
List<JRightPadded<Expression>> elements = jContainer.getPadding().getElements().stream()
.map(it -> {
Expression exp = (!(it.getElement() instanceof Expression) && it.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) it.getElement()) : (Expression) it.getElement();
return padRight(exp, it.getAfter(), it.getMarkers());
})
.collect(toList());
List<JRightPadded<Expression>> elements = new ArrayList<>();
for (JRightPadded<J> jjRightPadded : jContainer.getPadding().getElements()) {
Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();
JRightPadded<Expression> apply = padRight(exp, jjRightPadded.getAfter(), jjRightPadded.getMarkers());
elements.add(apply);
}
JContainer<Expression> arguments = JContainer.build(jContainer.getBefore(), elements, jContainer.getMarkers());

return new J.NewArray(
Expand Down Expand Up @@ -555,13 +554,13 @@ private J.MethodInvocation visitCallExpression(TSCNode node) {
true
);

List<JRightPadded<Expression>> typeParams = jContainer.getPadding().getElements().stream()
.map(it -> {
Expression exp = (!(it.getElement() instanceof Expression) && it.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) it.getElement()) : (Expression) it.getElement();
return padRight(exp, it.getAfter(), it.getMarkers());
})
.collect(toList());
List<JRightPadded<Expression>> typeParams = new ArrayList<>();
for (JRightPadded<J> jjRightPadded : jContainer.getPadding().getElements()) {
Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();
JRightPadded<Expression> apply = padRight(exp, jjRightPadded.getAfter(), jjRightPadded.getMarkers());
typeParams.add(apply);
}
typeParameters = JContainer.build(jContainer.getBefore(), typeParams, jContainer.getMarkers());
}

Expand All @@ -575,13 +574,13 @@ private J.MethodInvocation visitCallExpression(TSCNode node) {
this::visitNode,
true
);
List<JRightPadded<Expression>> elements = jContainer.getPadding().getElements().stream()
.map(it -> {
Expression exp = (!(it.getElement() instanceof Expression) && it.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) it.getElement()) : (Expression) it.getElement();
return padRight(exp, it.getAfter(), it.getMarkers());
})
.collect(toList());
List<JRightPadded<Expression>> elements = new ArrayList<>();
for (JRightPadded<J> jjRightPadded : jContainer.getPadding().getElements()) {
Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();
JRightPadded<Expression> apply = padRight(exp, jjRightPadded.getAfter(), jjRightPadded.getMarkers());
elements.add(apply);
}
arguments = JContainer.build(jContainer.getBefore(), elements, jContainer.getMarkers());
}

Expand Down Expand Up @@ -734,22 +733,28 @@ private J.ClassDeclaration visitClassDeclaration(TSCNode node) {
private J visitCaseClause(TSCNode node) {
TSCNode expression = node.getOptionalNodeProperty("expression");
List<TSCNode> statements = node.getNodeListProperty("statements");
Space prefix = whitespace();
JContainer<Expression> expressions = JContainer.build(
expression == null ? EMPTY : sourceBefore(TSCSyntaxKind.CaseKeyword),
singletonList(JRightPadded.build((Expression) visitNode(expression))),
Markers.EMPTY
);
List<JRightPadded<Statement>> list = new ArrayList<>(statements.size());
Space before = sourceBefore(TSCSyntaxKind.ColonToken);
for (TSCNode it : statements) {
JRightPadded<Statement> statementJRightPadded = maybeSemicolon((Statement) visitNode(it));
list.add(statementJRightPadded);
}
return new J.Case(
randomId(),
whitespace(),
prefix,
Markers.EMPTY,
J.Case.Type.Statement,
null,
expressions,
JContainer.build(
expression == null ? EMPTY : sourceBefore(TSCSyntaxKind.CaseKeyword),
singletonList(JRightPadded.build((Expression) visitNode(expression))),
Markers.EMPTY
),
JContainer.build(
sourceBefore(TSCSyntaxKind.ColonToken),
statements.stream()
.map(it -> maybeSemicolon((Statement) visitNode(it)))
.collect(toList()),
before,
list,
Markers.EMPTY
),
null
Expand Down Expand Up @@ -837,13 +842,13 @@ private J.Annotation visitDecorator(TSCNode node) {
true
);

List<JRightPadded<Expression>> elements = jContainer.getPadding().getElements().stream()
.map(it -> {
Expression exp = (!(it.getElement() instanceof Expression) && it.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) it.getElement()) : (Expression) it.getElement();
return padRight(exp, it.getAfter(), it.getMarkers());
})
.collect(toList());
List<JRightPadded<Expression>> elements = new ArrayList<>();
for (JRightPadded<J> jjRightPadded : jContainer.getPadding().getElements()) {
Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();
JRightPadded<Expression> apply = padRight(exp, jjRightPadded.getAfter(), jjRightPadded.getMarkers());
elements.add(apply);
}
arguments = JContainer.build(jContainer.getBefore(), elements, jContainer.getMarkers());
}
return new J.Annotation(
Expand All @@ -857,22 +862,28 @@ private J.Annotation visitDecorator(TSCNode node) {

private J visitDefaultClause(TSCNode node) {
List<TSCNode> statements = node.getNodeListProperty("statements");
Space prefix = whitespace();
JContainer<Expression> expressions = JContainer.build(
EMPTY,
singletonList(JRightPadded.build(new J.Identifier(randomId(), sourceBefore(TSCSyntaxKind.DefaultKeyword), Markers.EMPTY, emptyList(), "default", null, null))),
Markers.EMPTY
);
Space before = sourceBefore(TSCSyntaxKind.ColonToken);
List<JRightPadded<Statement>> list = new ArrayList<>(statements.size());
for (TSCNode it : statements) {
JRightPadded<Statement> statementJRightPadded = maybeSemicolon((Statement) visitNode(it));
list.add(statementJRightPadded);
}
return new J.Case(
randomId(),
whitespace(),
prefix,
Markers.EMPTY,
J.Case.Type.Statement,
null,
expressions,
JContainer.build(
EMPTY,
singletonList(JRightPadded.build(new J.Identifier(randomId(), sourceBefore(TSCSyntaxKind.DefaultKeyword), Markers.EMPTY, emptyList(), "default", null, null))),
Markers.EMPTY
),
JContainer.build(
sourceBefore(TSCSyntaxKind.ColonToken),
statements.stream()
.map(it -> maybeSemicolon((Statement) visitNode(it)))
.collect(toList()),
before,
list,
Markers.EMPTY
),
null
Expand Down Expand Up @@ -1633,13 +1644,13 @@ private J.NewClass visitNewExpression(TSCNode node) {
this::visitNode,
true
);
List<JRightPadded<Expression>> elements = jContainer.getPadding().getElements().stream()
.map(it -> {
Expression exp = (!(it.getElement() instanceof Expression) && it.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) it.getElement()) : (Expression) it.getElement();
return padRight(exp, it.getAfter(), it.getMarkers());
})
.collect(toList());
List<JRightPadded<Expression>> elements = new ArrayList<>();
for (JRightPadded<J> jjRightPadded : jContainer.getPadding().getElements()) {
Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();
JRightPadded<Expression> apply = padRight(exp, jjRightPadded.getAfter(), jjRightPadded.getMarkers());
elements.add(apply);
}
JContainer<Expression> arguments = JContainer.build(jContainer.getBefore(), elements, jContainer.getMarkers());
return new J.NewClass(
randomId(),
Expand Down Expand Up @@ -1684,13 +1695,13 @@ private J visitObjectLiteralExpression(TSCNode node) {
if (jContainer.getElements().isEmpty()) {
arguments = JContainer.empty();
} else {
List<JRightPadded<Expression>> elements = jContainer.getPadding().getElements().stream()
.map(it -> {
Expression exp = (!(it.getElement() instanceof Expression) && it.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) it.getElement()) : (Expression) it.getElement();
return padRight(exp, it.getAfter(), it.getMarkers());
})
.collect(toList());
List<JRightPadded<Expression>> elements = new ArrayList<>();
for (JRightPadded<J> jjRightPadded : jContainer.getPadding().getElements()) {
Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ?
new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();
JRightPadded<Expression> apply = padRight(exp, jjRightPadded.getAfter(), jjRightPadded.getMarkers());
elements.add(apply);
}
arguments = JContainer.build(jContainer.getBefore(), elements, jContainer.getMarkers());
}

Expand Down

0 comments on commit e8375f0

Please sign in to comment.