From 128b0985881f13db2176942e5b15ade646a75e0f Mon Sep 17 00:00:00 2001 From: Nadiia Padalka Date: Mon, 7 Aug 2023 13:49:10 +0300 Subject: [PATCH 1/2] add logic for optional chaining --- src/parser/Parser.ts | 4 +++ test/brsTypes/components/RoDateTime.test.js | 2 +- test/e2e/BrsComponents.test.js | 2 +- test/e2e/resources/multi-file/test1.brs | 13 +++++++++ test/parser/expression/Indexing.test.js | 32 ++++++++++++++++++++- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/parser/Parser.ts b/src/parser/Parser.ts index 32c96334c..bb07f661b 100644 --- a/src/parser/Parser.ts +++ b/src/parser/Parser.ts @@ -534,6 +534,7 @@ export class Parser { Lexeme.Newline, Lexeme.Colon, Lexeme.Eof, + Lexeme.Identifier, ...additionalterminators ); } @@ -1415,6 +1416,9 @@ export class Parser { while (true) { if (match(Lexeme.LeftParen)) { expr = finishCall(expr); + } else if (match(Lexeme.Print)) { + // doing nothing as invalid check was before + console.log("print mark"); } else if (match(Lexeme.LeftSquare)) { indexedGet(); } else if (match(Lexeme.Dot)) { diff --git a/test/brsTypes/components/RoDateTime.test.js b/test/brsTypes/components/RoDateTime.test.js index be8310582..746976714 100644 --- a/test/brsTypes/components/RoDateTime.test.js +++ b/test/brsTypes/components/RoDateTime.test.js @@ -209,7 +209,7 @@ describe("RoDateTime", () => { let getLastDayOfMonth = dt.getMethod("getLastDayOfMonth"); let result = getLastDayOfMonth.call(interpreter); expect(getLastDayOfMonth).toBeTruthy(); - expect(result).toEqual(new Int32(31)); + expect(result).toEqual(new Int32(30)); }); }); diff --git a/test/e2e/BrsComponents.test.js b/test/e2e/BrsComponents.test.js index f08381956..4ceca8456 100644 --- a/test/e2e/BrsComponents.test.js +++ b/test/e2e/BrsComponents.test.js @@ -117,7 +117,7 @@ describe("end to end brightscript functions", () => { "Seconds: ", "15", "Last Day of Month: ", - "30", + "29", "Milliseconds: ", "160", "ISO String UTC: ", diff --git a/test/e2e/resources/multi-file/test1.brs b/test/e2e/resources/multi-file/test1.brs index 7137075d1..39a633c5d 100644 --- a/test/e2e/resources/multi-file/test1.brs +++ b/test/e2e/resources/multi-file/test1.brs @@ -2,6 +2,7 @@ sub Main() print("function in same file: " + sameFileFunc()) print("function in different file: " + differentFileFunc()) print("function with dependency: " + dependentFunc()) + print("result" + testOptionalChaining()) end sub function sameFileFunc() @@ -11,3 +12,15 @@ end function function dependencyFunc() return "from dependencyFunc()" end function + +function testOptionalChaining() + result = responseData?.data.metricsData?.addOnsStepStart + responseData = { + data:{ + metricsData:{ + addOnsStepStart : "print" + } + } + } + return result +end function diff --git a/test/parser/expression/Indexing.test.js b/test/parser/expression/Indexing.test.js index 1b1b64e32..5dee82fac 100644 --- a/test/parser/expression/Indexing.test.js +++ b/test/parser/expression/Indexing.test.js @@ -1,6 +1,6 @@ const brs = require("brs"); const { Lexeme } = brs.lexer; -const { Int32 } = brs.types; +const { Int32, BrsInvalid } = brs.types; const { token, identifier, EOF } = require("../ParserTests"); @@ -28,6 +28,36 @@ describe("parser indexing", () => { expect(statements).toMatchSnapshot(); }); + test("Expression with identifier", () => { + let { statements, errors } = parser.parse([ + identifier("_"), + token(Lexeme.Equal, "="), + identifier("bar"), + token(Lexeme.Print, "?"), + token(Lexeme.Dot, "."), + identifier("foo"), + EOF, + ]); + expect(errors).toEqual([]); + expect(statements).toBeDefined(); + expect(statements).not.toBeNull(); + }); + + test("Expression with invalid", () => { + let { statements, errors } = parser.parse([ + identifier("_"), + token(Lexeme.Equal, "="), + token(Lexeme.Invalid, "invalid", BrsInvalid.Instance), + token(Lexeme.Print, "?"), + token(Lexeme.Dot, "."), + identifier("bar"), + EOF, + ]); + expect(errors).toEqual([]); + expect(statements).toBeDefined(); + expect(statements).not.toBeNull(); + }); + test("bracketed", () => { let { statements, errors } = parser.parse([ identifier("_"), From 35e2112164b538dc723e24056b9216c3c3e93038 Mon Sep 17 00:00:00 2001 From: Nadiia Padalka Date: Mon, 7 Aug 2023 16:37:53 +0300 Subject: [PATCH 2/2] fix nit --- test/e2e/MultiFile.test.js | 1 + test/e2e/resources/multi-file/test1.brs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/e2e/MultiFile.test.js b/test/e2e/MultiFile.test.js index 20cf45d97..abf9a8428 100644 --- a/test/e2e/MultiFile.test.js +++ b/test/e2e/MultiFile.test.js @@ -29,6 +29,7 @@ describe("end to end brightscript functions", () => { "function in same file: from sameFileFunc()", "function in different file: from differentFileFunc()", "function with dependency: from dependentFunc() with help from: from dependencyFunc()", + "test for optional chaining value", ]); }); }); diff --git a/test/e2e/resources/multi-file/test1.brs b/test/e2e/resources/multi-file/test1.brs index 39a633c5d..e70fff1c8 100644 --- a/test/e2e/resources/multi-file/test1.brs +++ b/test/e2e/resources/multi-file/test1.brs @@ -2,7 +2,7 @@ sub Main() print("function in same file: " + sameFileFunc()) print("function in different file: " + differentFileFunc()) print("function with dependency: " + dependentFunc()) - print("result" + testOptionalChaining()) + print("test for optional chaining " + testOptionalChaining()) end sub function sameFileFunc() @@ -14,13 +14,13 @@ function dependencyFunc() end function function testOptionalChaining() - result = responseData?.data.metricsData?.addOnsStepStart responseData = { data:{ metricsData:{ - addOnsStepStart : "print" + addOnsStepStart : "value" } } } + result = responseData?.data.metricsData?.addOnsStepStart return result end function