@@ -37,23 +37,53 @@ it("should resolve unknown member calls with optional members without inlining t
3737 expect ( c . toString ( ) ) . toBe ( "function () { return undefined(); }" ) ;
3838 expect ( ( ) => OBJECT . SUB1 ?. UNKNOWN ( ) ) . toThrow ( ) ;
3939} ) ;
40- it ( "should short-circuit a call on a member read from an unknown member (issue 21822)" , function ( ) {
41- const a = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. includes ( "foo" ) ; } ;
42- const b = function ( ) { return OBJECT ?. SUB1 ?. UNKNOWN ?. toUpperCase ( ) ; } ;
43- const c = function ( ) { return NOT_DEFINED . SUB2 . b ?. [ "includes" ] ( "foo" ) ; } ;
44- const d = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. a . b ( ) ; } ;
45- const e = function ( ) { return OBJECT . SUB1 . UNKNOWN . a ?. b ( ) ; } ;
40+ it ( "should still substitute arguments of an unknown member call (issue 15559)" , function ( ) {
41+ const a = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. ( STRING ) ; } ;
42+ expect ( a . toString ( ) ) . toBe ( 'function () { return undefined?.("string"); }' ) ;
43+ expect ( OBJECT . SUB1 . UNKNOWN ?. ( STRING ) ) . toBe ( undefined ) ;
44+ } ) ;
45+ it ( "should short-circuit a call reached through an unknown member (issue 21822)" , function ( ) {
46+ const a = function ( ) { return process . env . MISSING ?. includes ( "foo" ) ; } ;
47+ const b = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. includes ( "foo" ) ; } ;
48+ const c = function ( ) { return OBJECT ?. SUB1 ?. UNKNOWN ?. toUpperCase ( ) ; } ;
49+ const d = function ( ) { return NOT_DEFINED . SUB2 . b ?. [ "includes" ] ( "foo" ) ; } ;
50+ const e = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. a . b ( ) ; } ;
51+ const f = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. a ?. b ( ) ; } ;
4652 expect ( a . toString ( ) ) . toBe ( "function () { return undefined; }" ) ;
4753 expect ( b . toString ( ) ) . toBe ( "function () { return undefined; }" ) ;
4854 expect ( c . toString ( ) ) . toBe ( "function () { return undefined; }" ) ;
4955 expect ( d . toString ( ) ) . toBe ( "function () { return undefined; }" ) ;
50- expect ( e . toString ( ) ) . toBe ( "function () { return undefined(); }" ) ;
56+ expect ( e . toString ( ) ) . toBe ( "function () { return undefined; }" ) ;
57+ expect ( f . toString ( ) ) . toBe ( "function () { return undefined; }" ) ;
58+ expect ( process . env . MISSING ?. includes ( "foo" ) ) . toBe ( undefined ) ;
5159 expect ( OBJECT . SUB1 . UNKNOWN ?. includes ( "foo" ) ) . toBe ( undefined ) ;
60+ expect ( OBJECT ?. SUB1 ?. UNKNOWN ?. toUpperCase ( ) ) . toBe ( undefined ) ;
5261 expect ( NOT_DEFINED . SUB2 . b ?. [ "includes" ] ( "foo" ) ) . toBe ( undefined ) ;
62+ expect ( OBJECT . SUB1 . UNKNOWN ?. a . b ( ) ) . toBe ( undefined ) ;
63+ } ) ;
64+ it ( "should not evaluate the arguments of a short-circuited call (issue 21822)" , function ( ) {
65+ let calls = 0 ;
66+ const arg = function ( ) { calls ++ ; return STRING ; } ;
67+ expect ( OBJECT . SUB1 . UNKNOWN ?. includes ( arg ( ) ) ) . toBe ( undefined ) ;
68+ expect ( calls ) . toBe ( 0 ) ;
69+ } ) ;
70+ it ( "should keep a non-optional read after an unknown member throwing (issue 21822)" , function ( ) {
71+ const a = function ( ) { return OBJECT . SUB1 . UNKNOWN . a ?. ( ) ; } ;
72+ const b = function ( ) { return OBJECT . SUB1 . UNKNOWN . a ?. b ( ) ; } ;
73+ const c = function ( ) { return OBJECT . SUB1 . UNKNOWN [ "a" ] ?. ( ) ; } ;
74+ const d = function ( ) { return OBJECT . SUB1 . UNKNOWN . deep . method ( ) ; } ;
75+ expect ( a . toString ( ) ) . toBe ( "function () { return undefined.a?.(); }" ) ;
76+ expect ( b . toString ( ) ) . toBe ( "function () { return undefined.a?.b(); }" ) ;
77+ expect ( c . toString ( ) ) . toBe ( 'function () { return undefined["a"]?.(); }' ) ;
78+ expect ( d . toString ( ) ) . toBe ( "function () { return undefined.deep.method(); }" ) ;
79+ expect ( ( ) => OBJECT . SUB1 . UNKNOWN . a ?. ( ) ) . toThrow ( ) ;
5380 expect ( ( ) => OBJECT . SUB1 . UNKNOWN . a ?. b ( ) ) . toThrow ( ) ;
81+ expect ( ( ) => OBJECT . SUB1 . UNKNOWN [ "a" ] ?. ( ) ) . toThrow ( ) ;
82+ expect ( ( ) => OBJECT . SUB1 . UNKNOWN . deep . method ( ) ) . toThrow ( ) ;
5483} ) ;
55- it ( "should still substitute arguments of an unknown member call (issue 15559)" , function ( ) {
56- const a = function ( ) { return OBJECT . SUB1 . UNKNOWN ?. ( STRING ) ; } ;
57- expect ( a . toString ( ) ) . toBe ( 'function () { return undefined?.("string"); }' ) ;
58- expect ( OBJECT . SUB1 . UNKNOWN ?. ( STRING ) ) . toBe ( undefined ) ;
84+ it ( "should keep optional calls on defined members intact (issue 21822)" , function ( ) {
85+ expect ( OBJECT . SUB1 . a ?. toFixed ( 2 ) ) . toBe ( "1.00" ) ;
86+ expect ( STRING ?. toUpperCase ( ) ) . toBe ( "STRING" ) ;
87+ expect ( process . env . NODE_ENV ?. toUpperCase ( ) ) . toBe ( "PRODUCTION" ) ;
88+ expect ( OBJECT . SUB1 ?. a ) . toBe ( 1 ) ;
5989} ) ;
0 commit comments