From 64301d0d08996feb218fed5e741401147b99930d Mon Sep 17 00:00:00 2001 From: Hao-kang Den Date: Tue, 12 Mar 2013 09:09:07 +0800 Subject: [PATCH 1/4] fix node v0.10 path --- lib/coffee-script/coffee-script.js | 2 +- src/coffee-script.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 02aa3d78..61e11af5 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -107,7 +107,7 @@ mainModule = require.main; mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.'; mainModule.moduleCache && (mainModule.moduleCache = {}); - mainModule.paths = require('module')._nodeModulePaths(path.dirname(fs.realpathSync(options.filename))); + mainModule.paths = require('module')._nodeModulePaths(path.dirname(mainModule.filename)); if (!helpers.isCoffee(mainModule.filename) || require.extensions) { return mainModule._compile(compile(code, options), mainModule.filename); } else { diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index d4ca54ef..136783c3 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -105,7 +105,7 @@ exports.run = (code, options = {}) -> mainModule.moduleCache and= {} # Assign paths for node_modules loading - mainModule.paths = require('module')._nodeModulePaths path.dirname fs.realpathSync options.filename + mainModule.paths = require('module')._nodeModulePaths path.dirname mainModule.filename # Compile. if not helpers.isCoffee(mainModule.filename) or require.extensions From b0cbd90e645c9010b310fbb02c7162744bd5cb70 Mon Sep 17 00:00:00 2001 From: Demian Ferreiro Date: Mon, 11 Mar 2013 19:52:18 -0300 Subject: [PATCH 2/4] Fixes #1829. Preserve variable scope in the REPL --- lib/coffee-script/nodes.js | 17 +++++++++++------ lib/coffee-script/repl.js | 3 ++- src/nodes.coffee | 5 ++++- src/repl.coffee | 3 +-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 71830556..a0d4697b 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -395,19 +395,24 @@ }; Block.prototype.compileRoot = function(o) { - var exp, fragments, i, prelude, preludeExps, rest; + var exp, fragments, i, name, prelude, preludeExps, rest, _i, _len, _ref2; o.indent = o.bare ? '' : TAB; - o.scope = new Scope(null, this, null); o.level = LEVEL_TOP; this.spaced = true; + o.scope = new Scope(null, this, null); + _ref2 = o.locals || []; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + name = _ref2[_i]; + o.scope.parameter(name); + } prelude = []; if (!o.bare) { preludeExps = (function() { - var _i, _len, _ref2, _results; - _ref2 = this.expressions; + var _j, _len1, _ref3, _results; + _ref3 = this.expressions; _results = []; - for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) { - exp = _ref2[i]; + for (i = _j = 0, _len1 = _ref3.length; _j < _len1; i = ++_j) { + exp = _ref3[i]; if (!(exp.unwrap() instanceof Comment)) { break; } diff --git a/lib/coffee-script/repl.js b/lib/coffee-script/repl.js index f792886c..ad0d056f 100644 --- a/lib/coffee-script/repl.js +++ b/lib/coffee-script/repl.js @@ -21,7 +21,8 @@ ast = CoffeeScript.nodes(input); ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]); js = ast.compile({ - bare: true + bare: true, + locals: Object.keys(context) }); } catch (_error) { err = _error; diff --git a/src/nodes.coffee b/src/nodes.coffee index 624ec2d5..5d0aae22 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -303,9 +303,12 @@ exports.Block = class Block extends Base # clean up obvious double-parentheses. compileRoot: (o) -> o.indent = if o.bare then '' else TAB - o.scope = new Scope null, this, null o.level = LEVEL_TOP @spaced = yes + o.scope = new Scope null, this, null + # Mark given local variables in the root scope as parameters so they don't + # end up being declared on this block. + o.scope.parameter name for name in o.locals or [] prelude = [] unless o.bare preludeExps = for exp, i in @expressions diff --git a/src/repl.coffee b/src/repl.coffee index 251692f1..a8620794 100644 --- a/src/repl.coffee +++ b/src/repl.coffee @@ -15,7 +15,6 @@ replDefaults = # Require AST nodes to do some AST manipulation. {Block, Assign, Value, Literal} = require './nodes' - # TODO: fix #1829: pass in-scope vars and avoid accidentally shadowing them by omitting those declarations try # Generate the AST of the clean input. ast = CoffeeScript.nodes input @@ -23,7 +22,7 @@ replDefaults = ast = new Block [ new Assign (new Value new Literal '_'), ast, '=' ] - js = ast.compile bare: yes + js = ast.compile bare: yes, locals: Object.keys(context) catch err console.log prettyErrorMessage err, filename, input, yes cb null, vm.runInContext(js, context, filename) From 4ca6da4a95f1c4b5339c24af9848625e285b1361 Mon Sep 17 00:00:00 2001 From: Demian Ferreiro Date: Mon, 11 Mar 2013 20:16:48 -0300 Subject: [PATCH 3/4] Add REPL scoping tests --- test/repl.coffee | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/repl.coffee b/test/repl.coffee index 25877cee..b48def74 100644 --- a/test/repl.coffee +++ b/test/repl.coffee @@ -80,3 +80,14 @@ testRepl "evaluates multiline", (input, output) -> input.emitLine ' 1 + 1' input.emit 'keypress', null, ctrlV eq '2', output.lastWrite() + +testRepl "variables in scope are preserved", (input, output) -> + input.emitLine 'a = 1' + input.emitLine 'do -> a = 2' + input.emitLine 'a' + eq '2', output.lastWrite() + +testRepl "existencial assignment of previously declared variable", (input, output) -> + input.emitLine 'a = null' + input.emitLine 'a ?= 42' + eq '42', output.lastWrite() \ No newline at end of file From f56f5f234e1d48d103de7c8fdd606e5fb83541b6 Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Mon, 11 Mar 2013 21:48:21 -0500 Subject: [PATCH 4/4] @epidemian typo --- test/repl.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/repl.coffee b/test/repl.coffee index b48def74..a6d2a515 100644 --- a/test/repl.coffee +++ b/test/repl.coffee @@ -87,7 +87,7 @@ testRepl "variables in scope are preserved", (input, output) -> input.emitLine 'a' eq '2', output.lastWrite() -testRepl "existencial assignment of previously declared variable", (input, output) -> +testRepl "existential assignment of previously declared variable", (input, output) -> input.emitLine 'a = null' input.emitLine 'a ?= 42' - eq '42', output.lastWrite() \ No newline at end of file + eq '42', output.lastWrite()