Package com.sonar.sslr.api

Examples of com.sonar.sslr.api.AstNode


    subscribeTo(EcmaScriptGrammar.FOR_IN_STATEMENT);
  }

  @Override
  public void visitNode(AstNode astNode) {
    AstNode statementNode = astNode.getFirstChild(EcmaScriptGrammar.STATEMENT);

    if (statementNode.getFirstChild().is(EcmaScriptGrammar.BLOCK)) {
      AstNode statementListNode = statementNode.getFirstChild().getFirstChild(EcmaScriptGrammar.STATEMENT_LIST);
      if (statementListNode == null) {
        statementNode = null;
      } else {
        statementNode = statementListNode.getChildren(EcmaScriptGrammar.STATEMENT).get(0).getFirstChild();
      }
    } else {
      statementNode = statementNode.getFirstChild();
    }
View Full Code Here


    }

  }

  public static boolean isOnlyBooleanLiteral(AstNode exprNode) {
    AstNode exprChild = exprNode.getFirstChild(EcmaScriptGrammar.EXPRESSION).getFirstChild();

    if (!exprChild.getToken().equals(exprChild.getLastToken())) {
      return false;
    }

    String tokenValue = exprChild.getTokenValue();
    return EcmaScriptKeyword.TRUE.getValue().equals(tokenValue)
      || EcmaScriptKeyword.FALSE.getValue().equals(tokenValue);
  }
View Full Code Here

      EcmaScriptGrammar.LOGICAL_OR_EXPRESSION);
  }

  @Override
  public void visitNode(AstNode astNode) {
    AstNode boolLiteral = getBooleanLiteralFromExpresion(astNode);

    if (boolLiteral != null) {
      getContext().createLineViolation(this, "Remove the literal \"" + boolLiteral.getTokenOriginalValue() + "\" boolean value.", astNode);
    }
  }
View Full Code Here

    // e.g x == y == false
    } else if (expression.getNumberOfChildren() != 3){
      return null;
    }

    AstNode leftExpr = expression.getFirstChild();
    AstNode rightExpr = expression.getLastChild();

    if (isBooleanLiteral(leftExpr)) {
      return leftExpr;
    } else if (isBooleanLiteral(rightExpr)) {
      return rightExpr;
View Full Code Here

      return null;
    }
  }

  private static AstNode getBooleanLiteralFromUnaryExpression(AstNode unaryExpression) {
    AstNode boolLiteral = null;

    if (unaryExpression.getFirstChild().is(EcmaScriptPunctuator.BANG)) {
      AstNode expr = unaryExpression.getLastChild();

      if (isBooleanLiteral(expr)) {
        boolLiteral = expr;
      }
    }
View Full Code Here

  public void visitNode(AstNode node) {
    while (node.getParent().is(EcmaScriptGrammar.STATEMENT)) {
      node = node.getParent();
    }

    AstNode nextStatement = node.getNextSibling();
    if (isUnReachableCode(nextStatement)) {
      getContext().createLineViolation(this, "This statement can't be reached and so start a dead code block.", nextStatement);
    }
  }
View Full Code Here

        .subscribeTo(FUNCTION_NODES)
        .build());

    builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<LexerlessGrammar>(new SourceCodeBuilderCallback() {
      public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
        AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, EcmaScriptGrammar.BINDING_IDENTIFIER);
        final String functionName = identifier == null ? "anonymous" : identifier.getTokenValue();
        final String fileKey = parentSourceCode.isType(SourceFile.class) ? parentSourceCode.getKey() : parentSourceCode.getParent(SourceFile.class).getKey();
        SourceFunction function = new SourceFunction(fileKey + ":" + functionName + ":" + astNode.getToken().getLine() + ":" + astNode.getToken().getColumn());
        function.setStartAtLine(astNode.getTokenLine());
        return function;
      }
View Full Code Here

    subscribeTo(EcmaScriptGrammar.CALL_EXPRESSION);
  }

  @Override
  public void visitNode(AstNode astNode) {
    AstNode simpleCallExpr = astNode.getFirstChild(EcmaScriptGrammar.SIMPLE_CALL_EXPRESSION);

    if (simpleCallExpr != null && isAlertCall(simpleCallExpr.getFirstChild(EcmaScriptGrammar.MEMBER_EXPRESSION))) {
      getContext().createLineViolation(this, "Remove this usage of alert(...).", astNode);
    }
  }
View Full Code Here

    }
    getContext().peekSourceCode().add(EcmaScriptMetric.COMPLEXITY, 1);
  }

  private static boolean isLastReturnStatement(AstNode returnNode) {
    AstNode nextNode = returnNode.getNextAstNode();
    return nextNode.is(EcmaScriptPunctuator.RCURLYBRACE) && isNotNested(returnNode);
  }
View Full Code Here

    AstNode nextNode = returnNode.getNextAstNode();
    return nextNode.is(EcmaScriptPunctuator.RCURLYBRACE) && isNotNested(returnNode);
  }

  private static boolean isNotNested(AstNode returnNode) {
    AstNode parent = returnNode
      // Statement
      .getParent()
      // Statement list
      .getParent();
    return parent.getParent().is(EcmaScriptGrammar.FUNCTION_BODY);
  }
View Full Code Here

TOP

Related Classes of com.sonar.sslr.api.AstNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.