@Override public void leaveNode(AstNode astNode) { if (isInClass && classStack.peek().isInConstructor && astNode.is(FlexGrammar.FUNCTION_DEF)) { classStack.peek().isInConstructor = false; } else if (isInClass && astNode.is(FlexGrammar.CLASS_DEF)) { classStack.pop(); isInClass = classStack.isEmpty() ? false : true; } }
@Override public void visitNode(AstNode astNode) { if (astNode.is(FlexGrammar.CLASS_DEF)) { inClass = true; classStack.push(new ClassState(astNode)); } else if (inClass && astNode.is(FlexGrammar.QUALIFIED_IDENTIFIER)) { classStack.peek().use(astNode); } }
@Override public void visitNode(AstNode astNode) { if (astNode.is(PHPGrammar.FUNCTION_DECLARATION)) { getContext().createLineViolation(this, "Move this function into a class.", astNode); } else if (scopes.isEmpty()) { checkAssignment(astNode); } if (astNode.is(FunctionUtils.functions())) { scopes.push(astNode); } }
@Override public void visitNode(AstNode astNode) { if (astNode.is(PythonGrammar.FILE_INPUT)) { visitModule(astNode); } if (astNode.is(PythonGrammar.FUNCDEF)) { visitFuncDef(astNode); } if (astNode.is(PythonGrammar.CLASSDEF)) { visitClassDef(astNode); } }
@Override public void visitNode(AstNode astNode) { EcmaScriptGrammar g = getContext().getGrammar(); if (astNode.is(g.functionDeclaration, g.functionExpression)) { checkFunction(astNode); } else if (astNode.is(g.catch_, g.variableDeclaration, g.variableDeclarationNoIn)) { checkVariableDeclaration(astNode); } else if (astNode.is(g.propertySetParameterList)) { checkPropertySetParameterList(astNode); } else if (astNode.is(g.memberExpression)) { checkModification(astNode); } }
private static AstNode getFunctionBlock(AstNode functionNode) { if (functionNode.is(PHPGrammar.METHOD_DECLARATION)) { return functionNode.getFirstChild(PHPGrammar.METHOD_BODY).getFirstChild(PHPGrammar.BLOCK); } else { return functionNode.getFirstChild(PHPGrammar.BLOCK); } }
@Test public void test_children_when_one_child() { AstNodeType type1 = mock(AstNodeType.class); AstNodeType type2 = mock(AstNodeType.class); AstNode child = mock(AstNode.class); when(node.getNumberOfChildren()).thenReturn(1); when(node.getFirstChild()).thenReturn(child); AstSelect children = select.children(); assertThat((Object) children).isInstanceOf(SingleAstSelect.class); assertThat(children).containsOnly(child); when(node.getChildren()).thenReturn(ImmutableList.of(child)); children = select.children(type1); assertThat((Object) children).isSameAs(AstSelectFactory.empty()); when(child.getType()).thenReturn(type1); children = select.children(type1); assertThat((Object) children).isInstanceOf(SingleAstSelect.class); assertThat(children).containsOnly(child); children = select.children(type1, type2); assertThat((Object) children).isSameAs(AstSelectFactory.empty()); when(child.is(type1, type2)).thenReturn(true); children = select.children(type1, type2); assertThat((Object) children).isInstanceOf(SingleAstSelect.class); assertThat(children).containsOnly(child); }
@Override public void visitNode(AstNode node) { if (node.is(PHPGrammar.IF_STATEMENT)) { visitIfStatementNode(node); } else { visitCaseListNode(node); } }
@Override public void leaveNode(AstNode astNode) { if (astNode.is(FlexGrammar.CLASS_DEF)) { reportUnusedPrivateFunction(); classStack.pop(); inClass = classStack.isEmpty() ? false : true; } }
@Test public void test_filter_by_types() { AstNodeType type1 = mock(AstNodeType.class); AstNodeType type2 = mock(AstNodeType.class); assertThat((Object) select.filter(type1, type2)).isSameAs(AstSelectFactory.empty()); when(node.is(type1, type2)).thenReturn(true); assertThat((Object) select.filter(type1, type2)).isSameAs(select); }
private boolean isDefaultOrDeleteFunctionBody(AstNode functionBodyNode) { boolean defaultOrDelete = false; List<AstNode> functionBody = functionBodyNode.getChildren(); // look for exact sub AST if (functionBody.size() == 3) { if (functionBody.get(0).is(CxxPunctuator.ASSIGN) && functionBody.get(2).is(CxxPunctuator.SEMICOLON)) { AstNode bodyType = functionBody.get(1); if (bodyType.is(CxxKeyword.DELETE) || bodyType.is(CxxKeyword.DEFAULT)) { defaultOrDelete = true; } } } return defaultOrDelete; }
@Override public void visitNode(AstNode node) { if (node.is(CxxGrammarImpl.LITERAL)) { reg.reset(node.getTokenOriginalValue().replaceAll("\\s", "")); if (reg.find()) { getContext() .createLineViolation(this, "Do not hard code sensitive data in programs.", node); } } }
@Override public void visitNode(AstNode node) { for (AstNode nestedNode : node.getDescendants(PuppetGrammar.CLASSDEF, PuppetGrammar.DEFINITION)) { message = "Move this nested " + (nestedNode.is(PuppetGrammar.CLASSDEF) ? "class " : "define ") + "\"" + nestedNode.getFirstChild(PuppetGrammar.CLASSNAME).getTokenValue() + "\"" + " outside of " + (node.is(PuppetGrammar.CLASSDEF) ? "class " : "define ") + "\"" + node.getFirstChild(PuppetGrammar.CLASSNAME).getTokenValue() + "\"" + "."; addIssue(nestedNode, this, message); } }
@Test public void getSecondDeclarationTest() { AstNodeXPathQuery<AstNode> xpath1 = AstNodeXPathQuery.create("/COMPILATION_UNIT/DEFINITION[@tokenLine=4]"); AstNodeXPathQuery<AstNode> xpath2 = AstNodeXPathQuery.create("/COMPILATION_UNIT/DEFINITION[2]"); AstNode declarationAtLineFour = fileNode.getChildren().get(1); assertThat(declarationAtLineFour.is(MiniCGrammar.DEFINITION)).isTrue(); assertThat(declarationAtLineFour.getTokenLine()).isEqualTo(4); assertThat(xpath1.selectSingleNode(fileNode)).isEqualTo(declarationAtLineFour); assertThat(xpath1.selectSingleNode(fileNode)).isEqualTo(xpath2.selectSingleNode(fileNode)); }
private void checkAssignment(AstNode astNode) { AstNode memberExpr = astNode.getFirstChild(); AstNode variable = memberExpr.getFirstChild(); if (memberExpr.getNumberOfChildren() == 1 && variable.is(PHPGrammar.VARIABLE_WITHOUT_OBJECTS)) { String varName = variable.getTokenOriginalValue(); if (memberExpr.getTokens().size() == 1 && !CheckUtils.isSuperGlobal(varName) && !globalVariableNames.contains(varName)) { getContext().createLineViolation(this, "Move this variable into a class.", astNode); globalVariableNames.add(varName); } } }
@Test public void test_firstAncestor_by_types() { AstNodeType type1 = mock(AstNodeType.class); AstNodeType type2 = mock(AstNodeType.class); assertThat((Object) select.firstAncestor(type1, type2)).isSameAs(AstSelectFactory.empty()); AstNode parent = mock(AstNode.class); when(node.getParent()).thenReturn(parent); AstNode ancestor = mock(AstNode.class); when(ancestor.is(type1, type2)).thenReturn(true); when(parent.getParent()).thenReturn(ancestor); assertThat((Object) select.firstAncestor(type1, type2)).isInstanceOf(SingleAstSelect.class); assertThat(select.firstAncestor(type1, type2)).containsOnly(ancestor); }
@Override public void visitNode(AstNode astNode) { if (astNode.is(FlexGrammar.UNARY_EXPR) && "not".equals(astNode.getFirstChild().getTokenValue())) { getContext() .createLineViolation( this, "Operator 'not' not available in ActionScript 3.0", astNode.getFirstChild()); } else { String operator = getValue(astNode); if (deprecatedOperators.contains(operator)) { getContext() .createLineViolation( this, "Operator '" + operator + "' not available in ActionScript 3.0", astNode); } } }
@Override public void visitNode(AstNode astNode) { if (astNode.is(FlexGrammar.CLASS_DEF)) { isInClass = true; String className = Clazz.getName(astNode); classStack.push(new ClassState(className)); } else if (isConstructor(astNode)) { classStack.peek().isInConstructor = true; } else if (isCallToDispatchEventInConstructor(astNode)) { getContext() .createLineViolation( this, "Remove this event dispatch from the \"{0}\" constructor", astNode, classStack.peek().className); } }
@Override protected void retrievePrivateClassMember(AstNode classDeclaration) { for (AstNode classStmt : classDeclaration.getChildren(PHPGrammar.CLASS_STATEMENT)) { AstNode stmtChild = classStmt.getFirstChild(); if (stmtChild.is(PHPGrammar.CLASS_VARIABLE_DECLARATION)) { List<AstNode> modifiers = stmtChild .getFirstChild(PHPGrammar.VARIABLE_MODIFIERS) .getChildren(PHPGrammar.MEMBER_MODIFIER); if (isPrivate(modifiers)) { for (AstNode varDeclaration : stmtChild.getChildren(PHPGrammar.VARIABLE_DECLARATION)) { AstNode varIdentifier = varDeclaration.getFirstChild(PHPGrammar.VAR_IDENTIFIER); addPrivateMember(getCalledName(varIdentifier, modifiers), varIdentifier); } } } } }
private boolean isElseIf(AstNode astNode) { AstNode parentPreviousSibling = astNode.getParent().getPreviousSibling(); return parentPreviousSibling != null && parentPreviousSibling.is(PHPKeyword.ELSE); }
private boolean isConstructor(AstNode astNode) { return isInClass && astNode.is(FlexGrammar.FUNCTION_DEF) && Function.isConstructor(astNode, classStack.peek().className); }
private boolean isCallToDispatchEventInConstructor(AstNode astNode) { return isInClass && classStack.peek().isInConstructor && astNode.is(FlexGrammar.PRIMARY_EXPR) && isCallToDispatchEvent(astNode); }
@Override public void leaveNode(AstNode astNode) { if (astNode.is(FunctionUtils.functions())) { scopes.pop(); } }