Skip to content

Commit

Permalink
SONARJS-599 Update message of ConstructorFunctionsForSideEffectsCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
vilchik-elena authored and pynicolas committed Dec 22, 2015
1 parent 6faec0d commit a9c8177
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.javascript.checks.utils.CheckUtils;
import org.sonar.plugins.javascript.api.tree.Tree;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.plugins.javascript.api.tree.expression.NewExpressionTree;
import org.sonar.plugins.javascript.api.tree.statement.ExpressionStatementTree;
import org.sonar.plugins.javascript.api.visitors.BaseTreeVisitor;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
Expand All @@ -39,12 +42,14 @@
@SqaleConstantRemediation("5min")
public class ConstructorFunctionsForSideEffectsCheck extends BaseTreeVisitor {

private static final String MESSAGE = "Replace by a standard call to the function.";
private static final String MESSAGE = "Either remove this useless object instantiation of \"%s\" or use it";

@Override
public void visitExpressionStatement(ExpressionStatementTree tree) {
if (tree.expression().is(Kind.NEW_EXPRESSION)) {
getContext().addIssue(this, tree, MESSAGE);
Tree expression = tree.expression();
if (expression.is(Kind.NEW_EXPRESSION)) {
String message = String.format(MESSAGE, CheckUtils.asString(((NewExpressionTree) expression).expression()));
getContext().addIssue(this, expression, message);
}

super.visitExpressionStatement(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
*/
package org.sonar.javascript.checks;

import java.io.File;
import org.junit.Test;
import org.sonar.javascript.checks.utils.JavaScriptCheckVerifier;
import org.sonar.plugins.javascript.api.tests.TreeCheckTest;
import org.sonar.squidbridge.checks.CheckMessagesVerifier;

public class ConstructorFunctionsForSideEffectsCheckTest extends TreeCheckTest {

@Test
public void test() {
CheckMessagesVerifier.verify(getIssues("src/test/resources/checks/constructorFunctionsForSideEffects.js", new ConstructorFunctionsForSideEffectsCheck()))
.next().atLine(4).withMessage("Replace by a standard call to the function.")
.noMore();
JavaScriptCheckVerifier.verify(new ConstructorFunctionsForSideEffectsCheck(), new File("src/test/resources/checks/constructorFunctionsForSideEffects.js"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default new MyConstructor(); // OK
var something = new MyConstructor(); // OK
something = new MyConstructor(); // OK
new MyConstructor(); // NOK
new MyConstructor(); // Noncompliant {{Either remove this useless object instantiation of "MyConstructor" or use it}}
callMethod(new MyConstructor()); // OK
new MyConstructor().doSomething(); // OK

0 comments on commit a9c8177

Please sign in to comment.