summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/expressions/optional-chain-first-expression.js
blob: 89912aec4afb49e9e2995084f1f208b4b97671c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Verify bytecode emitter accepts all valid optional chain first expressions.

const expressions = [
  // https://tc39.es/ecma262/#sec-primary-expression
  "this",
  "ident",
  "null",
  "true",
  "false",
  "123",
  "123n",
  "'str'",
  "[]",
  "{}",
  "function(){}",
  "class{}",
  "function*(){}",
  "async function(){}",
  "async function*(){}",
  "/a/",
  "`str`",
  "(a + b)",

  // https://tc39.es/ecma262/#sec-left-hand-side-expressions
  "a[b]",
  "a.b",
  "a``",
  "super[a]",
  "super.a",
  "new.target",
  "import.meta",
  "new C()",
  "new C",
  "f()",
  "super()",
  "a?.b",
  "a?.[b]",
  "a?.()",
  "a?.``",
];

function tryParse(s, f = Function) {
  try { f(s); } catch {}
}

function tryRun(s, f = Function) {
  try { f(s)(); } catch {}
}

for (let expr of expressions) {
  // Evaluate in an expression context.
  tryRun(`void (${expr}?.());`);
  tryRun(`void (${expr}?.p());`);

  // Also try parenthesized.
  tryRun(`void ((${expr})?.());`);
  tryRun(`void ((${expr})?.p());`);
}

function inClassConstructor(s) {
  return `class C { constructor() { ${s} } }`;
}

for (let expr of ["super[a]", "super.a", "super()"]) {
  // Evaluate in an expression context.
  tryRun(inClassConstructor(`void (${expr}?.());`));
  tryRun(inClassConstructor(`void (${expr}?.p());`));

  // Also try parenthesized.
  tryRun(inClassConstructor(`void ((${expr})?.());`));
  tryRun(inClassConstructor(`void ((${expr})?.p());`));
}

if (typeof parseModule === "function") {
  const expressions = [
    "import.meta",
    "import('')",
  ];

  for (let expr of expressions) {
    // Evaluate in an expression context.
    tryParse(`void (${expr}?.());`, parseModule);
    tryParse(`void (${expr}?.p());`, parseModule);

    // Also try parenthesized.
    tryParse(`void ((${expr})?.());`, parseModule);
    tryParse(`void ((${expr})?.p());`, parseModule);
  }
}

if (typeof reportCompare === "function")
  reportCompare(true, true);