public static int getNumberOfLine(AstNode classDeclaration) { int firstLine = classDeclaration.getFirstChild(PHPPunctuator.LCURLYBRACE).getTokenLine(); int lastLine = classDeclaration.getFirstChild(PHPPunctuator.RCURLYBRACE).getTokenLine(); return lastLine - firstLine + 1; }
@Override public void visitNode(AstNode statementNode) { if (!isExcluded(statementNode)) { int line = statementNode.getTokenLine(); if (!statementsPerLine.containsKey(line)) { statementsPerLine.put(line, 0); } statementsPerLine.put(line, statementsPerLine.get(line) + 1); } }
@Override public void visitNode(AstNode astNode) { AstNode lastLabelNode = Iterables.getLast(astNode.getChildren(FlexGrammar.CASE_LABEL)); int lines = Math.max(astNode.getNextAstNode().getTokenLine() - lastLabelNode.getTokenLine(), 1); if (lines > max) { getContext().createLineViolation(this, "Reduce this switch case number of lines from {0} to at most {1}, for example by extracting code into methods.", lastLabelNode, lines, max); } }
public static boolean isOnASingleLine(AstNode parent) { List<AstNode> statements = parent.getChildren(PythonGrammar.STATEMENT); if (statements.isEmpty()) { return true; } return statements.get(0).getTokenLine() == statements.get(statements.size() - 1).getLastToken().getLine(); } }
private void checkCondition(AstNode caseLabel) { String expression = Expression.exprToString(caseLabel.getFirstChild(FlexGrammar.LIST_EXPRESSION)); AstNode duplicateCase = casesByCondition.get(expression); if (duplicateCase != null) { addIssue(MessageFormat.format("This case duplicates the case on line {0} with condition \"{1}\".", duplicateCase.getTokenLine(), expression), caseLabel); } else { casesByCondition.put(expression, caseLabel); } }
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("class:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } }, FlexGrammar.CLASS_DEF, FlexGrammar.INTERFACE_DEF));
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceFunction function = new SourceFunction("function:" + seq); function.setStartAtLine(astNode.getTokenLine()); return function; } }, FlexGrammar.FUNCTION_DEF, FlexGrammar.FUNCTION_EXPR));
private boolean isExtendsOnClassNameLine(AstNode classDeclaration, int classNameLine) { AstNode extendsNode = classDeclaration.getFirstChild(PHPGrammar.EXTENDS_FROM); if (extendsNode != null) { return classNameLine == extendsNode.getFirstChild(PHPKeyword.EXTENDS).getTokenLine(); } return true; }
public static int getNumberOfLine(AstNode functionNode) { AstNode blockNode = functionNode.getFirstChild(FlexGrammar.FUNCTION_COMMON).getFirstChild(FlexGrammar.BLOCK); // Is not an abstract method if (blockNode != null) { int firstLine = blockNode.getFirstChild(FlexPunctuator.LCURLYBRACE).getTokenLine(); int lastLine = blockNode.getFirstChild(FlexPunctuator.RCURLYBRACE).getTokenLine(); return lastLine - firstLine + 1; } return 0; } }
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { seq++; SourceClass cls = new SourceClass("class:" + seq); cls.setStartAtLine(astNode.getTokenLine()); return cls; } }, Kind.CLASS_DECLARATION, Kind.CLASS_EXPRESSION));
public static int getNumberOfLine(AstNode functionNode) { AstNode blockNode = functionNode.getFirstChild(FlexGrammar.FUNCTION_COMMON).getFirstChild(FlexGrammar.BLOCK); // Is not an abstract method if (blockNode != null) { int firstLine = blockNode.getFirstChild(FlexPunctuator.LCURLYBRACE).getTokenLine(); int lastLine = blockNode.getFirstChild(FlexPunctuator.RCURLYBRACE).getTokenLine(); return lastLine - firstLine + 1; } return 0; } }
private void checkCondition(AstNode caseLabel) { String expression = Expression.exprToString(caseLabel.getFirstChild(FlexGrammar.LIST_EXPRESSION)); AstNode duplicateCase = casesByCondition.get(expression); if (duplicateCase != null) { getContext().createLineViolation(this, "This case duplicates the case on line {0} with condition \"{1}\".", caseLabel, duplicateCase.getTokenLine(), expression); } else { casesByCondition.put(expression, caseLabel); } }
private void appendNodecontent(AstNode node) throws IOException { writer.append(node.getName()); if (node.getTokenValue() != null) { writer.append(" tokenValue=\"" + node.getTokenValue() + "\""); } if (node.hasToken()) { writer.append(" tokenLine=\"" + node.getTokenLine() + "\" tokenColumn=\"" + node.getToken().getColumn() + "\""); } }
private void appendNodecontent(AstNode node) throws IOException { writer.append(node.getName()); if (node.getTokenValue() != null) { writer.append(" tokenValue=\"" + node.getTokenValue() + "\""); } if (node.hasToken()) { writer.append(" tokenLine=\"" + node.getTokenLine() + "\" tokenColumn=\"" + node.getToken().getColumn() + "\""); } }
private boolean isFirstOnline(AstNode curlyBrace) { Token previousToken = curlyBrace.getPreviousAstNode().getLastToken(); // In one case, clonsing parenthesis can be on the same line as the opening curly brace if (previousToken.getType().equals(PHPPunctuator.RPARENTHESIS)) { previousToken = curlyBrace.getPreviousAstNode().getPreviousAstNode().getLastToken(); } return previousToken.getLine() != curlyBrace.getTokenLine(); }
@Override public void visitNode(AstNode astNode) { if (!isIdentityComparison(astNode)) { AstNode boolLiteral = getBooleanLiteralFromExpression(astNode); if (boolLiteral != null && !isAlreadyChecked(boolLiteral)) { getContext().createLineViolation(this, "Remove the literal \"" + boolLiteral.getTokenOriginalValue() + "\" boolean value.", astNode); alreadyChecked.put(boolLiteral.getTokenLine(), boolLiteral.getToken().getColumn()); } } }
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { String functionName = astNode.getFirstChild(PythonGrammar.CLASSNAME).getFirstChild().getTokenValue(); SourceClass function = new SourceClass(functionName + ":" + astNode.getToken().getLine()); function.setStartAtLine(astNode.getTokenLine()); return function; } }, PythonGrammar.CLASSDEF));
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { String className = astNode.getFirstDescendant(ErlangGrammarImpl.moduleAttr).getFirstChild(ErlangGrammarImpl.atom).getTokenValue(); SourceClass cls = new SourceClass(className + ":" + astNode.getToken().getLine()); cls.setStartAtLine(astNode.getTokenLine()); return cls; } }, ErlangGrammarImpl.module));
@Override public void visitNode(AstNode node) { if (!isExcluded(node) && node.getPreviousAstNode().getLastToken() != null && node.getPreviousAstNode().getLastToken().getLine() != node.getTokenLine()) { getContext().createLineViolation(this, "Move this left curly brace to the end of previous line of code.", node); } }
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, Kind.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; } }, FUNCTION_NODES));