feat: adding tests for optional chaining proposal (#2212)

This commit is contained in:
Benjamin E. Coe 2019-08-05 08:10:25 -07:00 committed by Leo Balter
parent c4dd26bcee
commit aae991da8a
9 changed files with 230 additions and 0 deletions

View File

@ -0,0 +1,20 @@
// 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]
---*/
function fn () {
return {a: 33};
};
// CallExpression Arguments
assert.sameValue(33, fn()?.a);
assert.sameValue(undefined, fn()?.b);

View File

@ -0,0 +1,25 @@
// Copyright 2019 Google, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
template string passed to tail position of optional chain
info: |
Static Semantics: Early Errors
OptionalChain:
?.TemplateLiteral
OptionalChain TemplateLiteral
features: [optional-chaining]
negative:
type: SyntaxError
phase: parse
---*/
$DONOTEVALUATE();
const a = {fn() {}};
// This production exists in order to prevent automatic semicolon
// insertion rules.
a?.fn
`hello`

View File

@ -0,0 +1,24 @@
// Copyright 2019 Google, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
template string passed to tail position of optional chain
info: |
Static Semantics: Early Errors
OptionalChain:
?.TemplateLiteral
OptionalChain TemplateLiteral
features: [optional-chaining]
negative:
type: SyntaxError
phase: parse
---*/
$DONOTEVALUATE();
const a = {fn() {}};
// This production exists in order to prevent automatic semicolon
// insertion rules.
a?.fn`hello`;

View File

@ -0,0 +1,56 @@
// 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 member expression
info: |
Left-Hand-Side Expressions
OptionalExpression:
MemberExpression OptionalChain
features: [optional-chaining]
---*/
// PrimaryExpression
// IdentifierReference
const arr = [10, 11];
const fn = (arg1, arg2) => {
return arg1 + arg2;
}
const i = 0;
const obj = {
a: 'hello',
b: {val: 13},
c(arg1) {
return arg1 * 2;
},
arr: [11, 12]
};
// OptionalChain: ?.[Expression]
assert.sameValue(11, arr?.[i + 1]);
// OptionalChain: ?.IdentifierName
assert.sameValue('hello', obj?.a);
// OptionalChain: ?.Arguments
assert.sameValue(30, fn?.(10, 20));
// OptionalChain: OptionalChain [Expression]
assert.sameValue(12, obj?.arr[i + 1]);
assert.throws(TypeError, function() {
obj?.d[i + 1];
});
// OptionalChain: OptionalChain .IdentifierName
assert.sameValue(13, obj?.b.val);
assert.throws(TypeError, function() {
obj?.d.e;
});
// OptionalChain: OptionalChain Arguments
assert.sameValue(20, obj?.c(10));
assert.throws(TypeError, function() {
obj?.d();
});

View File

@ -0,0 +1,27 @@
// 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 recursive optional expression
info: |
Left-Hand-Side Expressions
OptionalExpression:
OptionalExpression OptionalChain
features: [optional-chaining]
---*/
const obj = {
a: {
b: 22
}
};
function fn () {
return {};
}
// MemberExpression
assert.sameValue(22, (obj?.a)?.b);
// CallExpression
assert.sameValue(undefined, (fn()?.a)?.b);

View File

@ -0,0 +1,15 @@
// Copyright 2019 Google, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
ternary operation with decimal does not evaluate as optional chain
info: |
Punctuators
OptionalChainingPunctuator::
?.[lookahead DecimalDigit]
features: [optional-chaining]
---*/
const value = true ?.30 : false;
assert.sameValue(.30, value);

View File

@ -0,0 +1,18 @@
// Copyright 2019 Google, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
accessing optional value on undefined or null returns undefined.
info: |
If baseValue is undefined or null, then
Return undefined.
features: [optional-chaining]
---*/
const nul = null;
const undf = undefined;
assert.sameValue(undefined, nul?.a);
assert.sameValue(undefined, undf?.b);
assert.sameValue(undefined, null?.a);
assert.sameValue(undefined, undefined?.b);

View File

@ -0,0 +1,22 @@
// Copyright 2019 Google, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
demonstrate syntax-based short-circuiting.
info: |
If the expression on the LHS of ?. evaluates to null/undefined, the RHS is
not evaluated
features: [optional-chaining]
---*/
const a = undefined;
let x = 1;
a?.[++x] // short-circuiting.
a?.b.c(++x).d; // long short-circuiting.
undefined?.[++x] // short-circuiting.
undefined?.b.c(++x).d; // long short-circuiting.
assert.sameValue(1, x);

View File

@ -0,0 +1,23 @@
// Copyright 2019 Google, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
an optional expression cannot be target of assignment
info: |
Static Semantics: IsValidSimpleAssignmentTarget
LeftHandSideExpression:
OptionalExpression
Return false.
features: [optional-chaining]
negative:
type: SyntaxError
phase: parse
---*/
$DONOTEVALUATE();
const obj = {};
obj?.a = 33;