Package com.sonar.sslr.api

Examples of com.sonar.sslr.api.AstNode


  public void visitNode(AstNode astNode) {
    if (astNode.is(EcmaScriptGrammar.LABELLED_STATEMENT)) {
      String label = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER).getTokenValue();
      jumpTargets.push(new JumpTarget(label));
    } else if (astNode.is(EcmaScriptGrammar.BREAK_STATEMENT, EcmaScriptGrammar.CONTINUE_STATEMENT)) {
      AstNode labelNode = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER);
      String label = labelNode == null ? null : labelNode.getTokenValue();
      for (int i = jumpTargets.size() - 1; i >= 0; i--) {
        JumpTarget jumpTarget = jumpTargets.get(i);
        jumpTarget.jumps++;
        if (Objects.equal(label, jumpTarget.label)) {
          break;
View Full Code Here


  }

  @Override
  public void visitNode(AstNode astNode) {
    if (astNode.getNextAstNode().is(EcmaScriptGrammar.CASE_CLAUSE, EcmaScriptGrammar.DEFAULT_CLAUSE, EcmaScriptGrammar.CASE_CLAUSES)) {
      AstNode statementList = astNode.getFirstChild(EcmaScriptGrammar.STATEMENT_LIST);
      if (statementList != null
        && statementList.getLastChild().getFirstChild().isNot(EcmaScriptGrammar.BREAK_STATEMENT, EcmaScriptGrammar.RETURN_STATEMENT, EcmaScriptGrammar.THROW_STATEMENT)) {
        getContext().createLineViolation(this, "Last statement in this switch-clause should be an unconditional break.", astNode);
      }
    }
  }
View Full Code Here

    subscribeTo(EcmaScriptGrammar.CASE_BLOCK);
  }

  @Override
  public void visitNode(AstNode astNode) {
    AstNode defaultClauseNode = astNode.getFirstChild(EcmaScriptGrammar.DEFAULT_CLAUSE);
    if (defaultClauseNode == null) {
      getContext().createLineViolation(this, "Avoid switch statement without a \"default\" clause.", astNode);
    } else if (defaultClauseNode.getNextSibling().isNot(EcmaScriptPunctuator.RCURLYBRACE)) {
      getContext().createLineViolation(this, "\"default\" clause should be the last one.", astNode);
    }
  }
View Full Code Here

  }

  @Override
  public void visitNode(AstNode node) {
    if (isElseIf(node)) {
      AstNode elseClause = node.getFirstChild(EcmaScriptGrammar.ELSE_CLAUSE);
      if (elseClause == null) {
        getContext().createLineViolation(this, "Add the missing \"else\" clause.", node);
      }
    }
  }
View Full Code Here

    subscribeTo(EcmaScriptGrammar.BLOCK);
  }

  @Override
  public void visitNode(AstNode astNode) {
    AstNode stmtList = astNode.getFirstChild(EcmaScriptGrammar.STATEMENT_LIST);
    if (stmtList != null) {

      for (AstNode declarationNode : stmtList.getChildren(EcmaScriptGrammar.DECLARATION)) {
        if (declarationNode.getFirstChild().is(EcmaScriptGrammar.FUNCTION_DECLARATION)) {
          getContext().createLineViolation(this, "Do not use function declarations within blocks.", declarationNode);
        }
      }
    }
View Full Code Here

    subscribeTo(EcmaScriptGrammar.CALL_EXPRESSION);
  }

  @Override
  public void visitNode(AstNode node) {
    AstNode simpleCallExprNode = node.getFirstChild(EcmaScriptGrammar.SIMPLE_CALL_EXPRESSION);

    if (simpleCallExprNode != null) {
      AstNode memberExpressionNode = simpleCallExprNode.getFirstChild(EcmaScriptGrammar.MEMBER_EXPRESSION);

      if (memberExpressionNode != null && "eval".equals(memberExpressionNode.getTokenValue())) {
        getContext().createLineViolation(this, "Remove this use of the \"eval\" function.", node);
      }
    }
  }
View Full Code Here

      declareInCurrentScope(IdentifierUtils.getArrowParametersIdentifier(astNode));
    } else if (astNode.is(CONST_AND_VAR_NODES)) {
      declareInCurrentScope(IdentifierUtils.getVariableIdentifiers(astNode));

    } else if (astNode.is(EcmaScriptGrammar.PRIMARY_EXPRESSION)) {
      AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER);
      if (identifier != null) {
        currentScope.use(identifier);
      }
    }
  }
View Full Code Here

    }
  }

  private void checkCurrentScope() {
    for (Map.Entry<String, AstNode> entry : currentScope.firstDeclaration.entrySet()) {
      AstNode declaration = entry.getValue();
      AstNode usage = currentScope.firstUsage.get(entry.getKey());
      if (usage != null && usage.getTokenLine() < declaration.getTokenLine()) {
        getContext().createLineViolation(this, "Variable '" + entry.getKey() + "' referenced before declaration.", usage);
      }
    }
  }
View Full Code Here

      checkModification(astNode.getFirstDescendant(EcmaScriptGrammar.MEMBER_EXPRESSION));
    }
  }

  private void checkFunction(AstNode functionNode) {
    AstNode identifier = functionNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER);
    if (identifier != null && isEvalOrArguments(identifier.getTokenValue())) {
      getContext().createLineViolation(this, createMessageFor("function", identifier.getTokenValue()), identifier);
    }
    AstNode formalParameterList = functionNode.getFirstChild(EcmaScriptGrammar.FORMAL_PARAMETER_LIST);

    if (formalParameterList != null) {
      checkFormalParamList(formalParameterList);
    }
  }
View Full Code Here

      // enter new scope
      currentScope = new Scope(currentScope, astNode);
    } else if (currentScope != null && astNode.is(EcmaScriptGrammar.FORMAL_PARAMETER_LIST)) {
      declareInCurrentScope(IdentifierUtils.getParametersIdentifier(astNode));
    } else if (currentScope != null && astNode.is(EcmaScriptGrammar.PRIMARY_EXPRESSION)) {
      AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER);
      if (identifier != null) {
        if ("arguments".equals(identifier.getTokenValue())) {
          currentScope.useArgumentsArray = true;
        }
        currentScope.use(identifier);
      }
    }
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.