Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 997787e52ce619a467dcea86c30cce87124fdd75 1 parent 2ea51b0
@TrevorBurnham TrevorBurnham authored
View
26 lib/coffee-script/coffee-script.js
@@ -1,10 +1,11 @@
(function() {
- var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref;
+ var EventEmitter, Lexer, RESERVED, compile, fs, helpers, lexer, parser, path, _ref;
var __hasProp = Object.prototype.hasOwnProperty;
fs = require('fs');
path = require('path');
_ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED;
parser = require('./parser').parser;
+ EventEmitter = require('events').EventEmitter;
if (require.extensions) {
require.extensions['.coffee'] = function(module, filename) {
var content;
@@ -20,15 +21,32 @@
}
exports.VERSION = '1.1.3-pre';
exports.RESERVED = RESERVED;
- exports.helpers = require('./helpers');
- exports.compile = compile = function(code, options) {
+ exports.helpers = helpers = require('./helpers');
+ helpers.extend(exports, new EventEmitter);
+ exports.listeners();
+ exports.compile = compile = function(input, options) {
+ var output;
if (options == null) options = {};
+ exports.emit('compile', {
+ input: input,
+ options: options
+ });
try {
- return (parser.parse(lexer.tokenize(code))).compile(options);
+ output = (parser.parse(lexer.tokenize(input))).compile(options);
+ exports.emit('success', {
+ input: input,
+ options: options
+ });
+ return output;
} catch (err) {
if (options.filename) {
err.message = "In " + options.filename + ", " + err.message;
}
+ exports.emit('failure', {
+ input: input,
+ options: options,
+ err: err
+ });
throw err;
}
};
View
17 src/coffee-script.coffee
@@ -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
16 test/compilation.coffee
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.