-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtc.cpp
More file actions
273 lines (215 loc) · 8.22 KB
/
tc.cpp
File metadata and controls
273 lines (215 loc) · 8.22 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "tc.h"
#include "Luau/BuiltinDefinitions.h"
#include "Luau/Error.h"
#include "Luau/Transpiler.h"
#include "Luau/TypeAttach.h"
#include "Luau/Require.h"
LUAU_FASTFLAG(LuauSolverV2)
struct LuteFileResolver : Luau::FileResolver
{
std::optional<Luau::SourceCode> readSource(const Luau::ModuleName& name) override
{
Luau::SourceCode::Type sourceType;
std::optional<std::string> source = std::nullopt;
// If the module name is "-", then read source from stdin
if (name == "-")
{
source = readStdin();
sourceType = Luau::SourceCode::Script;
}
else
{
source = readFile(name);
sourceType = Luau::SourceCode::Module;
}
if (!source)
return std::nullopt;
return Luau::SourceCode{*source, sourceType};
}
std::optional<Luau::ModuleInfo> resolveModule(const Luau::ModuleInfo* context, Luau::AstExpr* node) override
{
if (Luau::AstExprConstantString* expr = node->as<Luau::AstExprConstantString>())
{
std::string path{expr->value.data, expr->value.size};
AnalysisRequireContext requireContext{context->name};
AnalysisCacheManager cacheManager;
AnalysisErrorHandler errorHandler;
RequireResolver resolver(path, requireContext, cacheManager, errorHandler);
RequireResolver::ResolvedRequire resolvedRequire = resolver.resolveRequire();
if (resolvedRequire.status == RequireResolver::ModuleStatus::FileRead)
return {{resolvedRequire.identifier}};
}
return std::nullopt;
}
std::string getHumanReadableModuleName(const Luau::ModuleName& name) const override
{
if (name == "-")
return "stdin";
return name;
}
private:
struct AnalysisRequireContext : RequireResolver::RequireContext
{
explicit AnalysisRequireContext(std::string path)
: path(std::move(path))
{
}
std::string getPath() override
{
return path;
}
bool isRequireAllowed() override
{
return true;
}
bool isStdin() override
{
return path == "-";
}
std::string createNewIdentifer(const std::string& path) override
{
return path;
}
private:
std::string path;
};
struct AnalysisCacheManager : public RequireResolver::CacheManager
{
AnalysisCacheManager() = default;
};
struct AnalysisErrorHandler : RequireResolver::ErrorHandler
{
AnalysisErrorHandler() = default;
};
};
struct LuteConfigResolver : Luau::ConfigResolver
{
Luau::Config defaultConfig;
mutable std::unordered_map<std::string, Luau::Config> configCache;
mutable std::vector<std::pair<std::string, std::string>> configErrors;
LuteConfigResolver(Luau::Mode mode)
{
defaultConfig.mode = mode;
}
const Luau::Config& getConfig(const Luau::ModuleName& name) const override
{
std::optional<std::string> path = getParentPath(name);
if (!path)
return defaultConfig;
return readConfigRec(*path);
}
const Luau::Config& 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::string configPath = joinPaths(path, Luau::kConfigName);
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.push_back({configPath, *error});
}
return configCache[path] = result;
}
};
static void report(const char* name, const Luau::Location& loc, const char* type, const char* message)
{
// fprintf(stderr, "%s(%d,%d): %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, type, message);
int columnEnd = (loc.begin.line == loc.end.line) ? loc.end.column : 100;
// Use stdout to match luacheck behavior
fprintf(stdout, "%s:%d:%d-%d: (W0) %s: %s\n", name, loc.begin.line + 1, loc.begin.column + 1, columnEnd, type, message);
}
static void reportError(const Luau::Frontend& frontend, const Luau::TypeError& error)
{
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(error.moduleName);
if (const Luau::SyntaxError* syntaxError = Luau::get_if<Luau::SyntaxError>(&error.data))
report(humanReadableName.c_str(), error.location, "SyntaxError", syntaxError->message.c_str());
else
report(
humanReadableName.c_str(),
error.location,
"TypeError",
Luau::toString(error, Luau::TypeErrorToStringOptions{frontend.fileResolver}).c_str()
);
}
static void reportWarning(const char* name, const Luau::LintWarning& warning)
{
report(name, warning.location, Luau::LintWarning::getName(warning.code), warning.text.c_str());
}
static bool reportModuleResult(Luau::Frontend& frontend, const Luau::ModuleName& name, bool annotate)
{
std::optional<Luau::CheckResult> cr = frontend.getCheckResult(name, false);
if (!cr)
{
fprintf(stderr, "Failed to find result for %s\n", name.c_str());
return false;
}
if (!frontend.getSourceModule(name))
{
fprintf(stderr, "Error opening %s\n", name.c_str());
return false;
}
for (auto& error : cr->errors)
reportError(frontend, error);
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(name);
for (auto& error : cr->lintResult.errors)
reportWarning( humanReadableName.c_str(), error);
for (auto& warning : cr->lintResult.warnings)
reportWarning( humanReadableName.c_str(), warning);
return cr->errors.empty() && cr->lintResult.errors.empty();
}
int typecheck(const std::vector<std::string> sourceFiles)
{
// Lute only supports the new type solver
FFlag::LuauSolverV2.value = true;
Luau::Mode mode = Luau::Mode::Strict;
bool annotate = true;
std::string basePath = "";
Luau::FrontendOptions frontendOptions;
frontendOptions.retainFullTypeGraphs = annotate;
frontendOptions.runLintChecks = true;
LuteFileResolver fileResolver;
LuteConfigResolver configResolver(mode);
Luau::Frontend frontend(&fileResolver, &configResolver, frontendOptions);
Luau::registerBuiltinGlobals(frontend, frontend.globals);
Luau::freeze(frontend.globals.globalTypes);
for (const std::string& path : sourceFiles)
frontend.queueModuleCheck(path);
std::vector<Luau::ModuleName> checkedModules;
try
{
checkedModules = frontend.checkQueuedModules(std::nullopt);
}
catch (const Luau::InternalCompilerError& ice)
{
Luau::Location location = ice.location ? *ice.location : Luau::Location();
std::string moduleName = ice.moduleName ? *ice.moduleName : "<unknown module>";
std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(moduleName);
Luau::TypeError error(location, moduleName, Luau::InternalError{ice.message});
report(
humanReadableName.c_str(),
location,
"InternalCompilerError",
Luau::toString(error, Luau::TypeErrorToStringOptions{frontend.fileResolver}).c_str()
);
return 1;
}
int failed = 0;
for (const Luau::ModuleName& name : checkedModules)
failed += !reportModuleResult(frontend, name, annotate);
if (!configResolver.configErrors.empty())
{
failed += int(configResolver.configErrors.size());
for (const auto& pair : configResolver.configErrors)
fprintf(stderr, "%s: %s\n", pair.first.c_str(), pair.second.c_str());
}
return failed ? 1 : 0;
}