[Pkg-javascript-commits] [node-acorn-jsx] 28/484: Use 'new Function' instead of '(1, eval)'

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:19:59 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-acorn-jsx.

commit 07152c41c45aff290b3f74fee83c661141781031
Author: Marijn Haverbeke <marijnh at gmail.com>
Date:   Fri Oct 12 23:09:23 2012 +0200

    Use 'new Function' instead of '(1, eval)'
    
    In the hope that it'll be less confusing.
---
 acorn.js   |  4 ++--
 index.html | 23 +++++++++++++----------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/acorn.js b/acorn.js
index 9aca7c2..2a107de 100644
--- a/acorn.js
+++ b/acorn.js
@@ -262,7 +262,7 @@
 
   function makePredicate(words) {
     words = words.split(" ");
-    var f = "(function(str){", cats = [];
+    var f = "", cats = [];
     out: for (var i = 0; i < words.length; ++i) {
       for (var j = 0; j < cats.length; ++j)
         if (cats[j][0].length == words[i].length) {
@@ -296,7 +296,7 @@
     } else {
       compareTo(words);
     }
-    return (1, eval)(f + "})");
+    return new Function("str", f);
   }
 
   // The ECMAScript 3 reserved word list.
diff --git a/index.html b/index.html
index 31df065..0afca7f 100644
--- a/index.html
+++ b/index.html
@@ -11,20 +11,21 @@ https://github.com/marijnh/acorn.git
 </code></pre>
 
 <p>Please use the <a href="https://github.com/marijnh/acorn/issues">github bug tracker</a> to report issues.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">exports</span><span class="p">)</span> <span class="p">{</span>
-  <span class="s2">"strict mode"</span><span class="p">;</span>
+  <span class="s2">"use strict"</span><span class="p">;</span>
 
   <span class="nx">exports</span><span class="p">.</span><span class="nx">version</span> <span class="o">=</span> <span class="s2">"0.0.1"</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">¶</a>               </div>               <p>The main exported interface (under <code>window. [...]
 browser) is a <code>parse</code> function that takes a code string and
 returns an abstract syntax tree as specified by <a href="https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API">Mozilla parser
 API</a>, with the caveat that the SpiderMonkey-specific syntax
-(<code>let</code>, <code>yield</code>, inline XML, etc) is not recognized.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">var</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">input</span><span class="p">,</span> <span class="nx">inputLen</span><span class="p">;</span>
+(<code>let</code>, <code>yield</code>, inline XML, etc) is not recognized.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">var</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">input</span><span class="p">,</span> <span class="nx">inputLen</span><span class="p">,</span> <span class="nx">sourceFile</span><span class="p">;</span>
 
   <span class="nx">exports</span><span class="p">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">inpt</span><span class="p">,</span> <span class="nx">opts</span><span class="p">)</span> <span class="p">{</span>
     <span class="nx">input</span> <span class="o">=</span> <span class="nb">String</span><span class="p">(</span><span class="nx">inpt</span><span class="p">);</span> <span class="nx">inputLen</span> <span class="o">=</span> <span class="nx">input</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
     <span class="nx">options</span> <span class="o">=</span> <span class="nx">opts</span> <span class="o">||</span> <span class="p">{};</span>
     <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">opt</span> <span class="k">in</span> <span class="nx">defaultOptions</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">opt</span><span class="p">))</span>
       <span class="nx">options</span><span class="p">[</span><span class="nx">opt</span><span class="p">]</span> <span class="o">=</span> <span class="nx">defaultOptions</span><span class="p">[</span><span class="nx">opt</span><span class="p">];</span>
-    <span class="k">return</span> <span class="nx">parseTopLevel</span><span class="p">();</span>
+    <span class="nx">sourceFile</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">sourceFile</span> <span class="o">||</span> <span class="kc">null</span><span class="p">;</span>
+    <span class="k">return</span> <span class="nx">parseTopLevel</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">program</span><span class="p">);</span>
   <span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">¶</a>               </div>               <p>A second optional argument can be given to further configure
 the parser process. These options are recognized:</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">var</span> <span class="nx">defaultOptions</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">defaultOptions</span> <span class="o">=</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-4">             <t [...]
 be either 3 or 5. This
