From 26a115adcfa9c792e09d01d8e0673fa3d8c2f6b0 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 24 Oct 2010 12:48:42 -0400 Subject: [PATCH] Removing the mixed-in sys/util module. Switching from 'puts' to console.log --- Cakefile | 10 ++++----- bin/cake | 1 - bin/coffee | 1 - examples/beautiful_code/binary_search.coffee | 6 ++--- .../beautiful_code/quicksort_runtime.coffee | 6 ++--- .../regular_expression_matcher.coffee | 12 +++++----- examples/blocks.coffee | 4 ++-- .../computer_science/binary_search.coffee | 8 +++---- examples/computer_science/bubble_sort.coffee | 4 ++-- examples/computer_science/linked_list.coffee | 20 ++++++++--------- .../computer_science/luhn_algorithm.coffee | 6 ++--- examples/computer_science/merge_sort.coffee | 4 ++-- .../computer_science/selection_sort.coffee | 4 ++-- examples/poignant.coffee | 22 +++++++++---------- examples/web_server.coffee | 3 +-- lib/cake.js | 8 +++---- lib/command.js | 16 +++++++------- lib/repl.js | 4 ++-- src/cake.coffee | 8 +++---- src/command.coffee | 18 +++++++-------- src/repl.coffee | 6 ++--- src/rewriter.coffee | 2 +- test/test_break.coffee | 2 +- 23 files changed, 86 insertions(+), 89 deletions(-) diff --git a/Cakefile b/Cakefile index 4f89ddfb..d6a1abaa 100644 --- a/Cakefile +++ b/Cakefile @@ -11,12 +11,12 @@ reset = '\033[0m' # Run a CoffeeScript through our node/coffee interpreter. run = (args) -> proc = spawn 'bin/coffee', args - proc.stderr.on 'data', (buffer) -> puts buffer.toString() + proc.stderr.on 'data', (buffer) -> console.log buffer.toString() proc.on 'exit', (status) -> process.exit(1) if status != 0 # Log a message with a color. log = (message, color, explanation) -> - puts color + message + reset + ' ' + (explanation or '') + console.log color + message + reset + ' ' + (explanation or '') option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`' @@ -25,9 +25,9 @@ task 'install', 'install CoffeeScript into /usr/local (or --prefix)', (options) lib = "#{base}/lib/coffee-script" bin = "#{base}/bin" node = "~/.node_libraries/coffee-script" - puts "Installing CoffeeScript to #{lib}" - puts "Linking to #{node}" - puts "Linking 'coffee' to #{bin}/coffee" + console.log "Installing CoffeeScript to #{lib}" + console.log "Linking to #{node}" + console.log "Linking 'coffee' to #{bin}/coffee" exec([ "mkdir -p #{lib} #{bin}" "cp -rf bin lib LICENSE README package.json src #{lib}" diff --git a/bin/cake b/bin/cake index c15f840e..e92e7523 100755 --- a/bin/cake +++ b/bin/cake @@ -4,5 +4,4 @@ var path = require('path'); var fs = require('fs'); var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'); -require(lib + '/helpers').extend(global, require(/^v0\.[012]/.test(process.version) ? 'sys' : 'util')); require(lib + '/cake').run(); diff --git a/bin/coffee b/bin/coffee index f81c5e0c..4dc0a5ec 100755 --- a/bin/coffee +++ b/bin/coffee @@ -4,5 +4,4 @@ var path = require('path'); var fs = require('fs'); var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'); -require(lib + '/helpers').extend(global, require(/^v0\.[012]/.test(process.version) ? 'sys' : 'util')); require(lib + '/command').run(); diff --git a/examples/beautiful_code/binary_search.coffee b/examples/beautiful_code/binary_search.coffee index 6ad42c8b..05156ef1 100644 --- a/examples/beautiful_code/binary_search.coffee +++ b/examples/beautiful_code/binary_search.coffee @@ -11,6 +11,6 @@ index = (list, target) -> if val < target then low = mid + 1 else high = mid return -1 -puts 2 is index [10, 20, 30, 40, 50], 30 -puts 4 is index [-97, 35, 67, 88, 1200], 1200 -puts 0 is index [0, 45, 70], 0 \ No newline at end of file +console.log 2 is index [10, 20, 30, 40, 50], 30 +console.log 4 is index [-97, 35, 67, 88, 1200], 1200 +console.log 0 is index [0, 45, 70], 0 \ No newline at end of file diff --git a/examples/beautiful_code/quicksort_runtime.coffee b/examples/beautiful_code/quicksort_runtime.coffee index 27794f44..d48e0198 100644 --- a/examples/beautiful_code/quicksort_runtime.coffee +++ b/examples/beautiful_code/quicksort_runtime.coffee @@ -8,6 +8,6 @@ runtime = (N) -> t = n - 1 + sum / n t -puts runtime(3) is 2.6666666666666665 -puts runtime(5) is 7.4 -puts runtime(8) is 16.92142857142857 +console.log runtime(3) is 2.6666666666666665 +console.log runtime(5) is 7.4 +console.log runtime(8) is 16.92142857142857 diff --git a/examples/beautiful_code/regular_expression_matcher.coffee b/examples/beautiful_code/regular_expression_matcher.coffee index 99ed0a11..b57be547 100644 --- a/examples/beautiful_code/regular_expression_matcher.coffee +++ b/examples/beautiful_code/regular_expression_matcher.coffee @@ -26,9 +26,9 @@ match_star = (c, regexp, text) -> return false unless text and (text[0] is c or c is '.') text = text.slice(1) -puts match("ex", "some text") -puts match("s..t", "spit") -puts match("^..t", "buttercup") -puts match("i..$", "cherries") -puts match("o*m", "vrooooommm!") -puts match("^hel*o$", "hellllllo") \ No newline at end of file +console.log match("ex", "some text") +console.log match("s..t", "spit") +console.log match("^..t", "buttercup") +console.log match("i..$", "cherries") +console.log match("o*m", "vrooooommm!") +console.log match("^hel*o$", "hellllllo") \ No newline at end of file diff --git a/examples/blocks.coffee b/examples/blocks.coffee index f9e67ffd..6edf4e6b 100644 --- a/examples/blocks.coffee +++ b/examples/blocks.coffee @@ -11,7 +11,7 @@ append = (location, data) -> throw new Error("Location does not exist") unless path.exists() File.open path, 'a', (file) -> - file.puts YAML.dump data + file.console.log YAML.dump data data @@ -35,7 +35,7 @@ write = (location, data) -> File.open path, 'w', (file) -> return false if Digest.MD5.hexdigest(file.read()) is data.hash() - file.puts YAML.dump data + file.console.log YAML.dump data true diff --git a/examples/computer_science/binary_search.coffee b/examples/computer_science/binary_search.coffee index 69fc678a..3a78755d 100644 --- a/examples/computer_science/binary_search.coffee +++ b/examples/computer_science/binary_search.coffee @@ -19,7 +19,7 @@ binary_search = (items, value) -> # Test the function. -puts 2 is binary_search [10, 20, 30, 40, 50], 30 -puts 4 is binary_search [-97, 35, 67, 88, 1200], 1200 -puts 0 is binary_search [0, 45, 70], 0 -puts(-1 is binary_search [0, 45, 70], 10) \ No newline at end of file +console.log 2 is binary_search [10, 20, 30, 40, 50], 30 +console.log 4 is binary_search [-97, 35, 67, 88, 1200], 1200 +console.log 0 is binary_search [0, 45, 70], 0 +console.log(-1 is binary_search [0, 45, 70], 10) \ No newline at end of file diff --git a/examples/computer_science/bubble_sort.coffee b/examples/computer_science/bubble_sort.coffee index 4757c8ef..07be5b02 100644 --- a/examples/computer_science/bubble_sort.coffee +++ b/examples/computer_science/bubble_sort.coffee @@ -7,5 +7,5 @@ bubble_sort = (list) -> # Test the function. -puts bubble_sort([3, 2, 1]).join(' ') is '1 2 3' -puts bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file +console.log bubble_sort([3, 2, 1]).join(' ') is '1 2 3' +console.log bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file diff --git a/examples/computer_science/linked_list.coffee b/examples/computer_science/linked_list.coffee index 95bb2efe..c59bb116 100644 --- a/examples/computer_science/linked_list.coffee +++ b/examples/computer_science/linked_list.coffee @@ -93,16 +93,16 @@ class LinkedList list = new LinkedList list.add("Hi") -puts list.size() is 1 -puts list.item(0) is "Hi" -puts list.item(1) is null +console.log list.size() is 1 +console.log list.item(0) is "Hi" +console.log list.item(1) is null list = new LinkedList list.add("zero").add("one").add("two") -puts list.size() is 3 -puts list.item(2) is "two" -puts list.remove(1) is "one" -puts list.item(0) is "zero" -puts list.item(1) is "two" -puts list.size() is 2 -puts list.item(-10) is null +console.log list.size() is 3 +console.log list.item(2) is "two" +console.log list.remove(1) is "one" +console.log list.item(0) is "zero" +console.log list.item(1) is "two" +console.log list.size() is 2 +console.log list.item(-10) is null diff --git a/examples/computer_science/luhn_algorithm.coffee b/examples/computer_science/luhn_algorithm.coffee index db1c5443..8ca5aa2c 100644 --- a/examples/computer_science/luhn_algorithm.coffee +++ b/examples/computer_science/luhn_algorithm.coffee @@ -31,6 +31,6 @@ is_valid_identifier = (identifier) -> # Tests. -puts is_valid_identifier("49927398716") is true -puts is_valid_identifier("4408041234567893") is true -puts is_valid_identifier("4408041234567890") is false +console.log is_valid_identifier("49927398716") is true +console.log is_valid_identifier("4408041234567893") is true +console.log is_valid_identifier("4408041234567890") is false diff --git a/examples/computer_science/merge_sort.coffee b/examples/computer_science/merge_sort.coffee index 4fb69072..f03e415f 100644 --- a/examples/computer_science/merge_sort.coffee +++ b/examples/computer_science/merge_sort.coffee @@ -15,5 +15,5 @@ merge_sort = (list) -> # Test the function. -puts merge_sort([3, 2, 1]).join(' ') is '1 2 3' -puts merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file +console.log merge_sort([3, 2, 1]).join(' ') is '1 2 3' +console.log merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file diff --git a/examples/computer_science/selection_sort.coffee b/examples/computer_science/selection_sort.coffee index 1e87d561..161e5862 100644 --- a/examples/computer_science/selection_sort.coffee +++ b/examples/computer_science/selection_sort.coffee @@ -19,5 +19,5 @@ selection_sort = (list) -> # Test the function. -puts selection_sort([3, 2, 1]).join(' ') is '1 2 3' -puts selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file +console.log selection_sort([3, 2, 1]).join(' ') is '1 2 3' +console.log selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file diff --git a/examples/poignant.coffee b/examples/poignant.coffee index dc07ef88..aa7bb0ad 100644 --- a/examples/poignant.coffee +++ b/examples/poignant.coffee @@ -76,29 +76,29 @@ WishScanner = # p_up = rand( charisma ) # if p_up % 9 == 7 # @life += p_up / 4 -# puts "[#{ self.class } magick powers up #{ p_up }!]" +# console.log "[#{ self.class } magick powers up #{ p_up }!]" # end # @life -= damage -# puts "[#{ self.class } has died.]" if @life <= 0 +# console.log "[#{ self.class } has died.]" if @life <= 0 # end # # # This method takes one turn in a fight. # def fight( enemy, weapon ) # if life <= 0 -# puts "[#{ self.class } is too dead to fight!]" +# console.log "[#{ self.class } is too dead to fight!]" # return # end # # # Attack the opponent # your_hit = rand( strength + weapon ) -# puts "[You hit with #{ your_hit } points of damage!]" +# console.log "[You hit with #{ your_hit } points of damage!]" # enemy.hit( your_hit ) # # # Retaliation # p enemy # if enemy.life > 0 # enemy_hit = rand( enemy.strength + enemy.weapon ) -# puts "[Your enemy hit with #{ enemy_hit } points of damage!]" +# console.log "[Your enemy hit with #{ enemy_hit } points of damage!]" # self.hit( enemy_hit ) # end # end @@ -112,24 +112,24 @@ Creature = p_up = Math.rand this.charisma if p_up % 9 is 7 this.life += p_up / 4 - puts "[" + this.name + " magick powers up " + p_up + "!]" + console.log "[" + this.name + " magick powers up " + p_up + "!]" this.life -= damage - if this.life <= 0 then puts "[" + this.name + " has died.]" + if this.life <= 0 then console.log "[" + this.name + " has died.]" # This method takes one turn in a fight. fight: (enemy, weapon) -> - if this.life <= 0 then return puts "[" + this.name + "is too dead to fight!]" + if this.life <= 0 then return console.log "[" + this.name + "is too dead to fight!]" # Attack the opponent. your_hit = Math.rand this.strength + weapon - puts "[You hit with " + your_hit + "points of damage!]" + console.log "[You hit with " + your_hit + "points of damage!]" enemy.hit your_hit # Retaliation. - puts enemy + console.log enemy if enemy.life > 0 enemy_hit = Math.rand enemy.strength + enemy.weapon - puts "[Your enemy hit with " + enemy_hit + "points of damage!]" + console.log "[Your enemy hit with " + enemy_hit + "points of damage!]" this.hit enemy_hit diff --git a/examples/web_server.coffee b/examples/web_server.coffee index c2ee0898..ea23e228 100644 --- a/examples/web_server.coffee +++ b/examples/web_server.coffee @@ -1,6 +1,5 @@ # Contributed by Jason Huggins -sys = require 'sys' http = require 'http' server = http.createServer (req, res) -> @@ -10,4 +9,4 @@ server = http.createServer (req, res) -> server.listen 3000 -sys.puts "Server running at http://localhost:3000/" +console.log "Server running at http://localhost:3000/" diff --git a/lib/cake.js b/lib/cake.js index ae45e3bc..4ff2f4cb 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -57,18 +57,18 @@ }; printTasks = function() { var _ref, desc, name, spaces, task; - puts(''); + console.log(''); for (name in _ref = tasks) { task = _ref[name]; spaces = 20 - name.length; spaces = spaces > 0 ? Array(spaces + 1).join(' ') : ''; desc = task.description ? "# " + task.description : ''; - puts("cake " + name + spaces + " " + desc); + console.log("cake " + name + spaces + " " + desc); } - return switches.length ? puts(oparse.help()) : undefined; + return switches.length ? console.log(oparse.help()) : undefined; }; missingTask = function(task) { - puts("No such task: \"" + task + "\""); + console.log("No such task: \"" + task + "\""); return process.exit(1); }; }).call(this); diff --git a/lib/command.js b/lib/command.js index 81056047..e4e013b3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -104,13 +104,13 @@ if (o.tokens) { return printTokens(CoffeeScript.tokens(t.input)); } else if (o.nodes) { - return puts(CoffeeScript.nodes(t.input).toString().trim()); + return console.log(CoffeeScript.nodes(t.input).toString().trim()); } else if (o.run) { return CoffeeScript.run(t.input, t.options); } else { t.output = CoffeeScript.compile(t.input, t.options); CoffeeScript.emit('success', task); - return o.print ? print(t.output) : o.compile ? writeJs(t.file, t.output, base) : o.lint ? lint(t.output) : undefined; + return o.print ? console.log(t.output.trim()) : o.compile ? writeJs(t.file, t.output, base) : o.lint ? lint(t.output) : undefined; } } catch (err) { CoffeeScript.emit('failure', err, task); @@ -118,7 +118,7 @@ return; } if (o.watch) { - return puts(err.message); + return console.log(err.message); } error(err.stack); return process.exit(1); @@ -163,7 +163,7 @@ js = ' '; } return fs.writeFile(jsPath, js, function(err) { - return err ? puts(err.message) : opts.compile && opts.watch ? puts("Compiled " + source) : undefined; + return err ? console.log(err.message) : opts.compile && opts.watch ? console.log("Compiled " + source) : undefined; }); }; return path.exists(dir, function(exists) { @@ -173,7 +173,7 @@ lint = function(js) { var conf, jsl, printIt; printIt = function(buffer) { - return puts(buffer.toString().trim()); + return console.log(buffer.toString().trim()); }; conf = __dirname + '/../extras/jsl.conf'; jsl = spawn('jsl', ['-nologo', '-stdin', '-conf', conf]); @@ -193,7 +193,7 @@ } return _result; })(); - return puts(strings.join(' ')); + return console.log(strings.join(' ')); }; parseOptions = function() { var o; @@ -211,11 +211,11 @@ }; }; usage = function() { - puts(optionParser.help()); + console.log(optionParser.help()); return process.exit(0); }; version = function() { - puts("CoffeeScript version " + CoffeeScript.VERSION); + console.log("CoffeeScript version " + CoffeeScript.VERSION); return process.exit(0); }; }).call(this); diff --git a/lib/repl.js b/lib/repl.js index d30f756e..78fbaa57 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -18,10 +18,10 @@ fileName: 'repl' }); if (val !== undefined) { - puts(inspect(val)); + console.log(inspect(val)); } } catch (err) { - puts(err.stack || err.toString()); + console.log(err.stack || err.toString()); } return repl.prompt(); }; diff --git a/src/cake.coffee b/src/cake.coffee index 61567bc6..4fbd19ab 100644 --- a/src/cake.coffee +++ b/src/cake.coffee @@ -55,15 +55,15 @@ exports.run = -> # Display the list of Cake tasks in a format similar to `rake -T` printTasks = -> - puts '' + console.log '' for all name, task of tasks spaces = 20 - name.length spaces = if spaces > 0 then Array(spaces + 1).join(' ') else '' desc = if task.description then "# #{task.description}" else '' - puts "cake #{name}#{spaces} #{desc}" - puts oparse.help() if switches.length + console.log "cake #{name}#{spaces} #{desc}" + console.log oparse.help() if switches.length # Print an error and exit when attempting to all an undefined task. missingTask = (task) -> - puts "No such task: \"#{task}\"" + console.log "No such task: \"#{task}\"" process.exit 1 diff --git a/src/command.coffee b/src/command.coffee index e90767ee..21a07119 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -100,12 +100,12 @@ compileScript = (file, input, base) -> t = task = {file, input, options} CoffeeScript.emit 'compile', task if o.tokens then printTokens CoffeeScript.tokens t.input - else if o.nodes then puts CoffeeScript.nodes(t.input).toString().trim() + else if o.nodes then console.log CoffeeScript.nodes(t.input).toString().trim() else if o.run then CoffeeScript.run t.input, t.options else t.output = CoffeeScript.compile t.input, t.options CoffeeScript.emit 'success', task - if o.print then print t.output + if o.print then console.log t.output.trim() else if o.compile then writeJs t.file, t.output, base else if o.lint then lint t.output catch err @@ -113,7 +113,7 @@ compileScript = (file, input, base) -> # node will print a stack trace and exit the program. CoffeeScript.emit 'failure', err, task return if CoffeeScript.listeners('failure').length - return puts err.message if o.watch + return console.log err.message if o.watch error err.stack process.exit 1 @@ -149,15 +149,15 @@ writeJs = (source, js, base) -> compile = -> js = ' ' if js.length <= 0 fs.writeFile jsPath, js, (err) -> - if err then puts err.message - else if opts.compile and opts.watch then puts "Compiled #{source}" + if err then console.log err.message + else if opts.compile and opts.watch then console.log "Compiled #{source}" path.exists dir, (exists) -> if exists then compile() else exec "mkdir -p #{dir}", compile # Pipe compiled JS through JSLint (requires a working `jsl` command), printing # any errors or warnings that arise. lint = (js) -> - printIt = (buffer) -> puts buffer.toString().trim() + printIt = (buffer) -> console.log buffer.toString().trim() conf = __dirname + '/../extras/jsl.conf' jsl = spawn 'jsl', ['-nologo', '-stdin', '-conf', conf] jsl.stdout.on 'data', printIt @@ -170,7 +170,7 @@ printTokens = (tokens) -> strings = for token in tokens [tag, value] = [token[0], token[1].toString().replace(/\n/, '\\n')] "[#{tag} #{value}]" - puts strings.join(' ') + console.log strings.join(' ') # Use the [OptionParser module](optparse.html) to extract all options from # `process.argv` that are specified in `SWITCHES`. @@ -187,10 +187,10 @@ compileOptions = (fileName) -> {fileName, bare: opts.bare} # Print the `--help` usage message and exit. usage = -> - puts optionParser.help() + console.log optionParser.help() process.exit 0 # Print the `--version` message and exit. version = -> - puts "CoffeeScript version #{CoffeeScript.VERSION}" + console.log "CoffeeScript version #{CoffeeScript.VERSION}" process.exit 0 diff --git a/src/repl.coffee b/src/repl.coffee index 294146b2..1abbc312 100644 --- a/src/repl.coffee +++ b/src/repl.coffee @@ -2,7 +2,7 @@ # and evaluates it. Good for simple tests, or poking around the **Node.js** API. # Using it looks like this: # -# coffee> puts "#{num} bottles of beer" for num in [99..1] +# coffee> console.log "#{num} bottles of beer" for num in [99..1] # Require the **coffee-script** module to get access to the compiler. CoffeeScript = require './coffee-script' @@ -21,9 +21,9 @@ helpers.extend global, quit: -> process.exit(0) run = (buffer) -> try val = CoffeeScript.eval buffer.toString(), bare: on, globals: on, fileName: 'repl' - puts inspect val if val isnt undefined + console.log inspect val if val isnt undefined catch err - puts err.stack or err.toString() + console.log err.stack or err.toString() repl.prompt() # Create the REPL by listening to **stdin**. diff --git a/src/rewriter.coffee b/src/rewriter.coffee index bec06d78..09295e75 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -10,7 +10,7 @@ class exports.Rewriter # Helpful snippet for debugging: - # puts (t[0] + '/' + t[1] for t in @tokens).join ' ' + # console.log (t[0] + '/' + t[1] for t in @tokens).join ' ' # Rewrite the token stream in multiple passes, one logical filter at # a time. This could certainly be changed into a single pass through the diff --git a/test/test_break.coffee b/test/test_break.coffee index 7a6219f6..4cb6d8da 100644 --- a/test/test_break.coffee +++ b/test/test_break.coffee @@ -4,7 +4,7 @@ callWithLambda = (l) -> null for i in array result = callWithLambda(->) if i == 2 - puts "i = 2" + console.log "i = 2" else break