From 7569e863b3fe60e44ac9e958e54c3fc197cce3cc Mon Sep 17 00:00:00 2001 From: Nadiia Padalka <35704032+nadiapadalka@users.noreply.github.com> Date: Wed, 25 Oct 2023 14:09:54 +0300 Subject: [PATCH] add logic for optional chaining (#21) * add logic for optional chaining * remove console log * format doc * fix test * fix uts --- src/parser/Parser.ts | 3 +++ test/e2e/resources/multi-file/test1.brs | 13 ++++++++++ test/parser/expression/Indexing.test.js | 32 ++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/parser/Parser.ts b/src/parser/Parser.ts index 32c96334c..677f60784 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,8 @@ export class Parser { while (true) { if (match(Lexeme.LeftParen)) { expr = finishCall(expr); + } else if (match(Lexeme.Print)) { + // doing nothing as invalid check was before } else if (match(Lexeme.LeftSquare)) { indexedGet(); } else if (match(Lexeme.Dot)) { 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 4e3c5ac60..73037c596 100644 --- a/test/parser/expression/Indexing.test.js +++ b/test/parser/expression/Indexing.test.js @@ -1,6 +1,6 @@ const brs = require("../../../lib"); 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("_"),