11import { notAllowedOptionalAssignment } from "../../src/transformation/utils/diagnostics" ;
22import * as util from "../util" ;
3- import { ScriptTarget } from "typescript" ;
43
54test . each ( [ "null" , "undefined" , '{ foo: "foo" }' ] ) ( "optional chaining (%p)" , value => {
65 util . testFunction `
7- const obj: { foo: string} | null | undefined = ${ value } ;
6+ const obj = ${ value } as { foo: string} | null | undefined;
87 return obj?.foo;
98 `
109 . expectToMatchJsResult ( )
@@ -24,7 +23,7 @@ test("long optional chain", () => {
2423
2524test . each ( [ "undefined" , "{}" , "{ foo: {} }" , "{ foo: {bar: 'baz'}}" ] ) ( "nested optional chaining (%p)" , value => {
2625 util . testFunction `
27- const obj: { foo?: { bar?: string } } | undefined = ${ value } ;
26+ const obj = ${ value } as { foo?: { bar?: string } } | undefined;
2827 return obj?.foo?.bar;
2928 ` . expectToMatchJsResult ( ) ;
3029} ) ;
@@ -33,7 +32,7 @@ test.each(["undefined", "{}", "{ foo: {} }", "{ foo: {bar: 'baz'}}"])(
3332 "nested optional chaining combined with coalescing (%p)" ,
3433 value => {
3534 util . testFunction `
36- const obj: { foo?: { bar?: string } } | undefined = ${ value } ;
35+ const obj = ${ value } as { foo?: { bar?: string } } | undefined;
3736 return obj?.foo?.bar ?? "not found";
3837 ` . expectToMatchJsResult ( ) ;
3938 }
@@ -108,7 +107,7 @@ test("unused call", () => {
108107 // should use if statement, as result is not used
109108} ) ;
110109
111- test . each ( [ "undefined" , "{ foo: v =>v }" ] ) ( "with preceding statements on right side" , value => {
110+ test . each ( [ "undefined" , "{ foo: (v: any) =>v }" ] ) ( "with preceding statements on right side" , value => {
112111 util . testFunction `
113112 let i = 0
114113 const obj: any = ${ value } ;
@@ -120,12 +119,12 @@ test.each(["undefined", "{ foo: v=>v }"])("with preceding statements on right si
120119} ) ;
121120
122121// unused, with preceding statements on right side
123- test . each ( [ "undefined" , "{ foo(val) {return val} }" ] ) (
122+ test . each ( [ "undefined" , "{ foo(val: any ) {return val} }" ] ) (
124123 "unused result with preceding statements on right side" ,
125124 value => {
126125 util . testFunction `
127126 let i = 0
128- const obj = ${ value } ;
127+ const obj = ${ value } as any ;
129128 obj?.foo(i++);
130129 return i
131130 `
@@ -135,8 +134,10 @@ test.each(["undefined", "{ foo(val) {return val} }"])(
135134 }
136135) ;
137136
138- test . each ( [ "undefined" , "{ foo(v) { return v} }" ] ) ( "with preceding statements on right side modifying left" , value => {
139- util . testFunction `
137+ test . each ( [ "undefined" , "{ foo(v: any) { return v} }" ] ) (
138+ "with preceding statements on right side modifying left" ,
139+ value => {
140+ util . testFunction `
140141 let i = 0
141142 let obj: any = ${ value } ;
142143 function bar() {
@@ -147,10 +148,11 @@ test.each(["undefined", "{ foo(v) { return v} }"])("with preceding statements on
147148
148149 return {result: obj?.foo(bar(), i++), obj, i}
149150 `
150- . expectToMatchJsResult ( )
151- . expectLuaToMatchSnapshot ( ) ;
152- // should use if statement, as there are preceding statements
153- } ) ;
151+ . expectToMatchJsResult ( )
152+ . expectLuaToMatchSnapshot ( ) ;
153+ // should use if statement, as there are preceding statements
154+ }
155+ ) ;
154156
155157test ( "does not suppress error if left side is false" , ( ) => {
156158 const result = util . testFunction `
@@ -233,7 +235,7 @@ test("does not crash when incorrectly used in assignment (#1044)", () => {
233235describe ( "optional chaining function calls" , ( ) => {
234236 test . each ( [ "() => 4" , "undefined" ] ) ( "stand-alone optional function (%p)" , value => {
235237 util . testFunction `
236- const f: (( ) => number) | undefined = ${ value } ;
238+ const f = ( ${ value } ) as (( ) => number) | undefined;
237239 return f?.();
238240 ` . expectToMatchJsResult ( ) ;
239241 } ) ;
@@ -255,7 +257,7 @@ describe("optional chaining function calls", () => {
255257
256258 test ( "object with method can be undefined" , ( ) => {
257259 util . testFunction `
258- const objWithMethods: { foo: () => number, bar: (this: void) => number } | undefined = undefined;
260+ const objWithMethods = undefined as { foo: () => number, bar: (this: void) => number } | undefined;
259261 return [objWithMethods?.foo() ?? "no foo", objWithMethods?.bar() ?? "no bar"];
260262 ` . expectToMatchJsResult ( ) ;
261263 } ) ;
@@ -326,8 +328,8 @@ describe("optional chaining function calls", () => {
326328
327329 test . each ( [ undefined , "[1, 2, 3, 4]" ] ) ( "Array: %p" , expr => {
328330 util . testFunction `
329- const value: any[] | undefined = ${ expr }
330- return value?.map(x=>x+1)
331+ const value = ${ expr } as any[] | undefined;
332+ return value?.map(x=>x+1);
331333 ` . expectToMatchJsResult ( ) ;
332334 } ) ;
333335 } ) ;
@@ -342,7 +344,6 @@ describe("optional chaining function calls", () => {
342344 `
343345 . setOptions ( {
344346 strict,
345- target : ScriptTarget . ES5 ,
346347 } )
347348 . expectToMatchJsResult ( ) ;
348349 } ) ;
@@ -397,7 +398,7 @@ describe("Unsupported optional chains", () => {
397398describe ( "optional delete" , ( ) => {
398399 test ( "successful" , ( ) => {
399400 util . testFunction `
400- const table = {
401+ const table: { bar?: number } = {
401402 bar: 3
402403 }
403404 return [delete table?.bar, table]
@@ -415,9 +416,9 @@ describe("optional delete", () => {
415416
416417 test ( "delete on undefined" , ( ) => {
417418 util . testFunction `
418- const table : {
419- bar: number
420- } | undefined = undefined
419+ const table = undefined as {
420+ bar? : number
421+ } | undefined;
421422 return [delete table?.bar, table ?? "nil"]
422423 ` . expectToMatchJsResult ( ) ;
423424 } ) ;
0 commit comments