From d9fba949836ce5a20e4d46475218f3d5ace7411a Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 12 Feb 2010 23:09:57 -0500 Subject: [PATCH] added coffee --lint to the self-compiler's resume --- lib/coffee_script/command_line.js | 25 ++++++++++++++++++++++++- src/command_line.coffee | 23 ++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/coffee_script/command_line.js b/lib/coffee_script/command_line.js index ab8be60d..b285e2ea 100644 --- a/lib/coffee_script/command_line.js +++ b/lib/coffee_script/command_line.js @@ -54,7 +54,13 @@ } opts = this.options; return posix.cat(source).addCallback(function(code) { - opts.tokens ? puts(coffee.tokenize(code).join(' ')) : opts.tree ? puts(coffee.tree(code).toString()) : opts.run ? eval(coffee.compile(code)) : opts.print ? puts(coffee.compile(code)) : exports.write_js(source, coffee.compile(code)); + var js; + if (opts.tokens) { + puts(coffee.tokenize(code).join(' ')); + } else { + opts.tree ? puts(coffee.tree(code).toString()) : (js = coffee.compile(code)); + opts.run ? eval(js) : opts.print ? puts(js) : opts.lint ? exports.lint(js) : exports.write_js(source, coffee.compile(code)); + } return exports.compile_scripts(); }); }; @@ -68,6 +74,23 @@ return posix.write(fd, js); }); }; + // Pipe compiled JS through JSLint (requires a working 'jsl' command). + exports.lint = function lint(js) { + var jsl; + jsl = process.createChildProcess('jsl', ['-nologo', '-stdin']); + jsl.addListener('output', function(result) { + if (result) { + return puts(result.replace(/\n/g, '')); + } + }); + jsl.addListener('errror', function(result) { + if (result) { + return puts(result); + } + }); + jsl.write(js); + return jsl.close(); + }; // Use OptionParser for all the options. exports.parse_options = function parse_options() { var oparser, opts, paths; diff --git a/src/command_line.coffee b/src/command_line.coffee index 5ece9f79..2a3c2ea5 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -63,11 +63,14 @@ exports.compile_scripts: -> return unless source: @sources.shift() opts: @options posix.cat(source).addCallback (code) -> - if opts.tokens then puts coffee.tokenize(code).join(' ') - else if opts.tree then puts coffee.tree(code).toString() - else if opts.run then eval coffee.compile code - else if opts.print then puts coffee.compile code - else exports.write_js source, coffee.compile code + if opts.tokens then puts coffee.tokenize(code).join(' ') + else if opts.tree then puts coffee.tree(code).toString() + else + js: coffee.compile code + if opts.run then eval js + else if opts.print then puts js + else if opts.lint then exports.lint js + else exports.write_js source, coffee.compile code exports.compile_scripts() # Write out a JavaScript source file with the compiled code. @@ -78,6 +81,16 @@ exports.write_js: (source, js) -> posix.open(js_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback (fd) -> posix.write(fd, js) +# Pipe compiled JS through JSLint (requires a working 'jsl' command). +exports.lint: (js) -> + jsl: process.createChildProcess('jsl', ['-nologo', '-stdin']) + jsl.addListener 'output', (result) -> + puts result.replace(/\n/g, '') if result + jsl.addListener 'errror', (result) -> + puts result if result + jsl.write js + jsl.close() + # Use OptionParser for all the options. exports.parse_options: -> opts: @options: {}