short-circuit-number-empty-string.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (C) 2019 Leo Balter. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: >
  5. Short circuit if the CoalesceExpressionHead is not undefined or null (the empty string)
  6. esid: sec-conditional-operator
  7. info: |
  8. ConditionalExpression :
  9. ShortCircuitExpression
  10. ShortCircuitExpression ? AssignmentExpression : AssignmentExpression
  11. ShortCircuitExpression :
  12. LogicalORExpression
  13. CoalesceExpression
  14. CoalesceExpression :
  15. CoalesceExpressionHead ?? BitwiseORExpression
  16. CoalesceExpressionHead :
  17. CoalesceExpression
  18. BitwiseORExpression
  19. Runtime Semantics: Evaluation
  20. CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression
  21. 1. Let lref be the result of evaluating CoalesceExpressionHead.
  22. 2. Let lval be ? GetValue(lref).
  23. 3. If lval is undefined or null,
  24. a. Let rref be the result of evaluating BitwiseORExpression.
  25. b. Return ? GetValue(rref).
  26. 4. Otherwise, return lval.
  27. features: [coalesce-expression]
  28. ---*/
  29. var x;
  30. var str = '';
  31. x = undefined;
  32. x = str ?? 1;
  33. assert.sameValue(x, str, 'str ?? 1');
  34. x = undefined;
  35. x = str ?? null;
  36. assert.sameValue(x, str, 'str ?? null');
  37. x = undefined;
  38. x = str ?? undefined;
  39. assert.sameValue(x, str, 'str ?? undefined');
  40. x = undefined;
  41. x = str ?? null ?? undefined;
  42. assert.sameValue(x, str, 'str ?? null ?? undefined');
  43. x = undefined;
  44. x = str ?? undefined ?? null;
  45. assert.sameValue(x, str, 'str ?? undefined ?? null');
  46. x = undefined;
  47. x = str ?? null ?? null;
  48. assert.sameValue(x, str, 'str ?? null ?? null');
  49. x = undefined;
  50. x = str ?? undefined ?? undefined;
  51. assert.sameValue(x, str, 'str ?? null ?? null');
  52. x = undefined;
  53. x = null ?? str ?? null;
  54. assert.sameValue(x, str, 'null ?? str ?? null');
  55. x = undefined;
  56. x = null ?? str ?? undefined;
  57. assert.sameValue(x, str, 'null ?? str ?? undefined');
  58. x = undefined;
  59. x = undefined ?? str ?? null;
  60. assert.sameValue(x, str, 'undefined ?? str ?? null');
  61. x = undefined;
  62. x = undefined ?? str ?? undefined;
  63. assert.sameValue(x, str, 'undefined ?? str ?? undefined');