From 7dfb555a4175123524018333f8da81e64d558fa3 Mon Sep 17 00:00:00 2001 From: Gang ZHANG Date: Wed, 14 Sep 2022 08:45:48 +0800 Subject: [PATCH] fix: ruby line counter should start from 1 instead of 0 --- .../extractor/ruby/jruby/JRubyVisitor.java | 47 ++++++++++--------- .../variable_assignment_dasgn.rb | 19 ++++++++ 2 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 src/test/resources/ruby-code-examples/variable_assignment_dasgn.rb diff --git a/src/main/java/depends/extractor/ruby/jruby/JRubyVisitor.java b/src/main/java/depends/extractor/ruby/jruby/JRubyVisitor.java index 032ab04..052b755 100644 --- a/src/main/java/depends/extractor/ruby/jruby/JRubyVisitor.java +++ b/src/main/java/depends/extractor/ruby/jruby/JRubyVisitor.java @@ -59,7 +59,7 @@ public class JRubyVisitor extends NoopVisitor { @Override public Object visitModuleNode(ModuleNode node) { String name = helper.getName(node.getCPath()); - context.foundNamespace(name,node.getPosition().getStartLine()); + context.foundNamespace(name,node.getPosition().getStartLine()+1); super.visitModuleNode(node); context.exitLastedEntity(); return null; @@ -67,7 +67,7 @@ public class JRubyVisitor extends NoopVisitor { @Override public Object visitClassNode(ClassNode node) { - TypeEntity type = context.foundNewType(helper.getName(node.getCPath()),node.getPosition().getStartLine()); + TypeEntity type = context.foundNewType(helper.getName(node.getCPath()),node.getPosition().getStartLine()+1); Node superNode = node.getSuper(); if (superNode instanceof ConstNode || @@ -96,7 +96,7 @@ public class JRubyVisitor extends NoopVisitor { public Object visitFCallNode(FCallNode node) { String fname = helper.getName(node); Collection params = getParams(node); - context.processSpecialFuncCall(fname, params, node.getPosition().getStartLine()); + context.processSpecialFuncCall(fname, params, node.getPosition().getStartLine()+1); return super.visitFCallNode(node); } @@ -121,7 +121,7 @@ public class JRubyVisitor extends NoopVisitor { String fname = helper.getName(node); Collection params = getParams(node); addCallToReceiverVar(node, fname); - context.processSpecialFuncCall(fname, params, node.getPosition().getStartLine()); + context.processSpecialFuncCall(fname, params, node.getPosition().getStartLine()+1); return super.visitCallNode(node); } @@ -142,7 +142,7 @@ public class JRubyVisitor extends NoopVisitor { public Object visitUnaryCallNode(UnaryCallNode node) { String fname = helper.getName(node); Collection params = new ArrayList<>(); - context.processSpecialFuncCall(fname, params, node.getPosition().getStartLine()); + context.processSpecialFuncCall(fname, params, node.getPosition().getStartLine()+1); return super.visitUnaryCallNode(node); } @@ -156,8 +156,8 @@ public class JRubyVisitor extends NoopVisitor { @Override public Object visitDefnNode(DefnNode node) { - FunctionEntity method = context.foundMethodDeclarator(node.getName(),node.getPosition().getStartLine()); - method.setLine(node.getPosition().getStartLine()); + FunctionEntity method = context.foundMethodDeclarator(node.getName(),node.getPosition().getStartLine()+1); + method.setLine(node.getPosition().getStartLine()+1); super.visitDefnNode(node); context.exitLastedEntity(); @@ -174,8 +174,8 @@ public class JRubyVisitor extends NoopVisitor { String className = ((INameNode) varNode).getName(); Entity entity = context.foundEntityWithName(GenericName.build(className)); if (entity != null && entity instanceof ContainerEntity) { - FunctionEntity method = context.foundMethodDeclarator(((ContainerEntity) entity), node.getName(),node.getPosition().getStartLine()); - method.setLine(node.getPosition().getStartLine()); + FunctionEntity method = context.foundMethodDeclarator(((ContainerEntity) entity), node.getName(),node.getPosition().getStartLine()+1); + method.setLine(node.getPosition().getStartLine()+1); handled = true; } @@ -183,16 +183,16 @@ public class JRubyVisitor extends NoopVisitor { String varName = ((INameNode) varNode).getName(); Entity var = context.foundEntityWithName(GenericName.build(varName)); if (var != null && var instanceof ContainerEntity) { - FunctionEntity method = context.foundMethodDeclarator(((ContainerEntity) var), node.getName(),node.getPosition().getStartLine()); - method.setLine(node.getPosition().getStartLine()); + FunctionEntity method = context.foundMethodDeclarator(((ContainerEntity) var), node.getName(),node.getPosition().getStartLine()+1); + method.setLine(node.getPosition().getStartLine()+1); handled = true; } } if (!handled) { // fallback to add it to last container - FunctionEntity method = context.foundMethodDeclarator(node.getName(),node.getPosition().getStartLine()); - method.setLine(node.getPosition().getStartLine()); + FunctionEntity method = context.foundMethodDeclarator(node.getName(),node.getPosition().getStartLine()+1); + method.setLine(node.getPosition().getStartLine()+1); } super.visitDefsNode(node); context.exitLastedEntity(); @@ -201,31 +201,31 @@ public class JRubyVisitor extends NoopVisitor { @Override public Object visitGlobalVarNode(GlobalVarNode node) { - context.foundVarDefinition(context.globalScope(), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(context.globalScope(), node.getName(),node.getPosition().getStartLine()+1); return super.visitGlobalVarNode(node); } @Override public Object visitInstVarNode(InstVarNode node) { - context.foundVarDefinition(context.currentType(), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(context.currentType(), node.getName(),node.getPosition().getStartLine()+1); return super.visitInstVarNode(node); } @Override public Object visitClassVarAsgnNode(ClassVarAsgnNode node) { - context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()+1); return super.visitClassVarAsgnNode(node); } @Override public Object visitClassVarDeclNode(ClassVarDeclNode node) { - context.foundVarDefinition(context.currentType(), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(context.currentType(), node.getName(),node.getPosition().getStartLine()+1); return super.visitClassVarDeclNode(node); } @Override public Object visitClassVarNode(ClassVarNode node) { - context.foundVarDefinition(context.currentType(), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(context.currentType(), node.getName(),node.getPosition().getStartLine()+1); return super.visitClassVarNode(node); } @@ -236,25 +236,26 @@ public class JRubyVisitor extends NoopVisitor { @Override public Object visitDVarNode(DVarNode node) { - context.foundVarDefinition(context.lastContainer(), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(context.lastContainer(), node.getName(),node.getPosition().getStartLine()+1); return super.visitDVarNode(node); } @Override public Object visitDAsgnNode(DAsgnNode node) { - context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()+1); + expressionUsage.foundExpression(node.getValue()); return super.visitDAsgnNode(node); } @Override public Object visitGlobalAsgnNode(GlobalAsgnNode node) { - context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()+1); return super.visitGlobalAsgnNode(node); } @Override public Object visitInstAsgnNode(InstAsgnNode node) { - context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()+1); return super.visitInstAsgnNode(node); } @@ -267,7 +268,7 @@ public class JRubyVisitor extends NoopVisitor { @Override public Object visitLocalAsgnNode(LocalAsgnNode node) { - context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()); + context.foundVarDefinition(helper.getScopeOfVar(node,context), node.getName(),node.getPosition().getStartLine()+1); return super.visitLocalAsgnNode(node); } diff --git a/src/test/resources/ruby-code-examples/variable_assignment_dasgn.rb b/src/test/resources/ruby-code-examples/variable_assignment_dasgn.rb new file mode 100644 index 0000000..4a307e5 --- /dev/null +++ b/src/test/resources/ruby-code-examples/variable_assignment_dasgn.rb @@ -0,0 +1,19 @@ +class Class + def test + var_int = 1 + var_c = Class.new + @@inst_var = Class.new + @class_var = Class.new + end + + def operator_is_call + var_compose = "c" + 1 #ruby treat '+' as call + var_1 = true + var_2 = true && false + var_3 = !true + var_4 = true and false + var_5 = 3 < 2 + var_6 = 3 << 2 + var_7 <<= 3; + end +end