@@ -1785,6 +1785,108 @@ describe('parser', () => {
17851785 expectZeroDiagnostics ( diagnostics ) ;
17861786 } ) ;
17871787 } ) ;
1788+
1789+ describe ( 'typed functions as types' , ( ) => {
1790+ it ( 'disallowed in brightscript mode' , ( ) => {
1791+ let { diagnostics } = parse ( `
1792+ function test(func as function())
1793+ return func()
1794+ end function
1795+ ` , ParseMode . BrightScript ) ;
1796+ expectDiagnosticsIncludes ( diagnostics , [
1797+ DiagnosticMessages . unexpectedToken ( ')' )
1798+ ] ) ;
1799+ } ) ;
1800+
1801+ it ( 'can be passed as param types' , ( ) => {
1802+ let { diagnostics } = parse ( `
1803+ function test(func as function())
1804+ return func()
1805+ end function
1806+ ` , ParseMode . BrighterScript ) ;
1807+ expectZeroDiagnostics ( diagnostics ) ;
1808+ } ) ;
1809+
1810+ it ( 'can have a return type' , ( ) => {
1811+ let { diagnostics } = parse ( `
1812+ function test(func as sub() as integer) as integer
1813+ return func()
1814+ end function
1815+ ` , ParseMode . BrighterScript ) ;
1816+ expectZeroDiagnostics ( diagnostics ) ;
1817+ } ) ;
1818+
1819+ it ( 'can use sub or function' , ( ) => {
1820+ let { diagnostics } = parse ( `
1821+ function test(func as sub() as integer) as integer
1822+ return func()
1823+ end function
1824+
1825+ function test2(func as function() as integer) as integer
1826+ return func()
1827+ end function
1828+ ` , ParseMode . BrighterScript ) ;
1829+ expectZeroDiagnostics ( diagnostics ) ;
1830+ } ) ;
1831+
1832+ it ( 'can have primitive parameters' , ( ) => {
1833+ let { diagnostics } = parse ( `
1834+ function test(func as function(name as string, num as integer) as integer) as integer
1835+ return func("hello", 123)
1836+ end function
1837+ ` , ParseMode . BrighterScript ) ;
1838+ expectZeroDiagnostics ( diagnostics ) ;
1839+ } ) ;
1840+
1841+ it ( 'can have complex parameters' , ( ) => {
1842+ let { diagnostics } = parse ( `
1843+ interface IFace
1844+ name as string
1845+ end interface
1846+
1847+ function test(func as function(thing as IFace) as integer) as integer
1848+ return func({name: "hello"})
1849+ end function
1850+ ` , ParseMode . BrighterScript ) ;
1851+ expectZeroDiagnostics ( diagnostics ) ;
1852+ } ) ;
1853+
1854+ it ( 'can have compound parameters' , ( ) => {
1855+ let { diagnostics } = parse ( `
1856+ interface IFace
1857+ name as string
1858+ end interface
1859+
1860+ function test(func as function(arg1 as string or integer, arg2 as IFace) as integer) as integer
1861+ return func("hello", {name: "hello"})
1862+ end function
1863+ ` , ParseMode . BrighterScript ) ;
1864+ expectZeroDiagnostics ( diagnostics ) ;
1865+ } ) ;
1866+
1867+ it ( 'can be used as return types' , ( ) => {
1868+ let { diagnostics } = parse ( `
1869+ function test() as function() as integer
1870+ return function() as integer
1871+ return 123
1872+ end function
1873+ end function
1874+ ` , ParseMode . BrighterScript ) ;
1875+ expectZeroDiagnostics ( diagnostics ) ;
1876+ } ) ;
1877+
1878+ it ( 'can have a union as return type' , ( ) => {
1879+ let { diagnostics } = parse ( `
1880+ type foo = function() as integer or string
1881+ function test() as foo
1882+ return function() as integer
1883+ return 123
1884+ end function
1885+ end function
1886+ ` , ParseMode . BrighterScript ) ;
1887+ expectZeroDiagnostics ( diagnostics ) ;
1888+ } ) ;
1889+ } ) ;
17881890} ) ;
17891891
17901892function parse ( text : string , mode ?: ParseMode ) {
0 commit comments