tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

destructuring-shorthand-defaults.js (3204B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // Ensure that the syntax used in shorthand destructuring with defaults
      6 // e.g. |{x=1, y=2} = {}| properly raises a syntax error within an object
      7 // literal. As per ES6 12.2.6 Object Initializer, "NOTE 3."
      8 
      9 const SYNTAX_ERROR_STMTS = [
     10    // expressions
     11    "({x={}={}}),",
     12    "({y={x={}={}={}={}={}={}={}={}}={}}),",
     13    "({a=1, b=2, c=3, x=({}={})}),",
     14    "({x=1, y={z={1}}})",
     15    "({x=1} = {y=1});",
     16    "({x: y={z=1}}={})",
     17    "({x=1}),",
     18    "({z={x=1}})=>{};",
     19    "({x = ({y=1}) => y})",
     20    "(({x=1})) => x",
     21    "({e=[]}==(;",
     22    "({x=1}[-1]);",
     23    "({x=y}[-9])",
     24    "({x=y}.x.z[-9])",
     25    "({x=y}`${-9}`)",
     26    "(new {x=y}(-9))",
     27    "new {x=1}",
     28    "new {x=1}={}",
     29    "typeof {x=1}",
     30    "typeof ({x=1})",
     31    "({x=y, [-9]:0})",
     32    "((({w = x} >(-9)",
     33    "++({x=1})",
     34    "--{x=1}",
     35    "!{x=1}={}",
     36    "delete {x=1}",
     37    "delete ({x=1})",
     38    "delete {x=1} = {}",
     39    "({x=1}.abc)",
     40    "x > (0, {a = b} );",
     41    // declarations
     42    "var x = 0 + {a=1} = {}",
     43    "let o = {x=1};",
     44    "var j = {x=1};",
     45    "var j = {x={y=1}}={};",
     46    "const z = {x=1};",
     47    "const z = {x={y=1}}={};",
     48    "const {x=1};",
     49    "const {x={y=33}}={};",
     50    "var {x=1};",
     51    "let {x=1};",
     52    "let x, y, {z=1}={}, {w=2}, {e=3};",
     53    // array initialization
     54    "[{x=1, y = ({z=2} = {})}];",
     55    // try/catch
     56    "try {throw 'a';} catch ({x={y=1}}) {}",
     57    // if/else
     58    "if ({k: 1, x={y=2}={}}) {}",
     59    "if (false) {} else if (true) { ({x=1}) }",
     60    // switch
     61    "switch ('c') { case 'c': ({x=1}); }",
     62    // for
     63    "for ({x=1}; 1;) {1}",
     64    "for ({x={y=2}}; 1;) {1}",
     65    "for (var x = 0; x < 2; x++) { ({x=1, y=2}) }",
     66    "for (let x=1;{x=1};){}",
     67    "for (let x=1;{x={y=2}};){}",
     68    "for (let x=1;1;{x=1}){}",
     69    "for (let x=1;1;{x={y=2}}){}",
     70    // while
     71    "while ({x=1}) {1};",
     72    "while ({x={y=2}}={}) {1};",
     73    // with
     74    "with ({x=1}) {};",
     75    "with ({x={y=3}={}}) {};",
     76    "with (Math) { ({x=1}) };",
     77    // ternary
     78    "true ? {x=1} : 1;",
     79    "false ? 1 : {x=1};",
     80    "{x=1} ? 2 : 3;",
     81    // assignment
     82    "({x} += {});",
     83    "({x = 1}) = {x: 2};",
     84 ]
     85 
     86 for (var stmt of SYNTAX_ERROR_STMTS) {
     87    assertThrowsInstanceOf(() => {
     88        Function(stmt);
     89    }, SyntaxError);
     90 }
     91 
     92 // A few tricky but acceptable cases:
     93 // see https://bugzilla.mozilla.org/show_bug.cgi?id=932080#c2
     94 
     95 assertEq((({a = 0}) => a)({}), 0);
     96 assertEq((({a = 0} = {}) => a)({}), 0);
     97 assertEq((({a = 0} = {}) => a)({a: 1}), 1);
     98 
     99 {
    100    let x, y;
    101    ({x=1} = {});
    102    assertEq(x, 1);
    103    ({x=1} = {x: 4});
    104    assertEq(x, 4);
    105    ({x=1, y=2} = {})
    106    assertEq(x, 1);
    107    assertEq(y, 2);
    108 }
    109 
    110 {
    111    let {x={i=1, j=2}={}}={};
    112    assertDeepEq(x, ({}));
    113    assertEq(i, 1);
    114    assertEq(j, 2);
    115 }
    116 
    117 // Default destructuring values, which are variables, should be defined
    118 // within closures (Bug 1255167).
    119 {
    120    let f = function(a){
    121        return (function({aa = a}){ return aa; })({});
    122    };
    123    assertEq(f(9999), 9999);
    124 }
    125 
    126 if (typeof reportCompare == "function")
    127    reportCompare(true, true);