In the code line, I saved a piece of code, maybe CSS, SASS, SCSS, JavaScript or CoffeeScript. The content comes from the user, and I need to check the syntax before saving to the database.
I need to check the syntax is correct. I am currently using an ugly hack that works. Do you have a better solution?
def check_js if language == 'coffee' # CoffeeScript CoffeeScript.compile code else # JavaScript Uglifier.compile code end rescue ExecJS::RuntimeError => e errors.add :code, e.message end def check_css if language == 'css' # CSS Sass::CSS.new(code).render else # SASS, SCSS Sass.compile code, syntax: language.to_sym end rescue Sass::SyntaxError => e errors.add :code, e.message end
ruby-on-rails sass coffeescript
Benj
source share