From af77456a343e71fb7acfba316487f5d3c484c4b0 Mon Sep 17 00:00:00 2001 From: Karan Batavia Date: Fri, 28 Jul 2023 15:08:52 +0530 Subject: [PATCH 1/2] added wsOrNl* before and after parameters --- .../io/joern/rubysrc2cpg/parser/RubyParser.g4 | 2 +- .../parser/MethodDefinitionTests.scala | 180 ++++++++++++++++++ 2 files changed, 181 insertions(+), 1 deletion(-) diff --git a/joern-cli/frontends/rubysrc2cpg/src/main/antlr4/io/joern/rubysrc2cpg/parser/RubyParser.g4 b/joern-cli/frontends/rubysrc2cpg/src/main/antlr4/io/joern/rubysrc2cpg/parser/RubyParser.g4 index ae8554bbbf73..b42150438904 100644 --- a/joern-cli/frontends/rubysrc2cpg/src/main/antlr4/io/joern/rubysrc2cpg/parser/RubyParser.g4 +++ b/joern-cli/frontends/rubysrc2cpg/src/main/antlr4/io/joern/rubysrc2cpg/parser/RubyParser.g4 @@ -335,7 +335,7 @@ methodOnlyIdentifier ; methodParameterPart - : LPAREN parameters? RPAREN + : LPAREN wsOrNl* parameters? wsOrNl* RPAREN | parameters? ; diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala index b7ac8e76129f..0c28538d8b88 100644 --- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala +++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala @@ -744,6 +744,186 @@ class MethodDefinitionTests extends RubyParserAbstractTest { | Separator | end""".stripMargin } + + "have correct structure when a method parameter is defined using whitespace" in { + val code = + """ + |class SampleClass + | def sample_method( first_param:, second_param:) + | end + |end + |""".stripMargin + + printAst(_.primary(), code) shouldBe + """ClassDefinitionPrimary + | ClassDefinition + | class + | WsOrNl + | ClassOrModuleReference + | SampleClass + | Separators + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | Statements + | ExpressionOrCommandStatement + | ExpressionExpressionOrCommand + | PrimaryExpression + | MethodDefinitionPrimary + | MethodDefinition + | def + | WsOrNl + | SimpleMethodNamePart + | DefinedMethodName + | MethodName + | MethodIdentifier + | sample_method + | MethodParameterPart + | ( + | WsOrNl + | Parameters + | Parameter + | KeywordParameter + | first_param + | : + | , + | WsOrNl + | Parameter + | KeywordParameter + | second_param + | : + | ) + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | end + | Separators + | Separator + | end""".stripMargin + } + + "have correct structure when method parameters are defined using new line" in { + val code = + """ + |class SomeClass + | def initialize( + | name, age) + | end + |end + |""".stripMargin + + printAst(_.primary(), code) shouldBe + """ClassDefinitionPrimary + | ClassDefinition + | class + | WsOrNl + | ClassOrModuleReference + | SomeClass + | Separators + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | Statements + | ExpressionOrCommandStatement + | ExpressionExpressionOrCommand + | PrimaryExpression + | MethodDefinitionPrimary + | MethodDefinition + | def + | WsOrNl + | SimpleMethodNamePart + | DefinedMethodName + | MethodName + | MethodIdentifier + | initialize + | MethodParameterPart + | ( + | WsOrNl + | WsOrNl + | Parameters + | Parameter + | MandatoryParameter + | name + | , + | WsOrNl + | Parameter + | MandatoryParameter + | age + | ) + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | end + | Separators + | Separator + | end""".stripMargin + } + + "have correct structure when method parameters are defined using wsOrNL" in { + val code = + """ + |class SomeClass + | def initialize( + | name, age + | ) + | end + |end + |""".stripMargin + + printAst(_.primary(), code) shouldBe + """ClassDefinitionPrimary + | ClassDefinition + | class + | WsOrNl + | ClassOrModuleReference + | SomeClass + | Separators + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | Statements + | ExpressionOrCommandStatement + | ExpressionExpressionOrCommand + | PrimaryExpression + | MethodDefinitionPrimary + | MethodDefinition + | def + | WsOrNl + | SimpleMethodNamePart + | DefinedMethodName + | MethodName + | MethodIdentifier + | initialize + | MethodParameterPart + | ( + | WsOrNl + | WsOrNl + | Parameters + | Parameter + | MandatoryParameter + | name + | , + | WsOrNl + | Parameter + | MandatoryParameter + | age + | WsOrNl + | WsOrNl + | ) + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | end + | Separators + | Separator + | end""".stripMargin + } } } From d6974853f312ce1cf0ebe783de877909e97aeb48 Mon Sep 17 00:00:00 2001 From: Karan Batavia Date: Fri, 28 Jul 2023 15:30:05 +0530 Subject: [PATCH 2/2] added one more test covering a use-case --- .../parser/MethodDefinitionTests.scala | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala index 0c28538d8b88..3b49952306d6 100644 --- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala +++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/MethodDefinitionTests.scala @@ -924,6 +924,75 @@ class MethodDefinitionTests extends RubyParserAbstractTest { | Separator | end""".stripMargin } + + "have correct structure when keyword parameters are defined using wsOrNL" in { + val code = + """ + |class SomeClass + | def initialize( + | name: nil, age + | ) + | end + |end + |""".stripMargin + + printAst(_.primary(), code) shouldBe + """ClassDefinitionPrimary + | ClassDefinition + | class + | WsOrNl + | ClassOrModuleReference + | SomeClass + | Separators + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | Statements + | ExpressionOrCommandStatement + | ExpressionExpressionOrCommand + | PrimaryExpression + | MethodDefinitionPrimary + | MethodDefinition + | def + | WsOrNl + | SimpleMethodNamePart + | DefinedMethodName + | MethodName + | MethodIdentifier + | initialize + | MethodParameterPart + | ( + | WsOrNl + | WsOrNl + | Parameters + | Parameter + | KeywordParameter + | name + | : + | WsOrNl + | PrimaryExpression + | VariableReferencePrimary + | PseudoVariableIdentifierVariableReference + | NilPseudoVariableIdentifier + | nil + | , + | WsOrNl + | Parameter + | MandatoryParameter + | age + | WsOrNl + | WsOrNl + | ) + | Separator + | WsOrNl + | BodyStatement + | CompoundStatement + | end + | Separators + | Separator + | end""".stripMargin + } } }