Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Require/src/AliasCycleTracker.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "AliasCycleTracker.h"
3
4
#include "Luau/DenseHash.h"
5
#include "Luau/StringUtils.h"
6
7
#include <optional>
8
#include <string>
9
#include <vector>
10
11
namespace Luau::Require
12
{
13
14
std::optional<std::string> AliasCycleTracker::add(std::string alias)
15
{
16
if (seen.contains(alias))
17
return Luau::format("detected alias cycle (%s)", getStringifiedCycle(alias).c_str());
18
19
seen.insert(alias);
20
ordered.push_back(std::move(alias));
21
return std::nullopt;
22
}
23
24
std::string AliasCycleTracker::getStringifiedCycle(const std::string& repeated) const
25
{
26
std::string result;
27
bool inCycle = false;
28
for (const std::string& item : ordered)
29
{
30
if (inCycle)
31
{
32
result += " -> ";
33
result += "@" + item;
34
}
35
if (item == repeated)
36
{
37
inCycle = true;
38
result += "@" + item;
39
}
40
}
41
result += " -> " + ("@" + repeated);
42
return result;
43
}
44
45
} // namespace Luau::Require
46
47