Skip to content

Commit

Permalink
Fix instanceof (P8) to find lines of binary operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
aravij committed Jul 20, 2020
1 parent fec72da commit 565df56
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions aibolit/patterns/instanceof/instance_of.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from typing import List
from typing import List, Optional

from aibolit.ast_framework import AST, ASTNodeType
from aibolit.ast_framework import AST, ASTNode, ASTNodeType
from aibolit.utils.ast_builder import build_ast


Expand All @@ -36,10 +36,23 @@ def value(self, filename: str):
lines: List[int] = []
for binary_operator in ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION):
if binary_operator.operator == 'instanceof':
lines.append(binary_operator.line)
operator_line = self._get_binary_operator_line(binary_operator)
if operator_line:
lines.append(operator_line)

for method_invocation in ast.get_proxy_nodes(ASTNodeType.METHOD_INVOCATION):
if method_invocation.member == 'isInstance':
lines.append(method_invocation.line)

return lines

def _get_binary_operator_line(self, binary_operator_node: ASTNode) -> Optional[int]:
assert binary_operator_node.node_type == ASTNodeType.BINARY_OPERATION
if binary_operator_node.line is not None:
return binary_operator_node.lines

if binary_operator_node.operandl.line is not None:
return binary_operator_node.operandl.line

# TODO: log that operator line was not found
return None

0 comments on commit 565df56

Please sign in to comment.