Permalink
Browse files

Fixing #1023 by moving compiler events from command to coffee-script

  • Loading branch information...
1 parent 2ea51b0 commit 997787e52ce619a467dcea86c30cce87124fdd75 @TrevorBurnham TrevorBurnham committed Sep 7, 2011
Showing with 51 additions and 8 deletions.
  1. +22 −4 lib/coffee-script/coffee-script.js
  2. +13 −4 src/coffee-script.coffee
  3. +16 −0 test/compilation.coffee

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -10,6 +10,7 @@ fs = require 'fs'
path = require 'path'
{Lexer,RESERVED} = require './lexer'
{parser} = require './parser'
+{EventEmitter} = require 'events'
# TODO: Remove registerExtension when fully deprecated.
if require.extensions
@@ -26,15 +27,23 @@ exports.VERSION = '1.1.3-pre'
exports.RESERVED = RESERVED
# Expose helpers for testing.
-exports.helpers = require './helpers'
+exports.helpers = helpers = require './helpers'
+
+# Allow CoffeeScript to emit Node.js events.
+helpers.extend exports, new EventEmitter
+exports.listeners() # events don't work without this (under Node 0.4.11)
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
# compiler.
-exports.compile = compile = (code, options = {}) ->
+exports.compile = compile = (input, options = {}) ->
+ exports.emit 'compile', {input, options}
try
- (parser.parse lexer.tokenize code).compile options
+ output = (parser.parse lexer.tokenize input).compile options
+ exports.emit 'success', {input, options}
+ output
catch err
err.message = "In #{options.filename}, #{err.message}" if options.filename
+ exports.emit 'failure', {input, options, err}
throw err
# Tokenize a string of CoffeeScript code, and return the array of tokens.
@@ -122,4 +131,4 @@ parser.lexer =
upcomingInput: ->
""
-parser.yy = require './nodes'
+parser.yy = require './nodes'
View
@@ -13,6 +13,22 @@ test "--bare", ->
eq -1, CoffeeScript.compile('x = y', bare: on).indexOf 'function'
ok 'passed' is CoffeeScript.eval '"passed"', bare: on, filename: 'test'
+test "compiler emits events", ->
+ compileCount = 0; successCount = 0; failureCount = 0
+ CoffeeScript.on 'compile', -> compileCount++
+ CoffeeScript.on 'success', -> successCount++
+ CoffeeScript.on 'failure', -> failureCount++
+ CoffeeScript.compile('x = y')
+ eq compileCount, 1
+ eq successCount, 1
+ eq failureCount, 0
+ compileCount = 0; successCount = 0; failureCount = 0
+ try
+ CoffeeScript.compile('^@$%!') # valid Perl, no doubt
+ eq compileCount, 1
+ eq successCount, 0
+ eq failureCount, 1
+
test "multiple generated references", ->
a = {b: []}
a.b[true] = -> this == a.b

0 comments on commit 997787e

Please sign in to comment.