Skip to content

Commit

Permalink
Record table type alias property locations
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Sep 17, 2023
1 parent 3090010 commit 19294df
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Analysis/include/Luau/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,15 @@ struct Property
bool deprecated = false;
std::string deprecatedSuggestion;
std::optional<Location> location = std::nullopt;
std::optional<Location> typeLocation = std::nullopt;
Tags tags;
std::optional<std::string> documentationSymbol;

// DEPRECATED
// TODO: Kill all constructors in favor of `Property::rw(TypeId read, TypeId write)` and friends.
Property();
Property(TypeId readTy, bool deprecated = false, const std::string& deprecatedSuggestion = "", std::optional<Location> location = std::nullopt,
const Tags& tags = {}, const std::optional<std::string>& documentationSymbol = std::nullopt);
const Tags& tags = {}, const std::optional<std::string>& documentationSymbol = std::nullopt, std::optional<Location> typeLocation = std::nullopt);

// DEPRECATED: Should only be called in non-RWP! We assert that the `readTy` is not nullopt.
// TODO: Kill once we don't have non-RWP.
Expand Down
3 changes: 2 additions & 1 deletion Analysis/src/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,11 @@ FunctionType::FunctionType(TypeLevel level, Scope* scope, std::vector<TypeId> ge
Property::Property() {}

Property::Property(TypeId readTy, bool deprecated, const std::string& deprecatedSuggestion, std::optional<Location> location, const Tags& tags,
const std::optional<std::string>& documentationSymbol)
const std::optional<std::string>& documentationSymbol, std::optional<Location> typeLocation)
: deprecated(deprecated)
, deprecatedSuggestion(deprecatedSuggestion)
, location(location)
, typeLocation(typeLocation)
, tags(tags)
, documentationSymbol(documentationSymbol)
, readTy(readTy)
Expand Down
2 changes: 1 addition & 1 deletion Analysis/src/TypeInfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5401,7 +5401,7 @@ TypeId TypeChecker::resolveTypeWorker(const ScopePtr& scope, const AstType& anno
std::optional<TableIndexer> tableIndexer;

for (const auto& prop : table->props)
props[prop.name.value] = {resolveType(scope, *prop.type)};
props[prop.name.value] = {resolveType(scope, *prop.type), /* deprecated: */ false, {}, std::nullopt, {}, std::nullopt, prop.location};

if (const auto& indexer = table->indexer)
tableIndexer = TableIndexer(resolveType(scope, *indexer->indexType), resolveType(scope, *indexer->resultType));
Expand Down
23 changes: 23 additions & 0 deletions tests/TypeInfer.aliases.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,4 +1036,27 @@ TEST_CASE_FIXTURE(BuiltinsFixture, "alias_expands_to_bare_reference_to_imported_
LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(Fixture, "table_types_record_the_property_locations")
{
CheckResult result = check(R"(
type Table = {
create: () -> ()
}
local x: Table
)");

LUAU_REQUIRE_NO_ERRORS(result);
auto ty = requireTypeAlias("Table");

auto ttv = Luau::get<Luau::TableType>(ty);
REQUIRE(ttv);

auto propIt = ttv->props.find("create");
REQUIRE(propIt != ttv->props.end());

CHECK_EQ(propIt->second.location, std::nullopt);
CHECK_EQ(propIt->second.typeLocation, Location({2, 12}, {2, 18}));
}

TEST_SUITE_END();

0 comments on commit 19294df

Please sign in to comment.