diff options
author | Richard van Velzen <rvanvelzen1@gmail.com> | 2016-01-20 19:04:36 +0100 |
---|---|---|
committer | Richard van Velzen <rvanvelzen1@gmail.com> | 2016-01-20 19:04:36 +0100 |
commit | b5a7197ae592a1ccdb217e74df4973d2a02a3a06 (patch) | |
tree | 70a36f264c9ba5992dcaeb2ccf7b75ec223759a0 /lib | |
parent | 26641f3fb20bce9394c3989bea0099dcd209be61 (diff) | |
parent | 1b703349cf824020c4dc64a58aa6d0dc3b809cea (diff) | |
download | tracifyjs-b5a7197ae592a1ccdb217e74df4973d2a02a3a06.tar.gz tracifyjs-b5a7197ae592a1ccdb217e74df4973d2a02a3a06.zip |
Merge pull request #928 from STRML/constPragma
Mark vars with /** @const */ pragma as consts so they can be eliminated.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/scope.js | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/scope.js b/lib/scope.js index 5e93020f..4cea5176 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -94,6 +94,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ var scope = self.parent_scope = null; var labels = new Dictionary(); var defun = null; + var last_var_had_const_pragma = false; var nesting = 0; var tw = new TreeWalker(function(node, descend){ if (options.screw_ie8 && node instanceof AST_Catch) { @@ -151,10 +152,13 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ // later. (node.scope = defun.parent_scope).def_function(node); } + else if (node instanceof AST_Var) { + last_var_had_const_pragma = node.has_const_pragma(); + } else if (node instanceof AST_SymbolVar || node instanceof AST_SymbolConst) { var def = defun.def_variable(node); - def.constant = node instanceof AST_SymbolConst; + def.constant = node instanceof AST_SymbolConst || last_var_had_const_pragma; def.init = tw.parent().value; } else if (node instanceof AST_SymbolCatch) { @@ -357,6 +361,12 @@ AST_Symbol.DEFMETHOD("global", function(){ return this.definition().global; }); +AST_Var.DEFMETHOD("has_const_pragma", function() { + var comments_before = this.start && this.start.comments_before; + var lastComment = comments_before && comments_before[comments_before.length - 1]; + return lastComment && /@const\b/.test(lastComment.value); +}); + AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){ return defaults(options, { except : [], |