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

Support ["prop"] syntax on class definitions #704

Merged
merged 2 commits into from
Oct 18, 2022
Merged
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
19 changes: 19 additions & 0 deletions Ast/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,25 @@ AstStat* Parser::parseDeclaration(const Location& start)
{
props.push_back(parseDeclaredClassMethod());
}
else if (lexer.current().type == '[')
{
const Lexeme begin = lexer.current();
nextLexeme(); // [

std::optional<AstArray<char>> chars = parseCharArray();

expectMatchAndConsume(']', begin);
expectAndConsume(':', "property type annotation");
AstType* type = parseTypeAnnotation();

// TODO: since AstName conains a char*, it can't contain null
bool containsNull = chars && (strnlen(chars->data, chars->size) < chars->size);

if (chars && !containsNull)
props.push_back(AstDeclaredClassProp{AstName(chars->data), type, false});
else
report(begin.location, "String literal contains malformed escape sequence");
}
else
{
Name propName = parseName("property name");
Expand Down
17 changes: 17 additions & 0 deletions tests/TypeInfer.definitions.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,21 @@ TEST_CASE_FIXTURE(Fixture, "class_definition_overload_metamethods")
CHECK_EQ(toString(requireType("shouldBeVector")), "Vector3");
}

TEST_CASE_FIXTURE(Fixture, "class_definition_string_props")
{
loadDefinition(R"(
declare class Foo
["a property"]: string
end
)");

CheckResult result = check(R"(
local x: Foo
local y = x["a property"]
)");

LUAU_REQUIRE_NO_ERRORS(result);
CHECK_EQ(toString(requireType("y")), "string");
}

TEST_SUITE_END();