Skip to content

Commit be2d197

Browse files
invernovilchik-elena
authored andcommitted
Fix some quality flaws
1 parent e21375a commit be2d197

File tree

15 files changed

+46
-44
lines changed

15 files changed

+46
-44
lines changed

javascript-checks/src/main/java/org/sonar/javascript/checks/EqEqEqCheck.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ boolean isProblem(BinaryExpressionTree tree, ProgramState currentState) {
5858
Type rightType = rightConstraint.type();
5959
Type leftType = leftConstraint.type();
6060

61-
if (leftType != null && leftType == rightType) {
62-
return true;
63-
}
64-
65-
return false;
61+
return leftType != null && leftType == rightType;
6662
}
6763

6864

javascript-checks/src/main/java/org/sonar/javascript/checks/ReassignedParameterCheck.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.sonar.javascript.checks;
2121

2222
import com.google.common.collect.ImmutableSet;
23-
import java.util.Optional;
2423
import java.util.Set;
2524
import javax.annotation.Nullable;
2625
import org.sonar.check.Rule;
@@ -84,30 +83,26 @@ private void checkForLoop(ForObjectStatementTree forLoop) {
8483

8584
} else if (variableOrExpression.is(Kind.IDENTIFIER_REFERENCE)) {
8685
IdentifierTree identifier = (IdentifierTree) variableOrExpression;
87-
checkSymbol(identifier.symbol(), identifier, forLoop, FOREACH_VARIABLE);
88-
86+
identifier.symbol().ifPresent(s -> checkSymbol(s, identifier, forLoop, FOREACH_VARIABLE));
8987
}
9088
}
9189

9290
private void checkBindingElement(BindingElementTree parameter, String title) {
9391
for (IdentifierTree identifierTree : parameter.bindingIdentifiers()) {
9492
// symbol for identifier from binding element should never be null
95-
checkSymbol(identifierTree.symbol(), identifierTree, null, title);
93+
identifierTree.symbol().ifPresent(s -> checkSymbol(s, identifierTree, null, title));
9694
}
9795
}
9896

