member-expression.js (2556B)
1 // Copyright 2019 Google, Inc. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 esid: prod-OptionalExpression 5 description: > 6 optional chain on member expression 7 info: | 8 Left-Hand-Side Expressions 9 OptionalExpression: 10 MemberExpression OptionalChain 11 features: [optional-chaining] 12 ---*/ 13 14 // PrimaryExpression 15 // IdentifierReference 16 const a = {b: 22}; 17 assert.sameValue(22, a?.b); 18 // this 19 function fn () { 20 return this?.a 21 } 22 assert.sameValue(33, fn.call({a: 33})); 23 // Literal 24 assert.sameValue(undefined, "hello"?.a); 25 assert.sameValue(undefined, null?.a); 26 // ArrayLiteral 27 assert.sameValue(2, [1, 2]?.[1]); 28 // ObjectLiteral 29 assert.sameValue(44, {a: 44}?.a); 30 // FunctionExpression 31 assert.sameValue('a', (function a () {}?.name)); 32 // ClassExpression 33 assert.sameValue('Foo', (class Foo {}?.name)); 34 // GeneratorFunction 35 assert.sameValue('a', (function * a () {}?.name)); 36 // AsyncFunctionExpression 37 assert.sameValue('a', (async function a () {}?.name)); 38 // AsyncGeneratorExpression 39 assert.sameValue('a', (async function * a () {}?.name)); 40 // RegularExpressionLiteral 41 assert.sameValue(true, /[a-z]/?.test('a')); 42 // TemplateLiteral 43 assert.sameValue('h', `hello`?.[0]); 44 // CoverParenthesizedExpressionAndArrowParameterList 45 assert.sameValue(undefined, ({a: 33}, null)?.a); 46 assert.sameValue(33, (undefined, {a: 33})?.a); 47 48 // MemberExpression [ Expression ] 49 const arr = [{a: 33}]; 50 assert.sameValue(33, arr[0]?.a); 51 assert.sameValue(undefined, arr[1]?.a); 52 53 // MemberExpression .IdentifierName 54 const obj = {a: {b: 44}}; 55 assert.sameValue(44, obj.a?.b); 56 assert.sameValue(undefined, obj.c?.b); 57 58 // MemberExpression TemplateLiteral 59 function f2 () { 60 return {a: 33}; 61 } 62 function f3 () {} 63 assert.sameValue(33, f2`hello world`?.a); 64 assert.sameValue(undefined, f3`hello world`?.a); 65 66 // MemberExpression SuperProperty 67 class A { 68 a () {} 69 undf () { 70 return super.a?.c; 71 } 72 } 73 class B extends A { 74 dot () { 75 return super.a?.name; 76 } 77 expr () { 78 return super['a']?.name; 79 } 80 undf2 () { 81 return super.b?.c; 82 } 83 } 84 const subcls = new B(); 85 assert.sameValue('a', subcls.dot()); 86 assert.sameValue('a', subcls.expr()); 87 assert.sameValue(undefined, subcls.undf2()); 88 assert.sameValue(undefined, (new A()).undf()); 89 90 // MemberExpression MetaProperty 91 class C { 92 constructor () { 93 assert.sameValue(undefined, new.target?.a); 94 } 95 } 96 new C(); 97 98 // new MemberExpression Arguments 99 class D { 100 constructor (val) { 101 this.a = val; 102 } 103 } 104 assert.sameValue(99, new D(99)?.a); 105 106 reportCompare(0, 0);