JavaScript Custom Rules

104 views
Skip to first unread message

Sam Zeng

unread,
Oct 25, 2016, 2:40:28 AM10/25/16
to SonarQube
Hi, I want to check if a string contains 'http://' at the start. My Java is poor, and I really can not find a person to ask.

Here is my code:

/*
 * Copyright (C) 2009-2013 SonarSource SA
 * All rights reserved
 * mailto:contact AT sonarsource DOT com
 */
package org.sonar.samples.javascript.checks;

import com.google.common.collect.ImmutableSet;
import java.util.Set;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sonar.check.Rule;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.plugins.javascript.api.tree.statement.VariableDeclarationTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;

@Rule(
  key
= "S1",
  priority
= Priority.MAJOR,
  name
= "HTTP protocol is not required.",
  tags
= {"convention"}
)
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.DATA_RELIABILITY)
@SqaleConstantRemediation("5min")
public class ForbiddenHttpProtocalUseCheck extends DoubleDispatchVisitorCheck {
 
private static final Pattern variablePattern = Pattern.compile(":(http)", Pattern.CASE_INSENSITIVE);

 
@Override
 
public void visitVariableDeclaration(VariableDeclarationTree tree) {
   
Matcher matcher = variablePattern.matcher( /* Some String Maybe here */ );
   
if (matcher.find()) {
      addIssue
(tree, "Remove the usage of this HTTP protocol.");
   
}

   
super.visitVariableDeclaration(tree);
 
}
}

On my Step, I only can match `var` statement.



Thanks,
Sam 

Pierre-Yves Nicolas

unread,
Oct 25, 2016, 8:48:35 AM10/25/16
to Sam Zeng, SonarQube
Hi Sam,

Instead of visiting variable declarations, you can visit literals. Something like:

  public void visitLiteral(LiteralTree tree) {
    String literalValue = tree.token().text();
    // ...
  }

Pierre-Yves

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/030c3cd0-2b7a-4fc5-a380-1e68c306c304%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam Zeng

unread,
Oct 25, 2016, 9:18:29 AM10/25/16
to SonarQube, zengd...@gmail.com
Thank you very much.

I found this too, I imitate this code, But My code still does not work. Did I write something wrong?


Here is my new code:

package org.sonar.samples.javascript.checks;

import java.util.Set;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.plugins.javascript.api.tree.expression.LiteralTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;

@Rule(
    key = "S1",
    priority = Priority.MAJOR,
    name = "HTTP protocol is not required.",
    tags = {"convention"}
)
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.DATA_RELIABILITY)
@SqaleConstantRemediation("5min")
public class ForbiddenHttpProtocalUseCheck extends DoubleDispatchVisitorCheck {
    @Override
    public void visitLiteral(LiteralTree tree) {
        if (tree.is(Kind.STRING_LITERAL)) {
            String value = tree.value();
            if (value.length() > 1 && value.startsWith("http://")) {
                addIssue(tree, "Remove the usage of this HTTP protocol.");
            }
        }
    }
}



在 2016年10月25日星期二 UTC+8下午8:48:35,Pierre-Yves Nicolas写道:
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

Pierre-Yves Nicolas

unread,
Oct 25, 2016, 9:26:17 AM10/25/16
to Sam Zeng, SonarQube
That's because the value of the literal includes the quotes.

To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/e9ca8d85-0754-403e-a838-49aca75a35d6%40googlegroups.com.

Sam Zeng

unread,
Oct 25, 2016, 10:22:39 AM10/25/16
to SonarQube, zengd...@gmail.com
Yes, You're right, thanks again.

Here is my final code, it works fo me, maybe help some people, or just me :)

/*
 * Copyright (C) 2009-2013 SonarSource SA
 * All rights reserved
 * mailto:contact AT sonarsource DOT com
 */
package org.sonar.samples.javascript.checks;

import java.util.Set;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.plugins.javascript.api.tree.expression.LiteralTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;

@Rule(
    key
= "S1",
    priority
= Priority.MAJOR,
    name
= "HTTP protocol is not required.",
    tags
= {"convention"}
)
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.DATA_RELIABILITY)
@SqaleConstantRemediation("5min")

public class ForbiddenHttpProtocalUseCheck extends DoubleDispatchVisitorCheck {

   
private static final Pattern variablePattern = Pattern.compile("http\\://", Pattern.CASE_INSENSITIVE);

   
@Override

   
public void visitLiteral(LiteralTree tree) {
       
if (tree.is(Kind.STRING_LITERAL)) {

           
String literalValue = tree.value();
           
Matcher matcher = variablePattern.matcher(literalValue);
//            System.out.println(literalValue.length());
//            System.out.println(matcher.find());

            if (literalValue.length() > 1 && matcher.find()) {

                addIssue
(tree, "Remove the usage of this HTTP protocol.");
           
}
       
}
   
}
}



在 2016年10月25日星期二 UTC+8下午9:26:17,Pierre-Yves Nicolas写道:

Sam Zeng

unread,
Nov 30, 2016, 2:25:54 AM11/30/16
to SonarQube, zengd...@gmail.com
is there a way to distinguish the following situation? I just want to check `a`。

var b = require('http://www.test.com');



在 2016年10月25日星期二 UTC+8下午9:26:17,Pierre-Yves Nicolas写道:
That's because the value of the literal includes the quotes.

Pierre-Yves Nicolas

unread,
Dec 1, 2016, 12:14:10 PM12/1/16
to Sam Zeng, SonarQube
What you want to achieve is not very clear to me.
You can try implementing visitCallExpression.

To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/c0ccdac4-9b13-4ca8-84f6-7ccd8852c8d5%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages