mirror of https://github.com/tc39/test262.git
Expand tests for Optional Chaining
This commit is contained in:
parent
6179359305
commit
89f9c13449
|
@ -0,0 +1,25 @@
|
||||||
|
// Copyright 2020 Salesforce.com, 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
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
|
features: [optional-chaining]
|
||||||
|
negative:
|
||||||
|
type: SyntaxError
|
||||||
|
phase: parse
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
// This production exists in order to prevent automatic semicolon
|
||||||
|
// insertion rules.
|
||||||
|
null?.
|
||||||
|
`hello`
|
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright 2020 Salesforce.com, 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
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
|
features: [optional-chaining]
|
||||||
|
negative:
|
||||||
|
type: SyntaxError
|
||||||
|
phase: parse
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
null?.`hello`;
|
|
@ -0,0 +1,25 @@
|
||||||
|
// Copyright 2020 Salesforce.com, 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
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
|
features: [optional-chaining]
|
||||||
|
negative:
|
||||||
|
type: SyntaxError
|
||||||
|
phase: parse
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
// This production exists in order to prevent automatic semicolon
|
||||||
|
// insertion rules.
|
||||||
|
null?.fn
|
||||||
|
`hello`
|
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright 2020 Salesforce.com, 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
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
|
features: [optional-chaining]
|
||||||
|
negative:
|
||||||
|
type: SyntaxError
|
||||||
|
phase: parse
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
null?.fn`hello`;
|
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2020 Salesforce.com, 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
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
|
features: [optional-chaining]
|
||||||
|
negative:
|
||||||
|
type: SyntaxError
|
||||||
|
phase: parse
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
const a = function() {};
|
||||||
|
|
||||||
|
// This production exists in order to prevent automatic semicolon
|
||||||
|
// insertion rules.
|
||||||
|
a?.
|
||||||
|
`hello`
|
|
@ -0,0 +1,24 @@
|
||||||
|
// Copyright 2020 Salesforce.com, 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
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
|
features: [optional-chaining]
|
||||||
|
negative:
|
||||||
|
type: SyntaxError
|
||||||
|
phase: parse
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
const a = function() {};
|
||||||
|
|
||||||
|
a?.`hello`;
|
|
@ -9,6 +9,8 @@ info: |
|
||||||
OptionalChain:
|
OptionalChain:
|
||||||
?.TemplateLiteral
|
?.TemplateLiteral
|
||||||
OptionalChain TemplateLiteral
|
OptionalChain TemplateLiteral
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
features: [optional-chaining]
|
features: [optional-chaining]
|
||||||
negative:
|
negative:
|
||||||
type: SyntaxError
|
type: SyntaxError
|
|
@ -9,6 +9,8 @@ info: |
|
||||||
OptionalChain:
|
OptionalChain:
|
||||||
?.TemplateLiteral
|
?.TemplateLiteral
|
||||||
OptionalChain TemplateLiteral
|
OptionalChain TemplateLiteral
|
||||||
|
|
||||||
|
It is a Syntax Error if any code matches this production.
|
||||||
features: [optional-chaining]
|
features: [optional-chaining]
|
||||||
negative:
|
negative:
|
||||||
type: SyntaxError
|
type: SyntaxError
|
||||||
|
@ -19,6 +21,4 @@ $DONOTEVALUATE();
|
||||||
|
|
||||||
const a = {fn() {}};
|
const a = {fn() {}};
|
||||||
|
|
||||||
// This production exists in order to prevent automatic semicolon
|
|
||||||
// insertion rules.
|
|
||||||
a?.fn`hello`;
|
a?.fn`hello`;
|
|
@ -4,16 +4,36 @@
|
||||||
esid: sec-optional-chaining-chain-evaluation
|
esid: sec-optional-chaining-chain-evaluation
|
||||||
description: optional call invoked on eval function should be indirect eval.
|
description: optional call invoked on eval function should be indirect eval.
|
||||||
info: |
|
info: |
|
||||||
12.3.9.2 Runtime Semantics: ChainEvaluation
|
Runtime Semantics: ChainEvaluation
|
||||||
OptionalChain: ?. Arguments
|
OptionalChain: ?. Arguments
|
||||||
1. Let thisChain be this OptionalChain.
|
1. Let thisChain be this OptionalChain.
|
||||||
2. Let tailCall be IsInTailPosition(thisChain).
|
2. Let tailCall be IsInTailPosition(thisChain).
|
||||||
3. Return ? EvaluateCall(baseValue, baseReference, Arguments, tailCall).
|
3. Return ? EvaluateCall(baseValue, baseReference, Arguments, tailCall).
|
||||||
|
|
||||||
|
Runtime Semantics: EvaluateCall ( func, ref, arguments, tailPosition )
|
||||||
|
|
||||||
|
...
|
||||||
|
7. Let result be Call(func, thisValue, argList).
|
||||||
|
...
|
||||||
|
|
||||||
|
eval ( x )
|
||||||
|
|
||||||
|
...
|
||||||
|
4. Return ? PerformEval(x, callerRealm, false, false).
|
||||||
|
|
||||||
|
Runtime Semantics: PerformEval ( x, callerRealm, strictCaller, direct )
|
||||||
features: [optional-chaining]
|
features: [optional-chaining]
|
||||||
---*/
|
---*/
|
||||||
|
|
||||||
const a = "global";
|
const a = 'global';
|
||||||
const b = (a => eval?.("a"))("local")
|
|
||||||
|
|
||||||
assert.sameValue(b, a);
|
function fn() {
|
||||||
|
const a = 'local';
|
||||||
|
return eval?.('a');
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(fn(), 'global', 'fn() returns "global" value from indirect eval');
|
||||||
|
|
||||||
|
const b = (a => eval?.('a'))('local');
|
||||||
|
|
||||||
|
assert.sameValue(b, 'global', 'b is "global", from indirect eval not observing parameter');
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
// Copyright 2020 Salesforce.com, Inc. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: prod-OptionalExpression
|
||||||
|
description: >
|
||||||
|
Productions for ?. Arguments
|
||||||
|
info: |
|
||||||
|
OptionalChain[Yield, Await]:
|
||||||
|
?. Arguments
|
||||||
|
features: [optional-chaining]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
function fn(arg1, arg2, arg3 = 0) {
|
||||||
|
return arg1 + arg2 + arg3;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(fn?.(10, 20), 30, 'regular');
|
||||||
|
assert.sameValue(String?.(42), '42', 'built-in');
|
||||||
|
assert.sameValue(fn ?. (...[10, 20, 40]), 70, 'spread');
|
|
@ -0,0 +1,42 @@
|
||||||
|
// Copyright 2020 Salesforce.com, Inc. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: prod-OptionalExpression
|
||||||
|
description: >
|
||||||
|
Productions for ?. [Expression]
|
||||||
|
info: |
|
||||||
|
OptionalChain:
|
||||||
|
?.[ Expression ]
|
||||||
|
features: [optional-chaining]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const $ = 'x';
|
||||||
|
const arr = [39, 42];
|
||||||
|
|
||||||
|
arr.true = 'prop';
|
||||||
|
arr[1.1] = 'other prop';
|
||||||
|
|
||||||
|
const obj = {
|
||||||
|
a: 'hello',
|
||||||
|
undefined: 40,
|
||||||
|
$: 0,
|
||||||
|
NaN: 41,
|
||||||
|
null: 42,
|
||||||
|
x: 43,
|
||||||
|
true: 44
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.sameValue(arr?.[0], 39, '[0]');
|
||||||
|
assert.sameValue(arr?.[0, 1], 42, '[0, 1]');
|
||||||
|
assert.sameValue(arr?.[1], 42, '[1]');
|
||||||
|
assert.sameValue(arr?.[1, 0], 39, '[1, 0]');
|
||||||
|
assert.sameValue(arr?.[{}, NaN, undefined, 2, 0, 10 / 10], 42, '[{}, NaN, undefined, 2, 0, 10 / 10]');
|
||||||
|
assert.sameValue(arr?.[true], 'prop', '[true]');
|
||||||
|
assert.sameValue(arr?.[1.1], 'other prop', '[1.1]');
|
||||||
|
|
||||||
|
assert.sameValue(obj?.[undefined], 40, '[undefined]');
|
||||||
|
assert.sameValue(obj?.[NaN], 41, '[NaN]');
|
||||||
|
assert.sameValue(obj?.[null], 42, '[null]');
|
||||||
|
assert.sameValue(obj?.['$'], 0, '["$"]');
|
||||||
|
assert.sameValue(obj?.[$], 43, '[$]');
|
||||||
|
assert.sameValue(obj?.[true], 44, '[true]');
|
|
@ -0,0 +1,38 @@
|
||||||
|
// Copyright 2020 Salesforce.com, Inc. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
esid: prod-OptionalExpression
|
||||||
|
description: >
|
||||||
|
Productions for ?. IdentifierName
|
||||||
|
info: |
|
||||||
|
OptionalChain[Yield, Await]:
|
||||||
|
?. IdentifierName
|
||||||
|
features: [optional-chaining]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const arr = [10, 11];
|
||||||
|
const obj = {
|
||||||
|
a: 'hello'
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.sameValue(obj?.a, 'hello');
|
||||||
|
assert.sameValue(obj?.\u0061, 'hello');
|
||||||
|
assert.sameValue(obj?.\u{0061}, 'hello');
|
||||||
|
|
||||||
|
assert.sameValue(obj?.\u0062, undefined);
|
||||||
|
assert.sameValue(obj?.\u{0062}, undefined);
|
||||||
|
|
||||||
|
assert.sameValue(arr ?. length, 2);
|
||||||
|
assert.sameValue(arr ?. l\u0065ngth, 2);
|
||||||
|
assert.sameValue(arr ?. l\u{0065}ngth, 2);
|
||||||
|
|
||||||
|
assert.sameValue(obj?.$, undefined);
|
||||||
|
|
||||||
|
obj.$ = 42;
|
||||||
|
assert.sameValue(obj?.$, 42);
|
||||||
|
|
||||||
|
assert.sameValue(obj?._, undefined);
|
||||||
|
|
||||||
|
obj._ = 39;
|
||||||
|
assert.sameValue(obj?._, 39);
|
Loading…
Reference in New Issue