tor-browser

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

for-of-async-of-starting-lhs.js (3687B)


      1 if (typeof getRealmConfiguration === "undefined") {
      2  var getRealmConfiguration = SpecialPowers.Cu.getJSTestingFunctions().getRealmConfiguration;
      3 }
      4 
      5 
      6 const AsyncFunction = async function(){}.constructor;
      7 
      8 function assertNoError(f, msg) {
      9  try {
     10    f();
     11  } catch (e) {
     12    assertEq(true, false, `${msg}: ${e}`);
     13  }
     14 }
     15 
     16 function assertSyntaxError(code) {
     17  assertThrowsInstanceOf(function () { Function(code); }, SyntaxError, "Function:" + code);
     18  assertThrowsInstanceOf(function () { AsyncFunction(code); }, SyntaxError, "AsyncFunction:" + code);
     19 
     20  if (typeof parseModule === "function") {
     21    assertThrowsInstanceOf(function () { parseModule(code); }, SyntaxError, "Module:" + code);
     22  }
     23 }
     24 
     25 function assertNoSyntaxError(code) {
     26  assertNoError(function () { Function(code); }, "Function:" + code);
     27  assertNoError(function () { AsyncFunction(code); }, "AsyncFunction:" + code);
     28 
     29  if (typeof parseModule === "function") {
     30    assertNoError(function () { parseModule(code); }, "Module:" + code);
     31  }
     32 }
     33 
     34 function assertNoSyntaxErrorAsyncContext(code) {
     35  assertNoError(function () { AsyncFunction(code); }, "AsyncFunction:" + code);
     36 
     37  if (typeof parseModule === "function") {
     38    assertNoError(function () { parseModule(code); }, "Module:" + code);
     39  }
     40 }
     41 
     42 const invalidTestCases = [
     43  // for-in loop: LHS can't start with an async arrow function.
     44  "for ( async of => {} in [] ) ;",
     45  "for ( async o\\u0066 => {} in [] ) ;",
     46 
     47  // for-of loop: LHS can't start with an async arrow function.
     48  "for ( async of => {} of [] ) ;",
     49  "for ( async o\\u0066 => {} of [] ) ;",
     50 
     51  // for-of loop: LHS can't start with an identifier named "async".
     52  "for ( async of [] ) ;",
     53 
     54  // for-await-of loop: LHS can't start with an async arrow function.
     55  "for await ( async of => {} of [] ) ;",
     56  "for await ( async o\\u0066 => {} of [] ) ;",
     57 ];
     58 
     59 for (let source of invalidTestCases) {
     60  assertSyntaxError(source);
     61 
     62  // Also test when the tokens are separated by newline characters.
     63  assertSyntaxError(source.split(" ").join("\n"));
     64 }
     65 
     66 // for-loop: async arrow functions are allowed in C-style for-loops.
     67 assertNoSyntaxError("for ( async of => {} ; ; ) ;")
     68 
     69 const validTestCases = [
     70  // for-loop: LHS can start with an identifier named "async".
     71  "for ( async ; ; ) ;",
     72  "for ( \\u0061sync ; ; ) ;",
     73 
     74  // for-in loop: LHS can start with an identifier named "async".
     75  "for ( async in [] ) ;",
     76  "for ( \\u0061sync in [] ) ;",
     77 
     78  // for-in loop: LHS can start with an property assignment starting with "async".
     79  "for ( async . prop in [] ) ;",
     80  "for ( async [ 0 ] in [] ) ;",
     81 
     82  // for-of loop: LHS can start with an identifier named "async" when escape characters are used.
     83  "for ( \\u0061sync of [] ) ;",
     84 
     85  // for-of loop: LHS can start with an property assignment starting with "async".
     86  "for ( async . prop of [] ) ;",
     87  "for ( async [ 0 ] of [] ) ;",
     88 ];
     89 
     90 for (let source of validTestCases) {
     91  assertNoSyntaxError(source);
     92 
     93  // Also test when the tokens are separated by newline characters.
     94  assertNoSyntaxError(source.split(" ").join("\n"));
     95 }
     96 
     97 const validTestCasesAsync = [
     98  // for-await-of loop: LHS can start with an identifier named "async".
     99  "for await ( async of [] ) ;",
    100  "for await ( \\u0061sync of [] ) ;",
    101 
    102  // for-await-of loop: LHS can start with an property assignment starting with "async".
    103  "for await ( async . prop of [] ) ;",
    104  "for await ( async [ 0 ] of [] ) ;",
    105 ];
    106 
    107 for (let source of validTestCasesAsync) {
    108  assertNoSyntaxErrorAsyncContext(source);
    109 
    110  // Also test when the tokens are separated by newline characters.
    111  assertNoSyntaxErrorAsyncContext(source.split(" ").join("\n"));
    112 }
    113 
    114 if (typeof reportCompare === "function")
    115  reportCompare(true, true);