99-
private void checkSymbol(Optional<Symbol> symbol, IdentifierTree declarationIdentifier, @Nullable ForObjectStatementTree loop, String title) {
100-
if (!symbol.isPresent()) {
101-
return;
102-
}
97+
private void checkSymbol(Symbol symbol, IdentifierTree declarationIdentifier, @Nullable ForObjectStatementTree loop, String title) {
10398

104-
for (Usage usage : symbol.get().usages()) {
99+
for (Usage usage : symbol.usages()) {
105100

106101
if (usage.isWrite() &&
107102
!usage.identifierTree().equals(declarationIdentifier)
108103
&& (loop == null || CheckUtils.isDescendant(usage.identifierTree(), loop))) {
109104

110-
addIssue(usage.identifierTree(), String.format(MESSAGE, title, symbol.get().name()));
105+
addIssue(usage.identifierTree(), String.format(MESSAGE, title, symbol.name()));
111106
}
112107
}
113108
}

javascript-checks/src/main/java/org/sonar/javascript/checks/UnconditionalJumpStatementCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void visitNode(Tree tree) {
6969
}
7070
}
7171

72-
private boolean isInfiniteFor(IterationStatementTree loopTree) {
72+
private static boolean isInfiniteFor(IterationStatementTree loopTree) {
7373
if (loopTree.is(Kind.FOR_STATEMENT)) {
7474
ForStatementTree forLoop = (ForStatementTree) loopTree;
7575
return forLoop.update() == null && forLoop.init() == null && forLoop.condition() == null;

javascript-frontend/src/main/java/org/sonar/javascript/metrics/CounterVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.sonar.javascript.metrics;
2121

2222
import java.util.Arrays;
23-
import java.util.HashSet;
23+
import java.util.EnumSet;
2424
import java.util.Set;
2525
import org.sonar.javascript.tree.KindSet;
2626
import org.sonar.plugins.javascript.api.tree.Tree;
@@ -59,7 +59,7 @@ public CounterVisitor(Tree tree) {
5959

6060
@Override
6161
public Set<Kind> nodesToVisit() {
62-
HashSet<Kind> result = new HashSet<>(KindSet.FUNCTION_KINDS.getSubKinds());
62+
Set<Kind> result = EnumSet.copyOf(KindSet.FUNCTION_KINDS.getSubKinds());
6363
result.addAll(Arrays.asList(STATEMENT_NODES));
6464
result.addAll(Arrays.asList(MetricsVisitor.getClassNodes()));
6565
return result;

javascript-frontend/src/main/java/org/sonar/javascript/metrics/MetricsVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
import java.io.Serializable;
2323
import java.util.Arrays;
24+
import java.util.EnumSet;
2425
import java.util.HashMap;
25-
import java.util.HashSet;
2626
import java.util.Map;
2727
import java.util.Set;
2828
import org.sonar.api.batch.fs.InputFile;
@@ -78,7 +78,7 @@ public Map<InputFile, Set<Integer>> executableLines() {
7878

7979
@Override
8080
public Set<Kind> nodesToVisit() {
81-
Set<Kind> result = new HashSet<>(KindSet.FUNCTION_KINDS.getSubKinds());
81+
Set<Kind> result = EnumSet.copyOf(KindSet.FUNCTION_KINDS.getSubKinds());
8282
result.addAll(Arrays.asList(CLASS_NODES));
8383
return result;
8484
}

javascript-frontend/src/main/java/org/sonar/javascript/parser/TreeFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,10 @@ public SeparatedList<SpecifierTree> exportListBody(
11271127
}
11281128

11291129
public SpecifierListTree exportList(InternalSyntaxToken openCurlyBraceToken, Optional<SeparatedList<SpecifierTree>> specifierList, InternalSyntaxToken closeCurlyBraceToken) {
1130-
return new SpecifierListTreeImpl(Kind.EXPORT_LIST, openCurlyBraceToken, specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
1130+
return new SpecifierListTreeImpl(
1131+
Kind.EXPORT_LIST,
1132+
openCurlyBraceToken,
1133+
specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
11311134
}
11321135

11331136
public NameSpaceExportDeclarationTree namespaceExportDeclaration(
@@ -1216,7 +1219,10 @@ public SeparatedList<SpecifierTree> newImportSpecifierList(
12161219
}
12171220

12181221
public SpecifierListTree importList(InternalSyntaxToken openCurlyBraceToken, Optional<SeparatedList<SpecifierTree>> specifierList, InternalSyntaxToken closeCurlyBraceToken) {
1219-
return new SpecifierListTreeImpl(Kind.IMPORT_LIST, openCurlyBraceToken, specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
1222+
return new SpecifierListTreeImpl(
1223+
Kind.IMPORT_LIST,
1224+
openCurlyBraceToken,
1225+
specifierList.or(new SeparatedListImpl<>(ImmutableList.of(), ImmutableList.of())), closeCurlyBraceToken);
12201226
}
12211227

12221228
public SpecifierTree nameSpaceImport(InternalSyntaxToken starToken, InternalSyntaxToken asToken, IdentifierTree localName) {

javascript-frontend/src/main/java/org/sonar/javascript/se/LiveVariableAnalysis.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.HashMap;
2828
import java.util.HashSet;
2929
import java.util.Map;
30-
import java.util.Optional;
3130
import java.util.Set;
3231
import javax.annotation.CheckForNull;
3332
import javax.annotation.Nullable;
@@ -171,21 +170,19 @@ public boolean hasUsagesInNestedFunctions(Symbol symbol) {
171170

172171
@CheckForNull
173172
private Usage add(IdentifierTree identifier) {
174-
addSymbol(identifier.symbol());
173+
identifier.symbol().ifPresent(s -> addSymbol(s));
175174
Usage usage = localVariableUsages.get(identifier);
176175
if (usage != null) {
177176
usagesInCFG.put(usage.symbol(), usage);
178177
}
179178
return usage;
180179
}
181180

182-
private void addSymbol(Optional<Symbol> symbolOptional) {
183-
if (!symbolOptional.isPresent() || symbols.contains(symbolOptional.get())) {
181+
private void addSymbol(Symbol symbol) {
182+
if (symbols.contains(symbol)) {
184183
return;
185184
}
186185

187-
Symbol symbol = symbolOptional.get();
188-
189186
symbols.add(symbol);
190187

191188
if (isLocalVariable(symbol)) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SonarQube JavaScript Plugin
3+
* Copyright (C) 2011-2017 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
@javax.annotation.ParametersAreNonnullByDefault
21+
package org.sonar.javascript.se.limitations;

javascript-frontend/src/main/java/org/sonar/javascript/tree/impl/declaration/ArrayBindingPatternTreeImpl.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ public List<IdentifierTree> bindingIdentifiers() {
9898
List<IdentifierTree> bindingIdentifiers = Lists.newArrayList();
9999

100100
for (Optional<BindingElementTree> element : elements) {
101-
if (element.isPresent()) {
102-
bindingIdentifiers.addAll(element.get().bindingIdentifiers());
103-
}
101+
element.ifPresent(bindingElementTree -> bindingIdentifiers.addAll(bindingElementTree.bindingIdentifiers()));
104102
}
105103

106104
return bindingIdentifiers;
@@ -110,10 +108,7 @@ private static class ElidedElementFilter implements Function<Optional<BindingEle
110108

111109
@Override
112110
public BindingElementTree apply(Optional<BindingElementTree> e) {
113-
if (e.isPresent()) {
114-
return e.get();
115-
}
116-
return null;
111+
return e.orElse(null);
117112
}
118113

119114
}

javascript-frontend/src/main/java/org/sonar/javascript/tree/impl/expression/ArrayAssignmentPatternTreeImpl.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ private static class ElidedElementFilter implements Function<Optional<Tree>, Tre
8686

8787
@Override
8888
public Tree apply(Optional<Tree> e) {
89-
if (e.isPresent()) {
90-
return e.get();
91-
}
92-
return null;
89+
return e.orElse(null);
9390
}
9491

9592
}

0 commit comments

Comments
 (0)