diff --git a/test/language/expressions/optional-chaining/call-expression.js b/test/language/expressions/optional-chaining/call-expression.js new file mode 100644 index 0000000000..fe8317c965 --- /dev/null +++ b/test/language/expressions/optional-chaining/call-expression.js @@ -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); diff --git a/test/language/expressions/optional-chaining/early-errors-tail-position-template-string-esi.js b/test/language/expressions/optional-chaining/early-errors-tail-position-template-string-esi.js new file mode 100644 index 0000000000..5ae907654a --- /dev/null +++ b/test/language/expressions/optional-chaining/early-errors-tail-position-template-string-esi.js @@ -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` diff --git a/test/language/expressions/optional-chaining/early-errors-tail-position-template-string.js b/test/language/expressions/optional-chaining/early-errors-tail-position-template-string.js new file mode 100644 index 0000000000..4d9befcc9e --- /dev/null +++ b/test/language/expressions/optional-chaining/early-errors-tail-position-template-string.js @@ -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`; diff --git a/test/language/expressions/optional-chaining/member-expression.js b/test/language/expressions/optional-chaining/member-expression.js new file mode 100644 index 0000000000..93fcf2931a --- /dev/null +++ b/test/language/expressions/optional-chaining/member-expression.js @@ -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(); +}); diff --git a/test/language/expressions/optional-chaining/optional-expression.js b/test/language/expressions/optional-chaining/optional-expression.js new file mode 100644 index 0000000000..eb9026557a --- /dev/null +++ b/test/language/expressions/optional-chaining/optional-expression.js @@ -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); diff --git a/test/language/expressions/optional-chaining/punctuator-decimal-lookahead.js b/test/language/expressions/optional-chaining/punctuator-decimal-lookahead.js new file mode 100644 index 0000000000..8cb007e459 --- /dev/null +++ b/test/language/expressions/optional-chaining/punctuator-decimal-lookahead.js @@ -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); diff --git a/test/language/expressions/optional-chaining/runtime-semantics-evaluation.js b/test/language/expressions/optional-chaining/runtime-semantics-evaluation.js new file mode 100644 index 0000000000..9062e2c5a4 --- /dev/null +++ b/test/language/expressions/optional-chaining/runtime-semantics-evaluation.js @@ -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); diff --git a/test/language/expressions/optional-chaining/short-circuiting.js b/test/language/expressions/optional-chaining/short-circuiting.js new file mode 100644 index 0000000000..0b0a1e0d8d --- /dev/null +++ b/test/language/expressions/optional-chaining/short-circuiting.js @@ -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); diff --git a/test/language/expressions/optional-chaining/static-semantics-simple-assignment.js b/test/language/expressions/optional-chaining/static-semantics-simple-assignment.js new file mode 100644 index 0000000000..7eb974455c --- /dev/null +++ b/test/language/expressions/optional-chaining/static-semantics-simple-assignment.js @@ -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;