From b61324058dcf0ca3827440ba92200df3080b70cd Mon Sep 17 00:00:00 2001 From: Chris Connelly Date: Thu, 26 Jan 2017 15:24:25 +0000 Subject: [PATCH] Fix "export" squashing variable declarations Exports that referenced variables assigned in the module would prevent the referenced variables from being declared, resulting in ReferenceErrors at run time. Fixes #4394. --- lib/coffee-script/nodes.js | 2 +- lib/coffee-script/scope.js | 7 +++++-- src/nodes.coffee | 2 +- src/scope.litcoffee | 5 ++--- test/modules.coffee | 16 ++++++++++++++++ 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 7d8b50c4..a4d09cc5 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -2059,7 +2059,7 @@ ModuleSpecifier.prototype.compileNode = function(o) { var code; - o.scope.add(this.identifier, this.moduleDeclarationType); + o.scope.find(this.identifier, this.moduleDeclarationType); code = []; code.push(this.makeCode(this.original.value)); if (this.alias != null) { diff --git a/lib/coffee-script/scope.js b/lib/coffee-script/scope.js index cd8939fd..60fa8a49 100644 --- a/lib/coffee-script/scope.js +++ b/lib/coffee-script/scope.js @@ -45,11 +45,14 @@ return this.parent.namedMethod(); }; - Scope.prototype.find = function(name) { + Scope.prototype.find = function(name, type) { + if (type == null) { + type = 'var'; + } if (this.check(name)) { return true; } - this.add(name, 'var'); + this.add(name, type); return false; }; diff --git a/src/nodes.coffee b/src/nodes.coffee index 6b62f389..adb1a9a6 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1352,7 +1352,7 @@ exports.ModuleSpecifier = class ModuleSpecifier extends Base children: ['original', 'alias'] compileNode: (o) -> - o.scope.add @identifier, @moduleDeclarationType + o.scope.find @identifier, @moduleDeclarationType code = [] code.push @makeCode @original.value code.push @makeCode " as #{@alias.value}" if @alias? diff --git a/src/scope.litcoffee b/src/scope.litcoffee index 8135cf00..b48aa231 100644 --- a/src/scope.litcoffee +++ b/src/scope.litcoffee @@ -44,9 +44,9 @@ function object that has a name filled in, or bottoms out. Look up a variable name in lexical scope, and declare it if it does not already exist. - find: (name) -> + find: (name, type = 'var') -> return yes if @check name - @add name, 'var' + @add name, type no Reserve a variable name as originating from a function parameter for this @@ -116,4 +116,3 @@ of this scope. assignedVariables: -> "#{v.name} = #{v.type.value}" for v in @variables when v.type.assigned - diff --git a/test/modules.coffee b/test/modules.coffee index a7753bba..9cc0e66c 100644 --- a/test/modules.coffee +++ b/test/modules.coffee @@ -733,3 +733,19 @@ test "export an imported aliased member named default", -> default as def } from 'lib';""" eq toJS(input), output + +test "#4394: export shouldn't prevent variable declarations", -> + input = """ + x = 1 + export { x } + """ + output = """ + var x; + + x = 1; + + export { + x + }; + """ + eq toJS(input), output