|
| 1 | +/* |
| 2 | + * creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs |
| 3 | + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.greencodeinitiative.creedengo.python.utils; |
| 19 | + |
| 20 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 21 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 22 | +import org.sonar.plugins.python.api.tree.*; |
| 23 | + |
| 24 | +import javax.annotation.CheckForNull; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +public class UtilsAST { |
| 29 | + |
| 30 | + private UtilsAST() { |
| 31 | + // Utility class - prevent instantiation |
| 32 | + } |
| 33 | + |
| 34 | + public static List<Argument> getArgumentsFromCall(CallExpression callExpression) { |
| 35 | + return Optional.ofNullable(callExpression) |
| 36 | + .map(CallExpression::argumentList) |
| 37 | + .map(ArgList::arguments) |
| 38 | + .orElse(List.of()); |
| 39 | + } |
| 40 | + |
| 41 | + public static String getMethodName(CallExpression callExpression) { |
| 42 | + return Optional.ofNullable(callExpression) |
| 43 | + .map(CallExpression::calleeSymbol) |
| 44 | + .map(Symbol::name) |
| 45 | + .orElse(""); |
| 46 | + } |
| 47 | + |
| 48 | + public static String getQualifiedName(CallExpression callExpression) { |
| 49 | + return Optional.ofNullable(callExpression) |
| 50 | + .map(CallExpression::calleeSymbol) |
| 51 | + .map(Symbol::fullyQualifiedName) |
| 52 | + .orElse(""); |
| 53 | + } |
| 54 | + |
| 55 | + public static String getVariableName(SubscriptionContext context) { |
| 56 | + |
| 57 | + if (context == null || context.syntaxNode() == null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + Tree current = context.syntaxNode(); |
| 62 | + while (current != null && !current.is(Tree.Kind.ASSIGNMENT_STMT)) { |
| 63 | + current = current.parent(); |
| 64 | + } |
| 65 | + if (current != null) { |
| 66 | + AssignmentStatement assignment = (AssignmentStatement) current; |
| 67 | + if (hasNonEmptyLhsExpressions(assignment)) { |
| 68 | + Expression leftExpr = assignment.lhsExpressions().get(0).expressions().get(0); |
| 69 | + if (leftExpr.is(Tree.Kind.NAME)) { |
| 70 | + Name variableName = (Name) leftExpr; |
| 71 | + return variableName.name(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + @CheckForNull |
| 80 | + public static RegularArgument nthArgumentOrKeyword(int argPosition, String keyword, List<Argument> arguments) { |
| 81 | + return arguments.stream() |
| 82 | + .filter(argument -> hasKeyword(argument, keyword) || |
| 83 | + (argument.is(Tree.Kind.REGULAR_ARGUMENT) |
| 84 | + && ((RegularArgument) argument).keywordArgument() == null |
| 85 | + && arguments.indexOf(argument) == argPosition)) |
| 86 | + .map(RegularArgument.class::cast) |
| 87 | + .findFirst() |
| 88 | + .orElse(null); |
| 89 | + } |
| 90 | + |
| 91 | + private static boolean hasKeyword(Argument argument, String keyword) { |
| 92 | + return argument instanceof RegularArgument regularArgument && |
| 93 | + Optional.ofNullable(regularArgument.keywordArgument()) |
| 94 | + .map(Name::name) |
| 95 | + .filter(name -> name.equals(keyword)) |
| 96 | + .isPresent(); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean hasNonEmptyLhsExpressions(AssignmentStatement assignment ) { |
| 100 | + return Optional.ofNullable(assignment.lhsExpressions()) |
| 101 | + .filter(lhs -> !lhs.isEmpty()) |
| 102 | + .map(lhs -> lhs.get(0).expressions()) |
| 103 | + .filter(exprLst -> !exprLst.isEmpty()) |
| 104 | + .isPresent(); |
| 105 | + } |
| 106 | +} |
0 commit comments