-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathconfigresolver.cpp
More file actions
79 lines (64 loc) · 2.42 KB
/
configresolver.cpp
File metadata and controls
79 lines (64 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "lute/configresolver.h"
#include "Luau/ParseResult.h"
#include "Luau/FileUtils.h"
#include "Luau/LuauConfig.h"
namespace Luau
{
LuteConfigResolver::LuteConfigResolver(Luau::Mode mode)
{
defaultConfig.mode = mode;
}
const Luau::Config& LuteConfigResolver::getConfig(const Luau::ModuleName& name) const
{
std::optional<std::string> path = getParentPath(name);
if (!path)
return defaultConfig;
return readConfigRec(*path);
}
const Luau::Config& LuteConfigResolver::readConfigRec(const std::string& path) const
{
auto it = configCache.find(path);
if (it != configCache.end())
return it->second;
std::optional<std::string> parent = getParentPath(path);
Luau::Config result = parent ? readConfigRec(*parent) : defaultConfig;
std::optional<std::string> configPath = joinPaths(path, Luau::kConfigName);
if (!isFile(*configPath))
configPath = std::nullopt;
std::optional<std::string> luauConfigPath = joinPaths(path, Luau::kLuauConfigName);
if (!isFile(*luauConfigPath))
luauConfigPath = std::nullopt;
if (configPath && luauConfigPath)
{
std::string ambiguousError = Luau::format("Both %s and %s files exist", Luau::kConfigName, Luau::kLuauConfigName);
configErrors.emplace_back(*configPath, std::move(ambiguousError));
}
else if (configPath)
{
if (std::optional<std::string> contents = readFile(*configPath))
{
Luau::ConfigOptions::AliasOptions aliasOpts;
aliasOpts.configLocation = *configPath;
aliasOpts.overwriteAliases = true;
Luau::ConfigOptions opts;
opts.aliasOptions = std::move(aliasOpts);
std::optional<std::string> error = Luau::parseConfig(*contents, result, opts);
if (error)
configErrors.emplace_back(*configPath, *error);
}
}
else if (luauConfigPath)
{
if (std::optional<std::string> contents = readFile(*luauConfigPath))
{
Luau::ConfigOptions::AliasOptions aliasOpts;
aliasOpts.configLocation = *luauConfigPath;
aliasOpts.overwriteAliases = true;
std::optional<std::string> error = Luau::extractLuauConfig(*contents, result, aliasOpts, Luau::InterruptCallbacks{});
if (error)
configErrors.emplace_back(*luauConfigPath, *error);
}
}
return configCache[path] = result;
}
} // namespace Luau