Created
February 7, 2013 12:28
-
-
Save octplane/4730604 to your computer and use it in GitHub Desktop.
Ruby syntax validity checker (JRuby)
Javascript syntax validity checker. (Rhino)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies += "org.jruby" % "jruby" % "1.7.2" | |
libraryDependencies += "org.mozilla" % "rhino" % "1.7R4" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jruby.util._ | |
import java.io.File | |
import org.mozilla.javascript._ | |
import org.jruby._ | |
/** A wrapper around file, allowing iteration either on direct children | |
or on directory tree */ | |
class RichFile(file: File) { | |
def children = new Iterable[File] { | |
def iterator = if (file.isDirectory) file.listFiles.iterator else Iterator.empty | |
} | |
def andTree : Iterable[File] = | |
Seq(file) ++ children.flatMap(child => new RichFile(child).andTree) | |
} | |
/** implicitely enrich java.io.File with methods of RichFile */ | |
object RichFile { | |
implicit def toRichFile(file: File) = new RichFile(file) | |
} | |
object Linter { | |
def main(args: Array[String]) = { | |
import RichFile._ | |
val root = new File("/home/oct/code/project") | |
// filtering comes for free | |
val r = Ruby.getDefaultInstance | |
for(f <- root.andTree) { | |
if(f.getName.endsWith(".rb")) { | |
val source = scala.io.Source.fromFile(f) | |
val lines = source.mkString | |
source.close () | |
try { | |
val rootNode = r.parse(lines,f.getName,r.getCurrentContext.getCurrentScope,0, false) | |
} catch { | |
case exception: exceptions.RaiseException => { | |
Console.println("Syntax error in "+f) | |
Console.println(exception) | |
} | |
} | |
} else if(f.getName.endsWith(".js")) { | |
val source = scala.io.Source.fromFile(f) | |
val lines = source.mkString | |
source.close () | |
val js_parser = new Parser() | |
try { | |
js_parser.parse(lines, f.getName, 0) | |
} catch { | |
case exception: EvaluatorException => { | |
Console.println("Syntax error in "+f) | |
Console.println(exception) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment