private static Token getToken(IdentifierTree identifierTree) { return ((InternalSyntaxToken) (identifierTree).identifierToken()).getToken(); } }
private static boolean isCollectionConstructor(ExpressionTree callee) { return callee.is(Kind.IDENTIFIER_REFERENCE) && COLLECTION_CONSTRUCTORS.contains(((IdentifierTree) callee).identifierToken().text()); }
private static int getLine(Usage usage) { return usage.identifierTree().identifierToken().line(); } }
private static boolean isNullOrUndefined(Tree tree) { return tree.is(Tree.Kind.NULL_LITERAL) || (tree.is(Tree.Kind.IDENTIFIER_REFERENCE) && "undefined".equals(((IdentifierTree) tree).identifierToken().text())); }
private static boolean isNullOrUndefined(Tree tree) { return tree.is(Tree.Kind.NULL_LITERAL) || (tree.is(Tree.Kind.IDENTIFIER_REFERENCE) && "undefined".equals(((IdentifierTree) tree).identifierToken().text())); }
private static int getLine(Symbol symbol) { return getDeclarationUsage(symbol).identifierTree().identifierToken().line(); }
private void checkSuperInvokedBeforeThisOrSuper(SuperTree superTree) { int line = superTree.firstToken().line(); int column = superTree.firstToken().column(); MethodDeclarationTree method = getEnclosingConstructor(superTree); Set<IssueLocation> secondaryLocations = new HashSet<>(); // get the usages of "super" before super() Set<SuperTree> superTrees = new SuperDetector().detectIn(method); superTrees.stream() .filter(s -> !s.parent().is(Kind.CALL_EXPRESSION)) .filter(s -> isBefore(s.firstToken(), line, column)) .map(IssueLocation::new) .forEach(secondaryLocations::add); // get the usages of "this" before super() Set<IdentifierTree> thisTrees = new ThisDetector().detectIn(method); thisTrees.stream() .filter(s -> isBefore(s.identifierToken(), line, column)) .map(IssueLocation::new) .forEach(secondaryLocations::add); // create the issue if (!secondaryLocations.isEmpty()) { PreciseIssue issue = addIssue(superTree, MESSAGE_SUPER_BEFORE_THIS_OR_SUPER); secondaryLocations.forEach(issue::secondary); } }
private static SyntaxToken functionToken(FunctionTree functionTree) { SyntaxToken token; if (functionTree.is(Kind.FUNCTION_DECLARATION, Kind.GENERATOR_DECLARATION)) { token = ((FunctionDeclarationTree) functionTree).name().identifierToken(); } else if (functionTree.is(Kind.FUNCTION_EXPRESSION, Kind.GENERATOR_FUNCTION_EXPRESSION)) { token = ((FunctionExpressionTree) functionTree).functionKeyword(); } else { token = ((ArrowFunctionTree) functionTree).doubleArrowToken(); } return token; }
public static Symbolizable.SymbolTable build(Symbolizable symbolizable, SymbolModel symbolModel, SourceFileOffsets sourceFileOffsets) { Symbolizable.SymbolTableBuilder builder = symbolizable.newSymbolTableBuilder(); for (Symbol symbol : symbolModel.getSymbols()) { if (!symbol.usages().isEmpty()){ List<Usage> usagesList = new LinkedList<>(symbol.usages()); InternalSyntaxToken token = (InternalSyntaxToken) (usagesList.get(0).identifierTree()).identifierToken(); org.sonar.api.source.Symbol reference = getHighlightedSymbol(sourceFileOffsets, builder, token); for (int i = 1; i < usagesList.size(); i++){ builder.newReference( reference, sourceFileOffsets.startOffset(getToken(usagesList.get(i).identifierTree())) ); } } } return builder.build(); }
private void visitSymbol(Symbol symbol) { if ("arguments".equals(symbol.name()) && symbol.external()) { return; } Scope scope = symbol.scope(); if (scope.outer() != null) { Symbol outerSymbol = scope.outer().lookupSymbol(symbol.name()); if (outerSymbol != null && !outerSymbol.external()) { IdentifierTree shadowedDeclaration = getDeclaration(outerSymbol).identifierTree(); String message = String.format(MESSAGE, symbol.name(), shadowedDeclaration.identifierToken().line()); raiseIssuesOnDeclarations(symbol, message, shadowedDeclaration); } } }
private void visitSymbol(Symbol symbol) { Usage firstDeclaration = null; for (Usage usage : symbol.usages()) { if (firstDeclaration == null) { if (usage.isDeclaration()) { firstDeclaration = usage; } } else if (usage.isDeclaration() && usage.kind() != Usage.Kind.LEXICAL_DECLARATION) { String message = String.format(MESSAGE, symbol.name(), firstDeclaration.identifierTree().identifierToken().line()); addIssue(usage.identifierTree(), message).secondary(firstDeclaration.identifierTree()); } } }