[Pkg-javascript-commits] [node-acorn-jsx] 172/484: Don't complain about reserved words in property name position

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:21 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 5bd50cce6f29f76c0c6bc09a46b8782a0dca8fd9
Author: Marijn Haverbeke <marijnh at gmail.com>
Date:   Fri Feb 14 11:27:48 2014 +0100

    Don't complain about reserved words in property name position
    
    Also add forbidReserved: "everywhere" support.
    
    Issue #85
---
 README.md     |  4 +++-
 acorn.js      | 28 +++++++++++++++++++---------
 index.html    | 28 +++++++++++++++++++---------
 test/tests.js | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 19 deletions(-)

diff --git a/README.md b/README.md
index 245b71b..32ee0b7 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,9 @@ object referring to that same position.
   trailing commas in array and object literals. Default is `true`.
 
 - **forbidReserved**: If `true`, using a reserved word will generate
-  an error. Defaults to `false`.
+  an error. Defaults to `false`. When given the value `"everywhere"`,
+  reserved words and keywords can also not be used as property names
+  (as in Internet Explorer's old parser).
   
 - **allowReturnOutsideFunction**: By default, a return statement at
   the top level raises an error. Set this to `true` to accept such
diff --git a/acorn.js b/acorn.js
index b58f1da..0f11931 100644
--- a/acorn.js
+++ b/acorn.js
@@ -62,7 +62,9 @@
     // trailing commas in array and object literals.
     allowTrailingCommas: true,
     // By default, reserved words are not enforced. Enable
-    // `forbidReserved` to enforce them.
+    // `forbidReserved` to enforce them. When this option has the
+    // value "everywhere", reserved words and keywords can also not be
+    // used as property names.
     forbidReserved: false,
     // When enabled, a return at the top level is not considered an
     // error.
@@ -941,13 +943,8 @@
   function readWord() {
     var word = readWord1();
     var type = _name;
-    if (!containsEsc) {
-      if (isKeyword(word)) type = keywordTypes[word];
-      else if (options.forbidReserved &&
-               (options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(word) ||
-               strict && isStrictReservedWord(word))
-        raise(tokStart, "The keyword '" + word + "' is reserved");
-    }
+    if (!containsEsc && isKeyword(word))
+      type = keywordTypes[word];
     return finishToken(type, word);
   }
 
@@ -1763,7 +1760,20 @@
 
   function parseIdent(liberal) {
     var node = startNode();
-    node.name = tokType === _name ? tokVal : (liberal && !options.forbidReserved && tokType.keyword) || unexpected();
+    if (liberal && options.forbidReserved == "everywhere") liberal = false;
+    if (tokType === _name) {
+      if (!liberal &&
+          (options.forbidReserved &&
+           (options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(tokVal) ||
+           strict && isStrictReservedWord(tokVal)) &&
+          input.slice(tokStart, tokEnd).indexOf("\\") == -1)
+        raise(tokStart, "The keyword '" + tokVal + "' is reserved");
+      node.name = tokVal;
+    } else if (liberal && tokType.keyword) {
+      node.name = tokType.keyword;
+    } else {
+      unexpected();
+    }
     tokRegexpAllowed = false;
     next();
     return finishNode(node, "Identifier");
diff --git a/index.html b/index.html
index 74e39e6..f189721 100644
--- a/index.html
+++ b/index.html
@@ -39,7 +39,9 @@ influences support for strict mode, the set of reserved words, and
 support for getters and setter.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">ecmaVersion</span><span class="o">:</span> <span class="mi">5</span><span class="p">,</span></pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">¶</a>               </div>       [...]
 automatic semicolon insertion.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">strictSemicolons</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span></pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">¶</a>               </di [...]
 trailing commas in array and object literals.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">allowTrailingCommas</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span></pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">¶</a>   [...]
-<code>forbidReserved</code> to enforce them.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">forbidReserved</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span></pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">¶</a>        [...]
+<code>forbidReserved</code> to enforce them. When this option has the
+value "everywhere", reserved words and keywords can also not be
+used as property names.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">forbidReserved</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span></pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">¶</a>               </div>        [...]
 error.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">allowReturnOutsideFunction</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span></pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">¶</a>               </div>             [...]
 <code>start</code> and <code>end</code> properties in <code>{line, column}</code> form (with
 line being 1-based and column 0-based) will be attached to the
@@ -676,13 +678,8 @@ containeds an escape, as a micro-optimization.</p>             </td>
 words when necessary.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx">readWord</span><span class="p">()</span> <span class="p">{</span>
     <span class="kd">var</span> <span class="nx">word</span> <span class="o">=</span> <span class="nx">readWord1</span><span class="p">();</span>
     <span class="kd">var</span> <span class="nx">type</span> <span class="o">=</span> <span class="nx">_name</span><span class="p">;</span>
-    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">containsEsc</span><span class="p">)</span> <span class="p">{</span>
-      <span class="k">if</span> <span class="p">(</span><span class="nx">isKeyword</span><span class="p">(</span><span class="nx">word</span><span class="p">))</span> <span class="nx">type</span> <span class="o">=</span> <span class="nx">keywordTypes</span><span class="p">[</span><span class="nx">word</span><span class="p">];</span>
-      <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">forbidReserved</span> <span class="o">&&</span>
-               <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">ecmaVersion</span> <span class="o">===</span> <span class="mi">3</span> <span class="o">?</span> <span class="nx">isReservedWord3</span> <span class="o">:</span> <span class="nx">isReservedWord5</span><span class="p">)(</span><span class="nx">word</span><span class="p">)</span> <span class="o">||</span>
-               <span class="nx">strict</span> <span class="o">&&</span> <span class="nx">isStrictReservedWord</span><span class="p">(</span><span class="nx">word</span><span class="p">))</span>
-        <span class="nx">raise</span><span class="p">(</span><span class="nx">tokStart</span><span class="p">,</span> <span class="s2">"The keyword '"</span> <span class="o">+</span> <span class="nx">word</span> <span class="o">+</span> <span class="s2">"' is reserved"</span><span class="p">);</span>
-    <span class="p">}</span>
+    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">containsEsc</span> <span class="o">&&</span> <span class="nx">isKeyword</span><span class="p">(</span><span class="nx">word</span><span class="p">))</span>
+      <span class="nx">type</span> <span class="o">=</span> <span class="nx">keywordTypes</span><span class="p">[</span><span class="nx">word</span><span class="p">];</span>
     <span class="k">return</span> <span class="nx">finishToken</span><span class="p">(</span><span class="nx">type</span><span class="p">,</span> <span class="nx">word</span><span class="p">);</span>
   <span class="p">}</span></pre></div>             </td>           </tr>                               <tr id="section-79">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-79">¶</a>               </div>               <h2>Parser</h2>             </td>             <td class="code">               <div class="highlight"><pre></pre></div>             </td>           </tr>                               <tr id="section-80" [...]
 syntactic elements, and recursively calling those, each function
@@ -1323,7 +1320,20 @@ for array literals).</p>             </td>             <td class="code">
 when parsing properties), it will also convert keywords into
 identifiers.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="kd">function</span> <span class="nx">parseIdent</span><span class="p">(</span><span class="nx">liberal</span><span class="p">)</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">node</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">tokType</span> <span class="o">===</span> <span class="nx">_name</span> <span class="o">?</span> <span class="nx">tokVal</span> <span class="o">:</span> <span class="p">(</span><span class="nx">liberal</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">forbidReserved</span> <spa [...]
+    <span class="k">if</span> <span class="p">(</span><span class="nx">liberal</span> <span class="o">&&</span> <span class="nx">options</span><span class="p">.</span><span class="nx">forbidReserved</span> <span class="o">==</span> <span class="s2">"everywhere"</span><span class="p">)</span> <span class="nx">liberal</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
+    <span class="k">if</span> <span class="p">(</span><span class="nx">tokType</span> <span class="o">===</span> <span class="nx">_name</span><span class="p">)</span> <span class="p">{</span>
+      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">liberal</span> <span class="o">&&</span>
+          <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">forbidReserved</span> <span class="o">&&</span>
+           <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">ecmaVersion</span> <span class="o">===</span> <span class="mi">3</span> <span class="o">?</span> <span class="nx">isReservedWord3</span> <span class="o">:</span> <span class="nx">isReservedWord5</span><span class="p">)(</span><span class="nx">tokVal</span><span class="p">)</span> <span class="o">||</span>
+           <span class="nx">strict</span> <span class="o">&&</span> <span class="nx">isStrictReservedWord</span><span class="p">(</span><span class="nx">tokVal</span><span class="p">))</span> <span class="o">&&</span>
+          <span class="nx">input</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tokStart</span><span class="p">,</span> <span class="nx">tokEnd</span><span class="p">).</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">"\\"</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
+        <span class="nx">raise</span><span class="p">(</span><span class="nx">tokStart</span><span class="p">,</span> <span class="s2">"The keyword '"</span> <span class="o">+</span> <span class="nx">tokVal</span> <span class="o">+</span> <span class="s2">"' is reserved"</span><span class="p">);</span>
+      <span class="nx">node</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">tokVal</span><span class="p">;</span>
+    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">liberal</span> <span class="o">&&</span> <span class="nx">tokType</span><span class="p">.</span><span class="nx">keyword</span><span class="p">)</span> <span class="p">{</span>
+      <span class="nx">node</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">tokType</span><span class="p">.</span><span class="nx">keyword</span><span class="p">;</span>
+    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+      <span class="nx">unexpected</span><span class="p">();</span>
+    <span class="p">}</span>
     <span class="nx">tokRegexpAllowed</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
     <span class="nx">next</span><span class="p">();</span>
     <span class="k">return</span> <span class="nx">finishNode</span><span class="p">(</span><span class="nx">node</span><span class="p">,</span> <span class="s2">"Identifier"</span><span class="p">);</span>
diff --git a/test/tests.js b/test/tests.js
index efce0b4..9cb0432 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -26348,6 +26348,39 @@ test("x = y-->10;\n --> nothing", {
   ]
 });
 
+test("'use strict';\nobject.static();", {
+  type: "Program",
+  body: [
+    {
+      type: "ExpressionStatement",
+      expression: {
+        type: "Literal",
+        value: "use strict",
+        raw: "'use strict'"
+      }
+    },
+    {
+      type: "ExpressionStatement",
+      expression: {
+        type: "CallExpression",
+        callee: {
+          type: "MemberExpression",
+          object: {
+            type: "Identifier",
+            name: "object"
+          },
+          property: {
+            type: "Identifier",
+            name: "static"
+          },
+          computed: false
+        },
+        arguments: []
+      }
+    }
+  ]
+});
+
 // Failure tests
 
 testFail("{",

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