tor-browser

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

trailing_comma_getter_setter.js (2438B)


      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 // Trailing comma is not allowed in getter and setter methods
      6 
      7 // 14.3 Method Definitions
      8 // MethodDefinition[Yield]:
      9 //   get PropertyName[?Yield] () { FunctionBody[~Yield] }
     10 //   set PropertyName[?Yield] ( PropertySetParameterList ) { FunctionBody[~Yield] }
     11 // PropertySetParameterList:
     12 //   FormalParameter[~Yield]
     13 
     14 function objectGetter(argList) {
     15    return eval(`({
     16        get m(${argList}) {}
     17    })`);
     18 }
     19 
     20 function objectSetter(argList) {
     21    return eval(`({
     22        set m(${argList}) {}
     23    })`);
     24 }
     25 
     26 function classGetter(argList) {
     27    return eval(`(class {
     28        get m(${argList}) {}
     29    })`);
     30 }
     31 
     32 function classStaticGetter(argList) {
     33    return eval(`(class {
     34        static get m(${argList}) {}
     35    })`);
     36 }
     37 
     38 function classSetter(argList) {
     39    return eval(`(class {
     40        set m(${argList}) {}
     41    })`);
     42 }
     43 
     44 function classStaticSetter(argList) {
     45    return eval(`(class {
     46        static set m(${argList}) {}
     47    })`);
     48 }
     49 
     50 const tests = [
     51    objectGetter,
     52    objectSetter,
     53    classGetter,
     54    classStaticGetter,
     55    classSetter,
     56    classStaticSetter,
     57 ];
     58 
     59 for (let test of tests) {
     60    // Trailing comma.
     61    assertThrowsInstanceOf(() => test("a, "), SyntaxError);
     62    assertThrowsInstanceOf(() => test("[], "), SyntaxError);
     63    assertThrowsInstanceOf(() => test("{}, "), SyntaxError);
     64    assertThrowsInstanceOf(() => test("a = 0, "), SyntaxError);
     65    assertThrowsInstanceOf(() => test("[] = [], "), SyntaxError);
     66    assertThrowsInstanceOf(() => test("{} = {}, "), SyntaxError);
     67 
     68    // Trailing comma in empty parameters list.
     69    assertThrowsInstanceOf(() => test(","), SyntaxError);
     70 
     71    // Leading comma.
     72    assertThrowsInstanceOf(() => test(", a"), SyntaxError);
     73    assertThrowsInstanceOf(() => test(", ...a"), SyntaxError);
     74 
     75    // Multiple trailing comma.
     76    assertThrowsInstanceOf(() => test("a, ,"), SyntaxError);
     77    assertThrowsInstanceOf(() => test("a..., ,"), SyntaxError);
     78 
     79    // Trailing comma after rest parameter.
     80    assertThrowsInstanceOf(() => test("...a ,"), SyntaxError);
     81    assertThrowsInstanceOf(() => test("a, ...b, "), SyntaxError);
     82 
     83    // Elision.
     84    assertThrowsInstanceOf(() => test("a, , b"), SyntaxError);
     85 }
     86 
     87 if (typeof reportCompare === "function")
     88    reportCompare(0, 0);