Skip to content

Commit

Permalink
Refactor 'corePrefix' to avoid name confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBakerEffendi committed Feb 12, 2025
1 parent 55bdcde commit 6055988
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
}

node.target match {
case regex @ StaticLiteral(s"${GlobalTypes.builtinPrefix}.${Defines.Regexp}") if node.isRegexMatch =>
case regex @ StaticLiteral(s"${GlobalTypes.`corePrefix`}.${Defines.Regexp}") if node.isRegexMatch =>
val loweredRegex = node.arguments.headOption match {
case Some(literal) => lowerRegexMatch(literal, regex, node.span)
case None =>
Expand Down Expand Up @@ -841,13 +841,13 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
(node.lowerBound, node.upperBound) match {
case (lb: StaticLiteral, ub: StaticLiteral) =>
(lb.typeFullName, ub.typeFullName) match {
case (s"${GlobalTypes.builtinPrefix}.Integer", s"${GlobalTypes.builtinPrefix}.Integer") =>
case (s"${GlobalTypes.`corePrefix`}.Integer", s"${GlobalTypes.`corePrefix`}.Integer") =>
generateRange(lb.span.text.toInt, ub.span.text.toInt, node.rangeOperator.exclusive)
.map(x =>
StaticLiteral(lb.typeFullName)(TextSpan(lb.line, lb.column, lb.lineEnd, lb.columnEnd, None, x.toString))
)
.toList
case (s"${GlobalTypes.builtinPrefix}.String", s"${GlobalTypes.builtinPrefix}.String") =>
case (s"${GlobalTypes.`corePrefix`}.String", s"${GlobalTypes.`corePrefix`}.String") =>
val lbVal = lb.span.text.replaceAll("['\"]", "")
val ubVal = ub.span.text.replaceAll("['\"]", "")

Expand Down Expand Up @@ -999,7 +999,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {

val argumentAst = node.arguments.map(astForMethodCallArgument)
val (dispatchType, methodFullName) =
if receiverType.startsWith(GlobalTypes.builtinPrefix) then (DispatchTypes.STATIC_DISPATCH, methodFullNameHint)
if receiverType.startsWith(GlobalTypes.corePrefix) then (DispatchTypes.STATIC_DISPATCH, methodFullNameHint)
else (DispatchTypes.DYNAMIC_DISPATCH, XDefines.DynamicCallUnknownFullName)

val call = callNode(node, code(node), methodName, methodFullName, dispatchType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ object RubyIntermediateAst {
final case class TypeIdentifier(typeFullName: String)(span: TextSpan)
extends RubyExpression(span)
with RubyIdentifier {
def isBuiltin: Boolean = typeFullName.startsWith(GlobalTypes.builtinPrefix)
def isBuiltin: Boolean = typeFullName.startsWith(GlobalTypes.corePrefix)
override def toString: String = s"TypeIdentifier(${span.text}, $typeFullName)"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package io.joern.rubysrc2cpg.datastructures

import better.files.File
import io.joern.rubysrc2cpg.passes.GlobalTypes
import io.joern.rubysrc2cpg.passes.GlobalTypes.builtinPrefix
import io.joern.rubysrc2cpg.passes.GlobalTypes.corePrefix
import io.joern.x2cpg.Defines
import io.joern.x2cpg.datastructures.{TypedScopeElement, *}
import io.joern.x2cpg.datastructures.*
import io.shiftleft.codepropertygraph.generated.NodeTypes
import io.shiftleft.codepropertygraph.generated.nodes.{DeclarationNew, NewLocal, NewMethodParameterIn}
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal
Expand All @@ -30,13 +30,13 @@ class RubyScope(summary: RubyProgramSummary, projectRoot: Option[String])
typesInScope.addAll(
Seq(
RubyType(
s"$builtinPrefix.Array",
List(RubyMethod("[]", List.empty, s"$builtinPrefix.Array", Option(s"$builtinPrefix.Array"))),
s"$corePrefix.Array",
List(RubyMethod("[]", List.empty, s"$corePrefix.Array", Option(s"$corePrefix.Array"))),
List.empty
),
RubyType(
s"$builtinPrefix.Hash",
List(RubyMethod("[]", List.empty, s"$builtinPrefix.Hash", Option(s"$builtinPrefix.Hash"))),
s"$corePrefix.Hash",
List(RubyMethod("[]", List.empty, s"$corePrefix.Hash", Option(s"$corePrefix.Hash"))),
List.empty
)
)
Expand Down Expand Up @@ -363,7 +363,7 @@ class RubyScope(summary: RubyProgramSummary, projectRoot: Option[String])
case None if GlobalTypes.kernelFunctions.contains(normalizedTypeName) =>
Option(RubyType(s"${GlobalTypes.kernelPrefix}.$normalizedTypeName", List.empty, List.empty))
case None if GlobalTypes.bundledClasses.contains(normalizedTypeName) =>
Option(RubyType(s"${GlobalTypes.builtinPrefix}.$normalizedTypeName", List.empty, List.empty))
Option(RubyType(s"${GlobalTypes.corePrefix}.$normalizedTypeName", List.empty, List.empty))
case None =>
None
case x => x
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.joern.rubysrc2cpg.parser

import io.joern.rubysrc2cpg.astcreation.RubyIntermediateAst.{RubyExpression, *}
import io.joern.rubysrc2cpg.astcreation.RubyIntermediateAst.*
import io.joern.rubysrc2cpg.parser.AstType.Send
import io.joern.rubysrc2cpg.parser.RubyJsonHelpers.*
import io.joern.rubysrc2cpg.passes.Defines
import io.joern.rubysrc2cpg.passes.Defines.{NilClass, RubyOperators}
import io.joern.rubysrc2cpg.passes.GlobalTypes.builtinPrefix
import io.joern.rubysrc2cpg.passes.GlobalTypes.corePrefix
import io.joern.rubysrc2cpg.utils.FreshNameGenerator
import io.joern.x2cpg.frontendspecific.rubysrc2cpg.ImportsPass
import io.joern.x2cpg.frontendspecific.rubysrc2cpg.ImportsPass.ImportCallNames
Expand Down Expand Up @@ -851,7 +851,7 @@ class RubyJsonToNodeCreator(
case Nil => RaiseCall(target, List.empty)(obj.toTextSpan)
case (argument: StaticLiteral) :: Nil =>
val simpleErrorId =
SimpleIdentifier(Option(s"$builtinPrefix.StandardError"))(argument.span.spanStart("StandardError"))
SimpleIdentifier(Option(s"$corePrefix.StandardError"))(argument.span.spanStart("StandardError"))
val implicitSimpleErrInst = SimpleObjectInstantiation(simpleErrorId, argument :: Nil)(
argument.span.spanStart(s"StandardError.new(${argument.text})")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Defines {
def getCoreType(typeInString: String): String = {
if (!GlobalTypes.bundledClasses.contains(typeInString))
logger.warn(s"Type '$typeInString' not considered a 'core' type")
s"${GlobalTypes.builtinPrefix}.$typeInString"
s"${GlobalTypes.corePrefix}.$typeInString"
}

object RubyOperators {
Expand All @@ -62,8 +62,8 @@ object Defines {

object GlobalTypes {
val Kernel = "Kernel"
val builtinPrefix = "__core"
val kernelPrefix = s"$builtinPrefix.$Kernel"
val corePrefix = "__core"
val kernelPrefix = s"$corePrefix.$Kernel"

/** Source: https://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/function.html
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.joern.rubysrc2cpg.passes

import io.joern.rubysrc2cpg.passes.Defines.Main
import io.joern.rubysrc2cpg.passes.GlobalTypes.{builtinPrefix, kernelPrefix}
import io.joern.rubysrc2cpg.passes.GlobalTypes.{corePrefix, kernelPrefix}
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.shiftleft.codepropertygraph.generated.nodes.Identifier
import io.shiftleft.semanticcpg.language.*
Expand Down Expand Up @@ -88,16 +88,16 @@ class RubyInternalTypeRecoveryTests extends RubyCode2CpgFixture(withPostProcessi
"propagate function return types" in {
inside(cpg.method.name("func2?").l) {
case func :: func2 :: Nil =>
func.methodReturn.typeFullName shouldBe s"$builtinPrefix.String"
func2.methodReturn.typeFullName shouldBe s"$builtinPrefix.String"
func.methodReturn.typeFullName shouldBe s"$corePrefix.String"
func2.methodReturn.typeFullName shouldBe s"$corePrefix.String"
case xs => fail(s"Expected 2 functions, got [${xs.name.mkString(",")}]")
}
}

"propagate return type to identifier c" in {
inside(cpg.identifier.name("c").l) {
case cIdent :: Nil =>
cIdent.typeFullName shouldBe s"$builtinPrefix.String"
cIdent.typeFullName shouldBe s"$corePrefix.String"
case xs => fail(s"Expected one identifier for c, got [${xs.name.mkString(",")}]")
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ class RubyInternalTypeRecoveryTests extends RubyCode2CpgFixture(withPostProcessi
case funcAssignment :: constructAssignment :: tmpAssignment :: Nil =>
inside(funcAssignment.argument.l) {
case (lhs: Identifier) :: rhs :: Nil =>
lhs.typeFullName shouldBe s"$builtinPrefix.String"
lhs.typeFullName shouldBe s"$corePrefix.String"
case xs => fail(s"Expected lhs and rhs, got [${xs.code.mkString(",")}] ")
}

Expand Down Expand Up @@ -256,9 +256,9 @@ class RubyExternalTypeRecoveryTests

"resolve 'x' and 'y' locally under foo.rb" in {
val Some(x) = cpg.identifier("x").where(_.file.name(".*foo.*")).headOption: @unchecked
x.typeFullName shouldBe s"$builtinPrefix.Integer"
x.typeFullName shouldBe s"$corePrefix.Integer"
val Some(y) = cpg.identifier("y").where(_.file.name(".*foo.*")).headOption: @unchecked
y.typeFullName shouldBe s"$builtinPrefix.String"
y.typeFullName shouldBe s"$corePrefix.String"
}

"resolve 'FooModule.x' and 'FooModule.y' field access primitive types correctly" in {
Expand All @@ -269,9 +269,9 @@ class RubyExternalTypeRecoveryTests
.name("z")
.l
z1.typeFullName shouldBe "ANY"
z1.dynamicTypeHintFullName shouldBe Seq(s"$builtinPrefix.Integer", s"$builtinPrefix.String")
z1.dynamicTypeHintFullName shouldBe Seq(s"$corePrefix.Integer", s"$corePrefix.String")
z2.typeFullName shouldBe "ANY"
z2.dynamicTypeHintFullName shouldBe Seq(s"$builtinPrefix.Integer", s"$builtinPrefix.String")
z2.dynamicTypeHintFullName shouldBe Seq(s"$corePrefix.Integer", s"$corePrefix.String")
}

"resolve 'FooModule.d' field access object types correctly" ignore {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.joern.rubysrc2cpg.querying

import io.joern.rubysrc2cpg.passes.Defines
import io.joern.rubysrc2cpg.passes.Defines.RubyOperators
import io.joern.rubysrc2cpg.passes.GlobalTypes.{builtinPrefix, kernelPrefix}
import io.joern.rubysrc2cpg.passes.GlobalTypes.{corePrefix, kernelPrefix}
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.joern.x2cpg.Defines as XDefines
import io.shiftleft.codepropertygraph.generated.nodes.{Block, Call, Identifier, Literal, Local}
Expand Down Expand Up @@ -99,7 +99,7 @@ class ArrayTests extends RubyCode2CpgFixture {

inside(asgns.map(_.source)) { case (foo: Literal) :: Nil =>
foo.code shouldBe "foo"
foo.typeFullName shouldBe s"$builtinPrefix.String"
foo.typeFullName shouldBe s"$corePrefix.String"
}
}

Expand All @@ -116,10 +116,10 @@ class ArrayTests extends RubyCode2CpgFixture {

inside(asgns.map(_.source)) { case (x: Literal) :: (y: Literal) :: Nil =>
x.code shouldBe ":x"
x.typeFullName shouldBe s"$builtinPrefix.Symbol"
x.typeFullName shouldBe s"$corePrefix.Symbol"

y.code shouldBe ":y"
y.typeFullName shouldBe s"$builtinPrefix.Symbol"
y.typeFullName shouldBe s"$corePrefix.Symbol"
}
}

Expand Down Expand Up @@ -159,7 +159,7 @@ class ArrayTests extends RubyCode2CpgFixture {
yFmtStrLit.code shouldBe "23"

zLit.code shouldBe "z"
zLit.typeFullName shouldBe s"$builtinPrefix.String"
zLit.typeFullName shouldBe s"$corePrefix.String"
}
}

Expand All @@ -171,8 +171,8 @@ class ArrayTests extends RubyCode2CpgFixture {
inside(cpg.call.nameExact("[]").l) {
case bracketCall :: Nil =>
bracketCall.name shouldBe "[]"
bracketCall.methodFullName shouldBe s"$builtinPrefix.Array.[]"
bracketCall.typeFullName shouldBe s"$builtinPrefix.Array"
bracketCall.methodFullName shouldBe s"$corePrefix.Array.[]"
bracketCall.typeFullName shouldBe s"$corePrefix.Array"

inside(bracketCall.argument.l) {
case _ :: one :: two :: three :: Nil =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ class CallTests extends RubyCode2CpgFixture(withPostProcessing = true) {
val List(atan2) = cpg.call.name("atan2").l
atan2.lineNumber shouldBe Some(3)
atan2.code shouldBe "Math.atan2(1, 1)"
atan2.methodFullName shouldBe s"${GlobalTypes.builtinPrefix}.Math.atan2"
atan2.methodFullName shouldBe s"${GlobalTypes.corePrefix}.Math.atan2"
atan2.dispatchType shouldBe DispatchTypes.DYNAMIC_DISPATCH

val List(mathRec: Call) = atan2.receiver.l: @unchecked
mathRec.argumentIndex shouldBe -1
mathRec.typeFullName shouldBe Defines.Any
mathRec.code shouldBe s"Math.atan2"

mathRec.argument(1).asInstanceOf[TypeRef].typeFullName shouldBe s"${GlobalTypes.builtinPrefix}.Math"
mathRec.argument(1).asInstanceOf[TypeRef].typeFullName shouldBe s"${GlobalTypes.corePrefix}.Math"
mathRec.argument(2).asInstanceOf[FieldIdentifier].canonicalName shouldBe "atan2"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ class ClassTests extends RubyCode2CpgFixture {

"create the `StandardError` local variable" in {
cpg.local.nameExact("some_variable").dynamicTypeHintFullName.toList shouldBe List(
s"${GlobalTypes.builtinPrefix}.StandardError"
s"${GlobalTypes.corePrefix}.StandardError"
)
}

Expand Down Expand Up @@ -962,7 +962,7 @@ class ClassTests extends RubyCode2CpgFixture {
inside(bodyMethod.block.astChildren.l) {
case (one: Literal) :: Nil =>
one.code shouldBe "1"
one.typeFullName shouldBe s"${GlobalTypes.builtinPrefix}.Integer"
one.typeFullName shouldBe s"${GlobalTypes.corePrefix}.Integer"
case xs => fail(s"Expected one literal, got [${xs.code.mkString(",")}]")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.joern.rubysrc2cpg.querying

import io.joern.rubysrc2cpg.passes.Defines.{Initialize, Main, RubyOperators}
import io.joern.rubysrc2cpg.passes.GlobalTypes.builtinPrefix
import io.joern.rubysrc2cpg.passes.GlobalTypes.corePrefix
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.joern.x2cpg.Defines
import io.shiftleft.codepropertygraph.generated.Operators
Expand Down Expand Up @@ -273,7 +273,7 @@ class DoBlockTests extends RubyCode2CpgFixture {

newCall.name shouldBe Initialize
newCall.methodFullName shouldBe Defines.DynamicCallUnknownFullName
newCall.dynamicTypeHintFullName should contain(s"$builtinPrefix.Array.$Initialize")
newCall.dynamicTypeHintFullName should contain(s"$corePrefix.Array.$Initialize")

inside(newCall.argument.l) {
case (_: Identifier) :: (x: Identifier) :: (closure: TypeRef) :: Nil =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.joern.rubysrc2cpg.querying

import io.joern.rubysrc2cpg.passes.Defines.RubyOperators
import io.joern.rubysrc2cpg.passes.GlobalTypes.builtinPrefix
import io.joern.rubysrc2cpg.passes.GlobalTypes.corePrefix
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.shiftleft.codepropertygraph.generated.Operators
import io.shiftleft.codepropertygraph.generated.nodes.{Call, Identifier, Literal, TypeRef}
Expand Down Expand Up @@ -101,7 +101,7 @@ class HashTests extends RubyCode2CpgFixture {
lhs.name shouldBe Operators.indexAccess

rhs.code shouldBe "\"abc\""
rhs.typeFullName shouldBe s"$builtinPrefix.String"
rhs.typeFullName shouldBe s"$corePrefix.String"
case _ => fail("Expected LHS and RHS after lowering")
}

Expand All @@ -110,7 +110,7 @@ class HashTests extends RubyCode2CpgFixture {
lhs.name shouldBe Operators.indexAccess

rhs.code shouldBe "\"ade\""
rhs.typeFullName shouldBe s"$builtinPrefix.String"
rhs.typeFullName shouldBe s"$corePrefix.String"
}
case _ => fail("Expected 5 calls (one per item in range)")
}
Expand All @@ -127,7 +127,7 @@ class HashTests extends RubyCode2CpgFixture {
lhs.name shouldBe Operators.indexAccess

rhs.code shouldBe "\"abc\""
rhs.typeFullName shouldBe s"$builtinPrefix.String"
rhs.typeFullName shouldBe s"$corePrefix.String"
case _ => fail("Expected LHS and RHS after lowering")
}
case _ => fail("Expected 3 calls (one per item in range)")
Expand Down Expand Up @@ -175,7 +175,7 @@ class HashTests extends RubyCode2CpgFixture {
case _ => fail("Expected range operator for non-primitive range key")
}

rhs.typeFullName shouldBe s"$builtinPrefix.String"
rhs.typeFullName shouldBe s"$corePrefix.String"
rhs.code shouldBe "\"a\""
case _ => fail("Expected LHS and RHS for association")
}
Expand All @@ -195,8 +195,8 @@ class HashTests extends RubyCode2CpgFixture {
case hashCall :: Nil =>
hashCall.code shouldBe "Hash [1 => \"a\", 2 => \"b\", 3 => \"c\"]"
hashCall.lineNumber shouldBe Some(2)
hashCall.methodFullName shouldBe s"$builtinPrefix.Hash.[]"
hashCall.typeFullName shouldBe s"$builtinPrefix.Hash"
hashCall.methodFullName shouldBe s"$corePrefix.Hash.[]"
hashCall.typeFullName shouldBe s"$corePrefix.Hash"

inside(hashCall.astChildren.l) {
case (_: Call) :: (_: TypeRef) :: (one: Call) :: (two: Call) :: (three: Call) :: Nil =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.joern.rubysrc2cpg.querying

import io.joern.rubysrc2cpg.passes.GlobalTypes.builtinPrefix
import io.joern.rubysrc2cpg.passes.GlobalTypes.corePrefix
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.semanticcpg.language.*
Expand All @@ -26,7 +26,7 @@ class HereDocTests extends RubyCode2CpgFixture {
localAst.code shouldBe "a"
callAst.code shouldBe "a = 10"

literalAst.typeFullName shouldBe s"$builtinPrefix.String"
literalAst.typeFullName shouldBe s"$corePrefix.String"

returnAst.code shouldBe "a"
case _ =>
Expand Down Expand Up @@ -55,7 +55,7 @@ class HereDocTests extends RubyCode2CpgFixture {
inside(assignmentCall.argument.l) {
case lhsArg :: (rhsArg: Literal) :: Nil =>
lhsArg.code shouldBe "a"
rhsArg.typeFullName shouldBe s"$builtinPrefix.String"
rhsArg.typeFullName shouldBe s"$corePrefix.String"
case _ => fail("Expected LHS and RHS for assignment")
}
case _ => fail("Expected call for assignment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.joern.rubysrc2cpg.querying

import io.joern.rubysrc2cpg.passes.Defines
import io.joern.rubysrc2cpg.passes.Defines.{Initialize, Main}
import io.joern.rubysrc2cpg.passes.GlobalTypes.{builtinPrefix, kernelPrefix}
import io.joern.rubysrc2cpg.passes.GlobalTypes.{corePrefix, kernelPrefix}
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.joern.x2cpg.frontendspecific.rubysrc2cpg.{ImplicitRequirePass, ImportsPass, TypeImportInfo}
import io.shiftleft.codepropertygraph.generated.DispatchTypes
Expand Down
Loading

0 comments on commit 6055988

Please sign in to comment.