Permalink
Browse files

Improved tree traversal

This fixes four issues

- The code used super.traverse to recurse the trees. This was wrong
  as it would skip one 'layer' of nodes.
- Traverse the argument list of DefDef's
- Traverse annotations on DefDef's
- Store occurrences in ValDefs

So it turned out that the failure that was seen with the string
interpolation test had nothing to do with string interpolation.

Originally it only found one occurrence of `x` in the following
example.

  def foo(x: String) = s"Hi there, ${x}"

I assumed that the `x` that was missing was the one inside of the
string interpolation, but actually it was the argument. Ups.

Fix #1001621
  • Loading branch information...
1 parent ba7e61f commit e9b0883d0354afbbd2a2844fcfdd24393c8235e6 @mads-hartmann mads-hartmann committed Apr 5, 2013
@@ -68,5 +68,18 @@ class OccurrenceCollectorTest {
}
}
+ @Test def stringInterpolation() {
+ doWithOccurrencesInUnit("org","example","StringInterpolation.scala") { occurrences =>
+ val x = occurrenceFor("x", occurrences)
+ assertEquals("Should be 2 occurrences of x %s".format(x), 2, x.size)
+ }
+ }
+
+ @Test def annotationsOnMethods() {
+ doWithOccurrencesInUnit("org","example", "Annotations.scala") { occurrences =>
+ val x = occurrenceFor("IOException", occurrences)
+ assertEquals("Should be 1 occurrences of IOException %s".format(x), 1, x.size)
+ }
+ }
}
@@ -0,0 +1,7 @@
+package org.example
+
+import java.io.IOException
+
+object Test {
+ @throws(classOf[IOException]) def test() = {}
+}
@@ -0,0 +1,5 @@
+package org.example
+
+object StringInterpolation {
+ def foo(x: String) = s"Hi there, ${x}"
+}
@@ -39,14 +39,23 @@ object OccurrenceCollector extends HasLogger {
case Select(rest,name) if !isSynthetic(pc)(t, name.toString) =>
occurrences += Occurrence(name.toString, file, t.pos.point, Reference)
- super.traverse(rest) // recurse in the case of chained selects: foo.baz.bar
+ traverse(rest) // recurse in the case of chained selects: foo.baz.bar
// Method definitions
- case DefDef(_, name, _, _, _, body) if !isSynthetic(pc)(t, name.toString) =>
+ case DefDef(mods, name, _, args, _, body) if !isSynthetic(pc)(t, name.toString) =>
occurrences += Occurrence(name.toString, file, t.pos.point, Declaration)
- super.traverse(body)
+ traverseTrees(mods.annotations)
+ traverseTreess(args)
+ traverse(body)
- case _ => super.traverse(t)
+ // Val's and arguments.
+ case ValDef(_, name, tpt, rhs) =>
+ occurrences += Occurrence(name.toString, file, t.pos.point, Declaration)
+ traverse(tpt)
+ traverse(rhs)
+
+ case _ =>
+ super.traverse(t)
}
}
}

0 comments on commit e9b0883

Please sign in to comment.