From 8efd5911e29aebb4dafb3fbca663c6d3390718b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sun, 6 Sep 2015 21:33:17 +0100 Subject: [PATCH 1/2] parse, output the let statement --- lib/ast.js | 4 ++++ lib/output.js | 3 +++ lib/parse.js | 23 ++++++++++++++++++----- test/compress/block-scope.js | 8 ++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/compress/block-scope.js diff --git a/lib/ast.js b/lib/ast.js index 7e7503ee..da15ca25 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -654,6 +654,10 @@ var AST_Var = DEFNODE("Var", null, { $documentation: "A `var` statement" }, AST_Definitions); +var AST_Let = DEFNODE("Let", null, { + $documentation: "A `let` statement" +}, AST_Definitions); + var AST_Const = DEFNODE("Const", null, { $documentation: "A `const` statement" }, AST_Definitions); diff --git a/lib/output.js b/lib/output.js index 5d8bef10..461f4fa4 100644 --- a/lib/output.js +++ b/lib/output.js @@ -976,6 +976,9 @@ function OutputStream(options) { if (!avoid_semicolon) output.semicolon(); }); + DEFPRINT(AST_Let, function(self, output){ + self._do_print(output, "let"); + }); DEFPRINT(AST_Var, function(self, output){ self._do_print(output, "var"); }); diff --git a/lib/parse.js b/lib/parse.js index 35096bc6..54403590 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -44,7 +44,7 @@ "use strict"; -var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in of instanceof new return switch throw try typeof var void while with'; +var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in of instanceof new return switch throw try typeof var let void while with'; var KEYWORDS_ATOM = 'false null true'; var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield' + " " + KEYWORDS_ATOM + " " + KEYWORDS; @@ -875,6 +875,9 @@ function parse($TEXT, options) { case "var": return tmp = var_(), semicolon(), tmp; + case "let": + return tmp = let_(), semicolon(), tmp; + case "const": return tmp = const_(), semicolon(), tmp; @@ -945,13 +948,15 @@ function parse($TEXT, options) { expect("("); var init = null; if (!is("punc", ";")) { - init = is("keyword", "var") - ? (next(), var_(true)) - : expression(true, true); + init = + is("keyword", "var") ? (next(), var_(true)) : + is("keyword", "let") ? (next(), let_(true)) : + expression(true, true); var is_in = is("operator", "in"); var is_of = is("keyword", "of"); if (is_in || is_of) { - if (init instanceof AST_Var && init.definitions.length > 1) + if ((init instanceof AST_Var || init instanceof AST_Let) && + init.definitions.length > 1) croak("Only one variable declaration allowed in for..in loop"); next(); if (is_in) { @@ -1249,6 +1254,14 @@ function parse($TEXT, options) { }); }; + var let_ = function(no_in) { + return new AST_Let({ + start : prev(), + definitions : vardefs(no_in, false), + end : prev() + }); + }; + var const_ = function() { return new AST_Const({ start : prev(), diff --git a/test/compress/block-scope.js b/test/compress/block-scope.js new file mode 100644 index 00000000..d0cdf75e --- /dev/null +++ b/test/compress/block-scope.js @@ -0,0 +1,8 @@ + +let_statement: { + input: { + let x = 6; + } + expect_exact: "let x=6;" +} + From b5626308b7eb4eaec6677cebfe5ddda53da0ce86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sun, 6 Sep 2015 21:56:55 +0100 Subject: [PATCH 2/2] Add a test to make sure future generations don't hoist lets --- test/compress/block-scope.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/compress/block-scope.js b/test/compress/block-scope.js index d0cdf75e..d1953ce5 100644 --- a/test/compress/block-scope.js +++ b/test/compress/block-scope.js @@ -6,3 +6,28 @@ let_statement: { expect_exact: "let x=6;" } +do_not_hoist_let: { + options = { + hoist_vars: true, + }; + input: { + function x() { + if (FOO) { + let let1; + let let2; + var var1; + var var2; + } + } + } + expect: { + function x() { + var var1, var2; + if (FOO) { + let let1; + let let2; + } + } + } +} +