@@ -9,7 +9,7 @@ import { PrintStatement, FunctionStatement, NamespaceStatement, ImportStatement
99import { Range } from 'vscode-languageserver' ;
1010import { DiagnosticMessages } from '../DiagnosticMessages' ;
1111import { isAliasStatement , isBlock , isCommentStatement , isFunctionStatement , isIfStatement , isIndexedGetExpression , isTypecastStatement } from '../astUtils/reflection' ;
12- import { expectDiagnostics , expectZeroDiagnostics } from '../testHelpers.spec' ;
12+ import { expectDiagnostics , expectDiagnosticsIncludes , expectZeroDiagnostics } from '../testHelpers.spec' ;
1313import { BrsTranspileState } from './BrsTranspileState' ;
1414import { SourceNode } from 'source-map' ;
1515import { BrsFile } from '../files/BrsFile' ;
@@ -1422,6 +1422,131 @@ describe('parser', () => {
14221422 expect ( ( ( ast . statements [ 0 ] as FunctionStatement ) . func . body . statements [ 0 ] as AssignmentStatement ) . name . text ) . to . eq ( 'alias' ) ;
14231423 } ) ;
14241424 } ) ;
1425+
1426+ describe ( 'inline interfaces' , ( ) => {
1427+ it ( 'inline interface param types disallowed in brightscript mode' , ( ) => {
1428+ let { diagnostics } = parse ( `
1429+ sub test(foo as {x as string})
1430+ print foo.x
1431+ end sub
1432+ ` , ParseMode . BrightScript ) ;
1433+ expectDiagnosticsIncludes ( diagnostics , [
1434+ DiagnosticMessages . functionParameterTypeIsInvalid ( 'foo' , '{' ) . message
1435+ ] ) ;
1436+ } ) ;
1437+
1438+ it ( 'inline interface return types disallowed in brightscript mode' , ( ) => {
1439+ let { diagnostics } = parse ( `
1440+ function test() as {x as string}
1441+ print {x: "hello"}
1442+ end function
1443+ ` , ParseMode . BrightScript ) ;
1444+ expectDiagnosticsIncludes ( diagnostics , [
1445+ DiagnosticMessages . invalidFunctionReturnType ( '{' ) . message
1446+ ] ) ;
1447+ } ) ;
1448+
1449+ it ( 'inline interface as param type' , ( ) => {
1450+ let { ast, diagnostics } = parse ( `
1451+ sub test(foo as {x as string})
1452+ print foo.x
1453+ end sub
1454+ ` , ParseMode . BrighterScript ) ;
1455+ expectZeroDiagnostics ( diagnostics ) ;
1456+ expect ( ast . statements . length ) . to . eq ( 1 ) ;
1457+ } ) ;
1458+
1459+ it ( 'inline interface as return type' , ( ) => {
1460+ let { ast, diagnostics } = parse ( `
1461+ function test() as {x as string}
1462+ print {x: "hello"}
1463+ end function
1464+ ` , ParseMode . BrighterScript ) ;
1465+ expectZeroDiagnostics ( diagnostics ) ;
1466+ expect ( ast . statements . length ) . to . eq ( 1 ) ;
1467+ } ) ;
1468+
1469+ it ( 'parses a big inline interface as param type' , ( ) => {
1470+ let { ast, diagnostics } = parse ( `
1471+ sub test(foo as {
1472+ x as string,
1473+ y as {a as integer}
1474+ z})
1475+ print foo.x + y.a.toStr()
1476+ end sub
1477+ ` , ParseMode . BrighterScript ) ;
1478+ expectZeroDiagnostics ( diagnostics ) ;
1479+ expect ( ast . statements . length ) . to . eq ( 1 ) ;
1480+ } ) ;
1481+
1482+ it ( 'allows optional members' , ( ) => {
1483+ let { ast, diagnostics } = parse ( `
1484+ sub test(p as {x as string, optional y})
1485+ end sub
1486+ ` , ParseMode . BrighterScript ) ;
1487+ expectZeroDiagnostics ( diagnostics ) ;
1488+ expect ( ast . statements . length ) . to . eq ( 1 ) ;
1489+ } ) ;
1490+
1491+ it ( 'is allowed as typecast' , ( ) => {
1492+ let { diagnostics } = parse ( `
1493+ sub test(p)
1494+ print (p as {name as string}).name
1495+ end sub
1496+ ` , ParseMode . BrighterScript ) ;
1497+ expectZeroDiagnostics ( diagnostics ) ;
1498+ } ) ;
1499+
1500+ it ( 'is allowed as class and interface field' , ( ) => {
1501+ let { diagnostics } = parse ( `
1502+ class Klass
1503+ x as {name as string}
1504+ end class
1505+ interface Iface
1506+ y as {age as integer}
1507+ end interface
1508+ ` , ParseMode . BrighterScript ) ;
1509+ expectZeroDiagnostics ( diagnostics ) ;
1510+ } ) ;
1511+
1512+ it ( 'can have custom type as member type' , ( ) => {
1513+ let { diagnostics } = parse ( `
1514+ interface IFace
1515+ name as string
1516+ end interface
1517+ function test(z as {foo as IFace})
1518+ return z.foo.name
1519+ end function
1520+ ` , ParseMode . BrighterScript ) ;
1521+ expectZeroDiagnostics ( diagnostics ) ;
1522+ } ) ;
1523+
1524+ it ( 'can have per-member doc comment' , ( ) => {
1525+ let { diagnostics } = parse ( `
1526+ interface IFace
1527+ inline as {
1528+ ' comment 1
1529+ name as string
1530+ ' comment 2
1531+ age as integer
1532+ }
1533+ end interface
1534+ function test(z as {foo as IFace})
1535+ return z.foo.inline.name
1536+ end function
1537+ ` , ParseMode . BrighterScript ) ;
1538+ expectZeroDiagnostics ( diagnostics ) ;
1539+ } ) ;
1540+
1541+ it ( 'can have string literals as members' , ( ) => {
1542+ let { diagnostics } = parse ( `
1543+ function test(z as {"this is a stringliteral" as string})
1544+ return z["this is a stringliteral"]
1545+ end function
1546+ ` , ParseMode . BrighterScript ) ;
1547+ expectZeroDiagnostics ( diagnostics ) ;
1548+ } ) ;
1549+ } ) ;
14251550} ) ;
14261551
14271552function parse ( text : string , mode ?: ParseMode ) {
0 commit comments