Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional chaining #684

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export class Parser {
Lexeme.Newline,
Lexeme.Colon,
Lexeme.Eof,
Lexeme.Identifier,
...additionalterminators
);
}
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion test/brsTypes/components/RoDateTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/BrsComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("end to end brightscript functions", () => {
"Seconds: ",
"15",
"Last Day of Month: ",
"30",
"29",
"Milliseconds: ",
"160",
"ISO String UTC: ",
Expand Down
1 change: 1 addition & 0 deletions test/e2e/MultiFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]);
});
});
13 changes: 13 additions & 0 deletions test/e2e/resources/multi-file/test1.brs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sub Main()
print("function in same file: " + sameFileFunc())
print("function in different file: " + differentFileFunc())
print("function with dependency: " + dependentFunc())
print("test for optional chaining " + testOptionalChaining())
end sub

function sameFileFunc()
Expand All @@ -11,3 +12,15 @@ end function
function dependencyFunc()
return "from dependencyFunc()"
end function

function testOptionalChaining()
responseData = {
data:{
metricsData:{
addOnsStepStart : "value"
}
}
}
result = responseData?.data.metricsData?.addOnsStepStart
return result
end function
32 changes: 31 additions & 1 deletion test/parser/expression/Indexing.test.js
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -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("_"),
Expand Down