@@ -152,7 +153,7 @@ predicate from a space-separated string of words.</p>
 
 <p>It starts by sorting the words by length.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx">makePredicate</span><span class="p">(</span><span class="nx">words</span><span class="p">)</span> <span class="p">{</span>
     <span class="nx">words</span> <span class="o">=</span> <span class="nx">words</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">" "</span><span class="p">);</span>
-    <span class="kd">var</span> <span class="nx">f</span> <span class="o">=</span> <span class="s2">"(function(str){"</span><span class="p">,</span> <span class="nx">cats</span> <span class="o">=</span> <span class="p">[];</span>
+    <span class="kd">var</span> <span class="nx">f</span> <span class="o">=</span> <span class="s2">""</span><span class="p">,</span> <span class="nx">cats</span> <span class="o">=</span> <span class="p">[];</span>
     <span class="nx">out</span><span class="o">:</span> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">words</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
       <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o"><</span> <span class="nx">cats</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="o">++</span><span class="nx">j</span><span class="p">)</span>
         <span class="k">if</span> <span class="p">(</span><span class="nx">cats</span><span class="p">[</span><span class="nx">j</span><span class="p">][</span><span class="mi">0</span><span class="p">].</span><span class="nx">length</span> <span class="o">==</span> <span class="nx">words</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
@@ -178,7 +179,7 @@ switch first dispatches on the lengths, to save on comparisons.</p>
       <span class="nx">f</span> <span class="o">+=</span> <span class="s2">"}"</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-34">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-34">¶</a>               </div>               <p>Otherwise, simply generate a flat <code>switch</code> statement.</p>             </td>             <td cla [...]
       <span class="nx">compareTo</span><span class="p">(</span><span class="nx">words</span><span class="p">);</span>
     <span class="p">}</span>
-    <span class="k">return</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">eval</span><span class="p">)(</span><span class="nx">f</span> <span class="o">+</span> <span class="s2">"})"</span><span class="p">);</span>
+    <span class="k">return</span> <span class="k">new</span> <span class="nb">Function</span><span class="p">(</span><span class="s2">"str"</span><span class="p">,</span> <span class="nx">f</span><span class="p">);</span>
   <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-35">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-35">¶</a>               </div>               <p>The ECMAScript 3 reserved word list.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">var</span> <span class="nx">isReservedWord3</span [...]
 whitespace, identifier, and identifier-start categories. These
 are only applied when a character is found to actually have a
@@ -551,7 +552,7 @@ tests ("use strict"; 010; -- should fail).</p>             </td>             <td
       <span class="nx">tokCommentsBefore</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
     <span class="p">}</span>
     <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">locations</span><span class="p">)</span>
-      <span class="nx">node</span><span class="p">.</span><span class="nx">loc</span> <span class="o">=</span> <span class="p">{</span><span class="nx">start</span><span class="o">:</span> <span class="nx">tokStartLoc</span><span class="p">,</span> <span class="nx">end</span><span class="o">:</span> <span class="kc">null</span><span class="p">};</span>
+      <span class="nx">node</span><span class="p">.</span><span class="nx">loc</span> <span class="o">=</span> <span class="p">{</span><span class="nx">start</span><span class="o">:</span> <span class="nx">tokStartLoc</span><span class="p">,</span> <span class="nx">end</span><span class="o">:</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">source</span><span class="o">:</span> <span class="nx">sourceFile</span><span class="p">};</span>
     <span class="k">return</span> <span class="nx">node</span><span class="p">;</span>
   <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-76">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-76">¶</a>               </div>               <p>Start a node whose start offset/comments information should be
 based on the start of another node. For example, a binary
@@ -563,7 +564,7 @@ already been parsed.</p>             </td>             <td class="code">
       <span class="nx">other</span><span class="p">.</span><span class="nx">commentsBefore</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
     <span class="p">}</span>
     <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">locations</span><span class="p">)</span>
