1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fixes #1829. Preserve variable scope in the REPL

This commit is contained in:
Demian Ferreiro 2013-03-11 19:52:18 -03:00
parent 69f6500ba9
commit 5a004425ca
4 changed files with 18 additions and 10 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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

View file

@ -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)