tor-browser

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

non-simple-with-strict-directive.js (4026B)


      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 const testCases = [
      6    // Array destructuring.
      7    "[]",
      8    "[a]",
      9    "x, [a]",
     10    "[a], x",
     11 
     12    // Array destructuring with defaults.
     13    "[a = 0]",
     14    "x, [a = 0]",
     15    "[a = 0], x",
     16 
     17    // Array destructuring with rest binding identifier.
     18    "[...a]",
     19    "x, [...a]",
     20    "[...a], x",
     21 
     22    // Array destructuring with rest binding pattern.
     23    "[...[a]]",
     24    "x, [...[a]]",
     25    "[...[a]], x",
     26 
     27    // Object destructuring.
     28    "{}",
     29    "{p: o}",
     30    "x, {p: o}",
     31    "{p: o}, x",
     32 
     33    // Object destructuring with defaults.
     34    "{p: o = 0}",
     35    "x, {p: o = 0}",
     36    "{p: o = 0}, x",
     37 
     38    // Object destructuring with shorthand identifier form.
     39    "{o}",
     40    "x, {o}",
     41    "{o}, x",
     42 
     43    // Object destructuring with CoverInitName.
     44    "{o = 0}",
     45    "x, {o = 0}",
     46    "{o = 0}, x",
     47 
     48    // Default parameter.
     49    "d = 0",
     50    "x, d = 0",
     51    "d = 0, x",
     52 
     53    // Rest parameter.
     54    "...rest",
     55    "x, ...rest",
     56 
     57    // Rest parameter with array destructuring.
     58    "...[]",
     59    "...[a]",
     60    "x, ...[]",
     61    "x, ...[a]",
     62 
     63    // Rest parameter with object destructuring.
     64    "...{}",
     65    "...{p: o}",
     66    "x, ...{}",
     67    "x, ...{p: o}",
     68 
     69    // All non-simple cases combined.
     70    "x, d = 123, [a], {p: 0}, ...rest",
     71 ];
     72 
     73 const GeneratorFunction = function*(){}.constructor;
     74 
     75 const functionDefinitions = [
     76    // FunctionDeclaration
     77    parameters => `function f(${parameters}) { "use strict"; }`,
     78 
     79    // FunctionExpression
     80    parameters => `void function(${parameters}) { "use strict"; };`,
     81    parameters => `void function f(${parameters}) { "use strict"; };`,
     82 
     83    // Function constructor
     84    parameters => `Function('${parameters}', '"use strict";')`,
     85 
     86    // GeneratorDeclaration
     87    parameters => `function* g(${parameters}) { "use strict"; }`,
     88 
     89    // GeneratorExpression
     90    parameters => `void function*(${parameters}) { "use strict"; };`,
     91    parameters => `void function* g(${parameters}) { "use strict"; };`,
     92 
     93    // GeneratorFunction constructor
     94    parameters => `GeneratorFunction('${parameters}', '"use strict";')`,
     95 
     96    // MethodDefinition
     97    parameters => `({ m(${parameters}) { "use strict"; } });`,
     98    parameters => `(class { m(${parameters}) { "use strict"; } });`,
     99    parameters => `class C { m(${parameters}) { "use strict"; } }`,
    100 
    101    // MethodDefinition (constructors)
    102    parameters => `(class { constructor(${parameters}) { "use strict"; } });`,
    103    parameters => `class C { constructor(${parameters}) { "use strict"; } }`,
    104 
    105    // MethodDefinition (getter)
    106    parameters => `({ get m(${parameters}) { "use strict"; } });`,
    107    parameters => `(class { get m(${parameters}) { "use strict"; } });`,
    108    parameters => `class C { get m(${parameters}) { "use strict"; } }`,
    109 
    110    // MethodDefinition (setter)
    111    parameters => `({ set m(${parameters}) { "use strict"; } });`,
    112    parameters => `(class { set m(${parameters}) { "use strict"; } });`,
    113    parameters => `class C { set m(${parameters}) { "use strict"; } }`,
    114 
    115    // GeneratorMethod
    116    parameters => `({ *m(${parameters}) { "use strict"; } });`,
    117    parameters => `(class { *m(${parameters}) { "use strict"; } });`,
    118    parameters => `class C { *m(${parameters}) { "use strict"; } }`,
    119 
    120    // ArrowFunction
    121    parameters => `(${parameters}) => { "use strict"; };`,
    122 ];
    123 
    124 for (let nonSimpleParameters of testCases) {
    125    for (let def of functionDefinitions) {
    126        // Non-strict script code.
    127        assertThrowsInstanceOf(() => eval(`
    128            ${def(nonSimpleParameters)}
    129        `), SyntaxError, def(nonSimpleParameters));
    130 
    131        // Strict script code.
    132        assertThrowsInstanceOf(() => eval(`
    133            "use strict";
    134            ${def(nonSimpleParameters)}
    135        `), SyntaxError, `"use strict"; ${def(nonSimpleParameters)}`);
    136    }
    137 }
    138 
    139 if (typeof reportCompare === 'function')
    140    reportCompare(0, 0);