Search code examples
ruby-on-railsherokuproduction-environment

Assets:precompile fails because a '/' character in .js file is interpreted as the beginning of a regex


Pushing my code to Heroku fails with this error:

SyntaxError: Invalid regular expression: / (this.sample_rate /: Unterminated group
(in /tmp/build_3iv6pbfccqx1r/app/assets/javascripts/application.js)

It occurs during rake assets:precompile.

The line that causes this error looks like:

data[i] = Math.sin(this.x++ / (this.sample_rate / (this.frequency * 2 * Math.PI)));

and it's in a .js file under app/assets/javascripts. This is my only file where using the '/' character causes any problems.


Solution

  • The obvious but not necessarily best answer would be to split up that line some.

    this.x++;
    data[i] = Math.sin(this.x / (this.sample_rate / (this.frequency * 2 * Math.PI)));
    

    Or

    this.x++;
    var t1 = this.frequency * 2 * Math.PI;
    var t2 = this.sample_rate / t1;
    var t3 = this.x / t2;
    data[i] = Math.sin(t3);
    

    Though I'm sure there are better variable names based on the underlying equation.

    However, I am interested to see why this is happening. Does the JavaScript work locally?