I moved this logic here, so that it needn't appear in both rewrite.js and rewrite.coffee.