-      <span class="nx">node</span><span class="p">.</span><span class="nx">loc</span> <span class="o">=</span> <span class="p">{</span><span class="nx">start</span><span class="o">:</span> <span class="nx">other</span><span class="p">.</span><span class="nx">loc</span><span class="p">.</span><span class="nx">start</span><span class="p">,</span> <span class="nx">end</span><span class="o">:</span> <span class="kc">null</span><span class="p">};</span>
+      <span class="nx">node</span><span class="p">.</span><span class="nx">loc</span> <span class="o">=</span> <span class="p">{</span><span class="nx">start</span><span class="o">:</span> <span class="nx">other</span><span class="p">.</span><span class="nx">loc</span><span class="p">.</span><span class="nx">start</span><span class="p">,</span> <span class="nx">end</span><span class="o">:</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">source</span><span clas [...]
 
     <span class="k">return</span> <span class="nx">node</span><span class="p">;</span>
   <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-77">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-77">¶</a>               </div>               <p>Finish an AST node, adding <code>type</code>, <code>end</code>, and <code>commentsAfter</code>
@@ -619,7 +620,9 @@ to.</p>             </td>             <td class="code">               <div class
     <span class="k">if</span> <span class="p">(</span><span class="nx">strict</span> <span class="o">&&</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="s2">"Identifier"</span> <span class="o">&&</span> <span class="nx">isStrictBadIdWord</span><span class="p">(</span><span class="nx">expr</span><span class="p">.</span><span class="nx">name</span><span class="p">))</span>
       <span class="nx">raise</span><span class="p">(</span><span class="nx">expr</span><span class="p">.</span><span class="nx">start</span><span class="p">,</span> <span class="s2">"Assigning to "</span> <span class="o">+</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">" in strict mode"</span><span class="p">);</span>
   <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-85">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-85">¶</a>               </div>               <h3>Statement parsing</h3>             </td>             <td class="code">               <div class="highlight"><pre></pre></div>             </td>           </tr>                               <tr id=" [...]
-statements, and wraps them in a Program node.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx">parseTopLevel</span><span class="p">()</span> <span class="p">{</span>
+statements, and wraps them in a Program node.  Optionally takes a
+<code>program</code> argument.  If present, the statements will be appended
+to its body instead of creating a new node.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx">parseTopLevel</span><span class="p">(</span><span class="nx">program</span><span class="p">)</span> <span class="p">{</span>
     <span class="nx">initTokenState</span><span class="p">();</span>
     <span class="nx">lastStart</span> <span class="o">=</span> <span class="nx">lastEnd</span> <span class="o">=</span> <span class="nx">tokPos</span><span class="p">;</span>
     <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">locations</span><span class="p">)</span> <span class="nx">lastEndLoc</span> <span class="o">=</span> <span class="nx">curLineLoc</span><span class="p">();</span>
@@ -627,8 +630,8 @@ statements, and wraps them in a Program node.</p>             </td>
     <span class="nx">labels</span> <span class="o">=</span> <span class="p">[];</span>
     <span class="nx">readToken</span><span class="p">();</span>
 
-    <span class="kd">var</span> <span class="nx">node</span> <span class="o">=</span> <span class="nx">startNode</span><span class="p">(),</span> <span class="nx">first</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
-    <span class="nx">node</span><span class="p">.</span><span class="nx">body</span> <span class="o">=</span> <span class="p">[];</span>
+    <span class="kd">var</span> <span class="nx">node</span> <span class="o">=</span> <span class="nx">program</span> <span class="o">||</span> <span class="nx">startNode</span><span class="p">(),</span> <span class="nx">first</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
+    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">program</span><span class="p">)</span> <span class="nx">node</span><span class="p">.</span><span class="nx">body</span> <span class="o">=</span> <span class="p">[];</span>
     <span class="k">while</span> <span class="p">(</span><span class="nx">tokType</span> <span class="o">!==</span> <span class="nx">_eof</span><span class="p">)</span> <span class="p">{</span>
       <span class="kd">var</span> <span class="nx">stmt</span> <span class="o">=</span> <span class="nx">parseStatement</span><span class="p">();</span>
       <span class="nx">node</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">stmt</span><span class="p">);</span>

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-jsx.git



More information about the Pkg-javascript-commits mailing list