Home Developer Scala.meta: A quick look

Scala.meta: A quick look

Author

Date

Category

At Codacy we don’t only rely on external tools to find code patterns.Β  Sometimes we employ other resources like scala.meta.

With Scala we also have a nice repository of homemade patterns. Therefore we need to be able to process Scala source code. And by processing we mainly mean transforming source code into Abstract Syntax Trees (ASTs).

Everything you find here you can try yourself in the repl. Be sure to add the following sbt resolver and dependencies

https://gist.github.com/johannegger/3226587d5ce3e66a8ad1

(Not yet deprecated) scala.reflect

Up until now we used the standard scala.reflect.api and the toolbox it provides. To parse scala code with help of the reflection toolbox we need to run the following:

https://gist.github.com/johannegger/ecc4c5a89f9363764046

The resulting tree variable is of type toolbox.u.Tree and this is the structure we use(d) to find problematic code. That doesn’t look too complicated you might say. Well you are right but the problems start here. Unfortunately those Trees are very complex (after all they represent the complete AST of your program). Also documentation is kinda poorβ€Šβ€”β€Šas writer of patterns I had to rely mainly on Scaladoc and partly Reflection.

Sidenote: Real lifeΒ .scala files usually reside in packages which means a typical sourcefile will start with package foo … The current implementation of the toolbox however is not able to parse such files. We were forced to implement a workaround that preyed on my mind ever since.

All of this doesn’t really make it easy for someone who doesn’t know the internals of scala.reflect.api to start writing patterns from scratch. But that’s exactly what we want. We want to make it possible for our users to write custom patterns for their issues.

scala.meta to theΒ rescue

Recently we started a very promising collaboration with Eugene Burmako and Mathieu Demarne from scala.meta. scala.meta was created to simplify metaprogramming in scala and will hopefully be the successor of the current scala.reflect. For us it already is. Let’s try to get a scala.meta.Tree

https://gist.github.com/johannegger/6467eae9b1feb1be0e2b

And that’s it. (Now try parsing a properΒ .scala file containing a package definitionβ€Šβ€”β€Šspoiler alertΒ : it works! ) But that’s not all, in scala.meta.tql we find a neat tree query language that helps us writing patterns. Let’s try to implement a pattern that finds occurrence of try-catch blocks. (On codacy you can read why you shouldn’t use them)

https://gist.github.com/johannegger/051a6c125b00c412951f

We can now pass trees to the tryMatcher, let’s just try a simple test code:

https://gist.github.com/johannegger/c15090d300eea1f49365

Et voilaβ€Šβ€”β€Šthe repl doesn’t lieβ€Šβ€”β€Šβ€œfound culprit at line 0”

Stay tuned about the future of scala.meta at Codacy.


Edit: We just published an ebook: β€œThe Ultimate Guide to Code Review” based on a survey of 680+ developers. Enjoy!


About Codacy

Codacy is used by thousands of developers to analyze billions of lines of code every day!

Getting started is easy – and free! Just use yourΒ Β GitHub, Bitbucket or Google account toΒ sign up.

GET STARTED

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

How to implement coding standards in your organization

Coding standards are a set of guidelines and best practices designed to ensure that your code is consistent and readable, making...

Leading Your Team to Engineering Excellence

On March 7th, we did a webinar called Leading Your Team to Engineering Excellence. Guest speaker Steve Berczuk, Lead Software Engineer...

3 popular C# style guides that will help your team write better code

C# is a popular programming language developed by Microsoft, and you can use it for developing web applications, games, and more....

February Product Update πŸš€

Hi there πŸ‘‹, Here are a few updates from February with very exciting news, so keep on...

Velocity vs. Cycle Time: understanding the key differences

Velocity and Cycle time are two standard metrics to measure the efficiency and effectiveness of software development teams. They help you...