coffeescript
Advanced tools
Comparing version 1.1.1 to 1.1.2
(function() { | ||
var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref; | ||
var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref; | ||
var __hasProp = Object.prototype.hasOwnProperty; | ||
fs = require('fs'); | ||
path = require('path'); | ||
vm = require('vm'); | ||
_ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED; | ||
@@ -21,3 +21,3 @@ parser = require('./parser').parser; | ||
} | ||
exports.VERSION = '1.1.1'; | ||
exports.VERSION = '1.1.2'; | ||
exports.RESERVED = RESERVED; | ||
@@ -49,44 +49,72 @@ exports.helpers = require('./helpers'); | ||
exports.run = function(code, options) { | ||
var Module, root; | ||
root = module; | ||
while (root.parent) { | ||
root = root.parent; | ||
} | ||
root.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.'; | ||
if (root.moduleCache) { | ||
root.moduleCache = {}; | ||
} | ||
var Module, mainModule; | ||
mainModule = require.main; | ||
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.'; | ||
mainModule.moduleCache && (mainModule.moduleCache = {}); | ||
if (process.binding('natives').module) { | ||
Module = require('module').Module; | ||
root.paths = Module._nodeModulePaths(path.dirname(options.filename)); | ||
mainModule.paths = Module._nodeModulePaths(path.dirname(options.filename)); | ||
} | ||
if (path.extname(root.filename) !== '.coffee' || require.extensions) { | ||
return root._compile(compile(code, options), root.filename); | ||
if (path.extname(mainModule.filename) !== '.coffee' || require.extensions) { | ||
return mainModule._compile(compile(code, options), mainModule.filename); | ||
} else { | ||
return root._compile(code, root.filename); | ||
return mainModule._compile(code, mainModule.filename); | ||
} | ||
}; | ||
exports.eval = function(code, options) { | ||
var g, js, sandbox; | ||
var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref2, _ref3, _ref4, _require; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
sandbox = options.sandbox; | ||
if (!sandbox) { | ||
sandbox = { | ||
require: require, | ||
module: { | ||
exports: {} | ||
if (!(code = code.trim())) { | ||
return; | ||
} | ||
if (_ref2 = require('vm'), Script = _ref2.Script, _ref2) { | ||
sandbox = Script.createContext(); | ||
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox; | ||
if (options.sandbox != null) { | ||
if (options.sandbox instanceof sandbox.constructor) { | ||
sandbox = options.sandbox; | ||
} else { | ||
_ref3 = options.sandbox; | ||
for (k in _ref3) { | ||
if (!__hasProp.call(_ref3, k)) continue; | ||
v = _ref3[k]; | ||
sandbox[k] = v; | ||
} | ||
} | ||
}; | ||
for (g in global) { | ||
sandbox[g] = global[g]; | ||
} | ||
sandbox.global = sandbox; | ||
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox; | ||
sandbox.__filename = options.filename || 'eval'; | ||
sandbox.__dirname = path.dirname(sandbox.__filename); | ||
if (!(sandbox.module || sandbox.require)) { | ||
Module = require('module'); | ||
sandbox.module = _module = new Module(options.modulename || 'eval'); | ||
sandbox.require = _require = function(path) { | ||
return Module._load(path, _module); | ||
}; | ||
_module.filename = sandbox.__filename; | ||
_ref4 = Object.getOwnPropertyNames(require); | ||
for (_i = 0, _len = _ref4.length; _i < _len; _i++) { | ||
r = _ref4[_i]; | ||
_require[r] = require[r]; | ||
} | ||
_require.paths = _module.paths = Module._nodeModulePaths(process.cwd()); | ||
_require.resolve = function(request) { | ||
return Module._resolveFilename(request, _module); | ||
}; | ||
} | ||
} | ||
sandbox.__filename = options.filename || 'eval'; | ||
sandbox.__dirname = path.dirname(sandbox.__filename); | ||
js = compile("_=(" + (code.trim()) + ")", options); | ||
return vm.runInNewContext(js, sandbox, sandbox.__filename); | ||
o = {}; | ||
for (k in options) { | ||
if (!__hasProp.call(options, k)) continue; | ||
v = options[k]; | ||
o[k] = v; | ||
} | ||
o.bare = true; | ||
js = compile(code, o); | ||
if (Script) { | ||
return Script.runInContext(js, sandbox); | ||
} else { | ||
return eval(js); | ||
} | ||
}; | ||
@@ -93,0 +121,0 @@ lexer = new Lexer; |
@@ -58,9 +58,27 @@ (function() { | ||
compileScripts = function() { | ||
var base, compile, source, _i, _len, _results; | ||
_results = []; | ||
var base, compile, source, unprocessed, _i, _j, _len, _len2, _results; | ||
unprocessed = []; | ||
for (_i = 0, _len = sources.length; _i < _len; _i++) { | ||
source = sources[_i]; | ||
unprocessed[sources.indexOf(source)] = 1; | ||
} | ||
_results = []; | ||
for (_j = 0, _len2 = sources.length; _j < _len2; _j++) { | ||
source = sources[_j]; | ||
base = path.join(source); | ||
compile = function(source, topLevel) { | ||
compile = function(source, sourceIndex, topLevel) { | ||
var remaining_files; | ||
remaining_files = function() { | ||
var total, x, _k, _len3; | ||
total = 0; | ||
for (_k = 0, _len3 = unprocessed.length; _k < _len3; _k++) { | ||
x = unprocessed[_k]; | ||
total += x; | ||
} | ||
return total; | ||
}; | ||
return path.exists(source, function(exists) { | ||
if (topLevel && !exists && source.slice(-7) !== '.coffee') { | ||
return compile("" + source + ".coffee", sourceIndex, topLevel); | ||
} | ||
if (topLevel && !exists) { | ||
@@ -75,15 +93,22 @@ throw new Error("File not found: " + source); | ||
return fs.readdir(source, function(err, files) { | ||
var file, _j, _len2, _results2; | ||
_results2 = []; | ||
for (_j = 0, _len2 = files.length; _j < _len2; _j++) { | ||
file = files[_j]; | ||
_results2.push(compile(path.join(source, file))); | ||
var file, _k, _len3; | ||
if (err) { | ||
throw err; | ||
} | ||
return _results2; | ||
unprocessed[sourceIndex] += files.length; | ||
for (_k = 0, _len3 = files.length; _k < _len3; _k++) { | ||
file = files[_k]; | ||
compile(path.join(source, file), sourceIndex); | ||
} | ||
return unprocessed[sourceIndex] -= 1; | ||
}); | ||
} else if (topLevel || path.extname(source) === '.coffee') { | ||
fs.readFile(source, function(err, code) { | ||
if (err) { | ||
throw err; | ||
} | ||
unprocessed[sourceIndex] -= 1; | ||
if (opts.join) { | ||
contents[sources.indexOf(source)] = code.toString(); | ||
if (helpers.compact(contents).length > 0) { | ||
contents[sourceIndex] = helpers.compact([contents[sourceIndex], code.toString()]).join('\n'); | ||
if (helpers.compact(contents).length > 0 && remaining_files() === 0) { | ||
return compileJoin(); | ||
@@ -98,2 +123,4 @@ } | ||
} | ||
} else { | ||
return unprocessed[sourceIndex] -= 1; | ||
} | ||
@@ -103,3 +130,3 @@ }); | ||
}; | ||
_results.push(compile(source, true)); | ||
_results.push(compile(source, sources.indexOf(source), true)); | ||
} | ||
@@ -106,0 +133,0 @@ return _results; |
@@ -51,3 +51,3 @@ (function() { | ||
tag = 'IDENTIFIER'; | ||
if (__indexOf.call(JS_KEYWORDS, id) >= 0 || !forcedIdentifier && __indexOf.call(COFFEE_KEYWORDS, id) >= 0) { | ||
if (!forcedIdentifier && (__indexOf.call(JS_KEYWORDS, id) >= 0 || __indexOf.call(COFFEE_KEYWORDS, id) >= 0)) { | ||
tag = id.toUpperCase(); | ||
@@ -198,3 +198,3 @@ if (tag === 'WHEN' && (_ref3 = this.tag(), __indexOf.call(LINE_BREAK, _ref3) >= 0)) { | ||
Lexer.prototype.regexToken = function() { | ||
var match, prev, regex, _ref2; | ||
var length, match, prev, regex, _ref2; | ||
if (this.chunk.charAt(0) !== '/') { | ||
@@ -204,3 +204,5 @@ return 0; | ||
if (match = HEREGEX.exec(this.chunk)) { | ||
return this.heregexToken(match); | ||
length = this.heregexToken(match); | ||
this.line += count(match[0], '\n'); | ||
return length; | ||
} | ||
@@ -448,2 +450,4 @@ prev = last(this.tokens); | ||
return this; | ||
} else { | ||
return this; | ||
} | ||
@@ -464,3 +468,3 @@ } | ||
Lexer.prototype.balancedString = function(str, end) { | ||
var i, letter, prev, stack, _ref2; | ||
var i, letter, match, prev, stack, _ref2; | ||
stack = [end]; | ||
@@ -482,2 +486,4 @@ for (i = 1, _ref2 = str.length; 1 <= _ref2 ? i < _ref2 : i > _ref2; 1 <= _ref2 ? i++ : i--) { | ||
stack.push(end = letter); | ||
} else if (end === '}' && letter === '/' && (match = HEREGEX.exec(str.slice(i)) || REGEX.exec(str.slice(i)))) { | ||
i += match[0].length - 1; | ||
} else if (end === '}' && letter === '{') { | ||
@@ -624,3 +630,3 @@ stack.push(end = '}'); | ||
IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/; | ||
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i; | ||
NUMBER = /^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i; | ||
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[^\n\S]*)?\1/; | ||
@@ -627,0 +633,0 @@ OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/; |
117
lib/repl.js
(function() { | ||
var ACCESSOR, CoffeeScript, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, getCompletions, getPropertyNames, inspect, readline, repl, run, stdin, stdout; | ||
var __hasProp = Object.prototype.hasOwnProperty; | ||
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, g, getCompletions, inspect, nonContextGlobals, readline, repl, run, sandbox, stdin, stdout, _i, _len; | ||
CoffeeScript = require('./coffee-script'); | ||
@@ -8,2 +7,5 @@ readline = require('readline'); | ||
Script = require('vm').Script; | ||
Module = require('module'); | ||
REPL_PROMPT = 'coffee> '; | ||
REPL_PROMPT_CONTINUATION = '......> '; | ||
enableColours = false; | ||
@@ -19,37 +21,41 @@ if (process.platform !== 'win32') { | ||
backlog = ''; | ||
run = (function() { | ||
var g, sandbox; | ||
sandbox = { | ||
require: require, | ||
module: { | ||
exports: {} | ||
sandbox = Script.createContext(); | ||
nonContextGlobals = ['Buffer', 'console', 'process', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout']; | ||
for (_i = 0, _len = nonContextGlobals.length; _i < _len; _i++) { | ||
g = nonContextGlobals[_i]; | ||
sandbox[g] = global[g]; | ||
} | ||
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox; | ||
run = function(buffer) { | ||
var code, returnValue, _; | ||
if (!buffer.toString().trim() && !backlog) { | ||
repl.prompt(); | ||
return; | ||
} | ||
code = backlog += buffer; | ||
if (code[code.length - 1] === '\\') { | ||
backlog = "" + backlog.slice(0, -1) + "\n"; | ||
repl.setPrompt(REPL_PROMPT_CONTINUATION); | ||
repl.prompt(); | ||
return; | ||
} | ||
repl.setPrompt(REPL_PROMPT); | ||
backlog = ''; | ||
try { | ||
_ = sandbox._; | ||
returnValue = CoffeeScript.eval("_=(" + code + "\n)", { | ||
sandbox: sandbox, | ||
filename: 'repl', | ||
modulename: 'repl' | ||
}); | ||
if (returnValue === void 0) { | ||
sandbox._ = _; | ||
} else { | ||
process.stdout.write(inspect(returnValue, false, 2, enableColours) + '\n'); | ||
} | ||
}; | ||
for (g in global) { | ||
sandbox[g] = global[g]; | ||
} catch (err) { | ||
error(err); | ||
} | ||
sandbox.global = sandbox; | ||
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox; | ||
return function(buffer) { | ||
var code, val; | ||
code = backlog += '\n' + buffer.toString(); | ||
if (code[code.length - 1] === '\\') { | ||
return backlog = backlog.slice(0, backlog.length - 1); | ||
} | ||
backlog = ''; | ||
try { | ||
val = CoffeeScript.eval(code, { | ||
sandbox: sandbox, | ||
bare: true, | ||
filename: 'repl' | ||
}); | ||
if (val !== void 0) { | ||
process.stdout.write(inspect(val, false, 2, enableColours) + '\n'); | ||
} | ||
} catch (err) { | ||
error(err); | ||
} | ||
return repl.prompt(); | ||
}; | ||
})(); | ||
return repl.prompt(); | ||
}; | ||
ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/; | ||
@@ -65,7 +71,7 @@ SIMPLEVAR = /\s*(\w*)$/i; | ||
try { | ||
val = Script.runInThisContext(obj); | ||
val = Script.runInContext(obj, sandbox); | ||
} catch (error) { | ||
return [[], text]; | ||
return; | ||
} | ||
completions = getCompletions(prefix, getPropertyNames(val)); | ||
completions = getCompletions(prefix, Object.getOwnPropertyNames(val)); | ||
return [completions, prefix]; | ||
@@ -75,6 +81,7 @@ } | ||
completeVariable = function(text) { | ||
var completions, free, scope, _ref; | ||
var completions, free, possibilities, vars, _ref; | ||
if (free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0) { | ||
scope = Script.runInThisContext('this'); | ||
completions = getCompletions(free, CoffeeScript.RESERVED.concat(getPropertyNames(scope))); | ||
vars = Script.runInContext('Object.getOwnPropertyNames(this)', sandbox); | ||
possibilities = vars.concat(CoffeeScript.RESERVED); | ||
completions = getCompletions(free, possibilities); | ||
return [completions, free]; | ||
@@ -84,6 +91,6 @@ } | ||
getCompletions = function(prefix, candidates) { | ||
var el, _i, _len, _results; | ||
var el, _j, _len2, _results; | ||
_results = []; | ||
for (_i = 0, _len = candidates.length; _i < _len; _i++) { | ||
el = candidates[_i]; | ||
for (_j = 0, _len2 = candidates.length; _j < _len2; _j++) { | ||
el = candidates[_j]; | ||
if (el.indexOf(prefix) === 0) { | ||
@@ -95,11 +102,2 @@ _results.push(el); | ||
}; | ||
getPropertyNames = function(obj) { | ||
var name, _results; | ||
_results = []; | ||
for (name in obj) { | ||
if (!__hasProp.call(obj, name)) continue; | ||
_results.push(name); | ||
} | ||
return _results; | ||
}; | ||
process.on('uncaughtException', error); | ||
@@ -114,8 +112,19 @@ if (readline.createInterface.length < 3) { | ||
} | ||
repl.setPrompt('coffee> '); | ||
repl.on('attemptClose', function() { | ||
if (backlog) { | ||
backlog = ''; | ||
process.stdout.write('\n'); | ||
repl.setPrompt(REPL_PROMPT); | ||
return repl.prompt(); | ||
} else { | ||
return repl.close(); | ||
} | ||
}); | ||
repl.on('close', function() { | ||
process.stdout.write('\n'); | ||
return stdin.destroy(); | ||
}); | ||
repl.on('line', run); | ||
repl.setPrompt(REPL_PROMPT); | ||
repl.prompt(); | ||
}).call(this); |
@@ -165,3 +165,3 @@ (function() { | ||
return this.scanTokens(function(token, i, tokens) { | ||
var callObject, current, next, prev, seenSingle, tag, _ref, _ref2, _ref3; | ||
var callObject, current, next, prev, seenControl, seenSingle, tag, _ref, _ref2, _ref3; | ||
tag = token[0]; | ||
@@ -174,2 +174,3 @@ if (tag === 'CLASS' || tag === 'IF') { | ||
seenSingle = false; | ||
seenControl = false; | ||
if (__indexOf.call(LINEBREAKS, tag) >= 0) { | ||
@@ -194,9 +195,12 @@ noCall = false; | ||
} | ||
if (tag === 'IF' || tag === 'ELSE' || tag === '->' || tag === '=>') { | ||
if (tag === 'IF' || tag === 'ELSE' || tag === 'CATCH' || tag === '->' || tag === '=>') { | ||
seenSingle = true; | ||
} | ||
if (tag === 'IF' || tag === 'ELSE' || tag === 'SWITCH' || tag === 'TRY') { | ||
seenControl = true; | ||
} | ||
if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') { | ||
return true; | ||
} | ||
return !token.generated && this.tag(i - 1) !== ',' && __indexOf.call(IMPLICIT_END, tag) >= 0 && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref4 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref4) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{'))); | ||
return !token.generated && this.tag(i - 1) !== ',' && (__indexOf.call(IMPLICIT_END, tag) >= 0 || (tag === 'INDENT' && !seenControl)) && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref4 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref4) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{'))); | ||
}, action); | ||
@@ -358,3 +362,3 @@ if (prev[0] === '?') { | ||
IMPLICIT_BLOCK = ['->', '=>', '{', '[', ',']; | ||
IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR', 'INDENT']; | ||
IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR']; | ||
SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN']; | ||
@@ -361,0 +365,0 @@ SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']; |
@@ -6,3 +6,3 @@ { | ||
"author": "Jeremy Ashkenas", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"licenses": [{ | ||
@@ -9,0 +9,0 @@ "type": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
283249
22
5913
4