Subscribed unsubscribe Subscribe Subscribe

枕を欹てて聴く

香炉峰の雪は簾を撥げて看る

Introducing ibrik - code coverage tool for CoffeeScript by using istanbul

ECMAScript CoffeeScript AST

ibrikImage: © lorises 2010

Istanbul made by Krishnan Anantheswaran from the Yahoo! Cocktail team is great code coverage tool for JavaScript. (see usage by Ariya Hidayat)

Internally it uses Esprima to parse JavaScript code and Escodegen to generate instrumented JavaScript code.

istanbul architecture

These modules use Mozilla JavaScript AST as Internal Representation. Therefore, if we prepare Mozilla JavaScript AST, istanbul can generate instrumented code and coverage result.

I've created ibrik, the code coverage tool for CoffeeScript by using istanbul and CoffeeScriptRedux compiler.

We can install it by

npm install -g ibrik

write test.coffee

call = ->
  console.log 'this is function'

a = 200

if no
  call()

if yes
  do -> console.log('hello')
  if no
    console.log 'out'

and use it by

ibrik cover test.coffee

We gets summary.

Statements   : 72.73% ( 8/11 )
Branches     : 50% ( 3/6 )
Functions    : 50% ( 1/2 )
Lines        : 66.67% ( 6/9 )

And by using istanbul, we can see coverage result on browser.

istanbul report html && open coverage/index.html

brower result

Internal

CoffeeScriptRedux made by Michael Ficarra generates Mozilla JS AST and generate JavaScript code by using Escodegen as its backend.

CoffeeScriptRedux architecture

So ibrik generates Mozilla JS AST by using CoffeeScriptRedux compiler and passes it to istanbul instrumenter.

ibrik archirecture

istanbul instrumenter refers line & column information attached to AST, so by attaching line & column information to original (CoffeeScript) code, we can get instrumented JavaScript code and trace CoffeeScript execution. This is the same logic to how CoffeeScriptRedux compiler generates SourceMap.

Future

ibrik implementation is still at very experimental stage. Check the ibrik project on GitHub.

Contributions are welcome.