summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/language/expressions/optional-chaining/call-expression.js
blob: aa3f4abeb5c4044db668a83f7de58dc7de56abbd (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
// Copyright 2019 Google, Inc.  All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
  optional chain on call expression
info: |
  Left-Hand-Side Expressions
    OptionalExpression:
      CallExpression OptionalChain
features: [optional-chaining]
---*/

// CallExpression CoverCallExpressionAndAsyncArrowHead
function fn () {
  return {a: 33};
};
const obj = {
  fn () {
    return 44;
  }
}
assert.sameValue(33, fn()?.a);
assert.sameValue(undefined, fn()?.b);
assert.sameValue(44, obj?.fn());

// CallExpression SuperCall
class A {}
class B extends A {
  constructor () {
    assert.sameValue(undefined, super()?.a);
  }
}
new B();

// CallExpression Arguments
function fn2 () {
  return () => {
    return {a: 66};
  };
}
function fn3 () {
  return () => {
    return null;
  };
}
assert.sameValue(66, fn2()()?.a);
assert.sameValue(undefined, fn3()()?.a);

// CallExpression [Expression]
function fn4 () {
  return [{a: 77}];
}
function fn5 () {
  return [];
}
assert.sameValue(77, fn4()[0]?.a);
assert.sameValue(undefined, fn5()[0]?.a);

// CallExpression .IdentifierName
function fn6 () {
  return {
    a: {
      b: 88
    }
  };
}
assert.sameValue(88, fn6().a?.b);
assert.sameValue(undefined, fn6().b?.c);

// CallExpression TemplateLiteral
function fn7 () {
  return () => {};
}
assert.sameValue(undefined, fn7()`hello`?.a);

reportCompare(0, 0);