Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/FileResolver.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/TypeCheckLimits.h"
5
6
#include <memory>
7
#include <optional>
8
#include <string>
9
#include <vector>
10
11
namespace Luau
12
{
13
14
class AstExpr;
15
16
using ModuleName = std::string;
17
18
struct SourceCode
19
{
20
enum Type
21
{
22
None,
23
Module,
24
Script,
25
};
26
27
std::string source;
28
Type type;
29
};
30
31
struct ModuleInfo
32
{
33
ModuleName name;
34
bool optional = false;
35
};
36
37
struct RequireAlias
38
{
39
explicit RequireAlias(std::string alias)
40
: alias(std::move(alias))
41
{
42
}
43
44
explicit RequireAlias(std::string alias, std::vector<std::string> tags)
45
: alias(std::move(alias))
46
, tags(std::move(tags))
47
{
48
}
49
50
std::string alias; // Unprefixed alias name (no leading `@`).
51
std::vector<std::string> tags = {};
52
};
53
54
struct RequireNode
55
{
56
virtual ~RequireNode() {}
57
58
// Get the path component representing this node.
59
virtual std::string getPathComponent() const = 0;
60
61
// Get the displayed user-facing label for this node, defaults to getPathComponent()
62
virtual std::string getLabel() const
63
{
64
return getPathComponent();
65
}
66
67
// Get tags to attach to this node's RequireSuggestion (defaults to none).
68
virtual std::vector<std::string> getTags() const
69
{
70
return {};
71
}
72
73
// Resolve a path relative to the current node. The Luau.Require library
74
// provides utilities that can help with implementing this logic.
75
virtual std::unique_ptr<RequireNode> resolvePathToNode(const std::string& path) const = 0;
76
77
// Get children of this node, if any (if this node represents a directory).
78
virtual std::vector<std::unique_ptr<RequireNode>> getChildren() const = 0;
79
80
// A list of the aliases available to this node.
81
virtual std::vector<RequireAlias> getAvailableAliases() const = 0;
82
};
83
84
struct RequireSuggestion
85
{
86
std::string label;
87
std::string fullPath;
88
std::vector<std::string> tags;
89
};
90
using RequireSuggestions = std::vector<RequireSuggestion>;
91
92
struct RequireSuggester
93
{
94
virtual ~RequireSuggester() {}
95
std::optional<RequireSuggestions> getRequireSuggestions(const ModuleName& requirer, const std::optional<std::string>& pathString) const;
96
97
protected:
98
virtual std::unique_ptr<RequireNode> getNode(const ModuleName& name) const = 0;
99
100
private:
101
std::optional<RequireSuggestions> getRequireSuggestionsImpl(const ModuleName& requirer, const std::optional<std::string>& path) const;
102
};
103
104
struct FileResolver
105
{
106
FileResolver() = default;
107
FileResolver(std::shared_ptr<RequireSuggester> requireSuggester)
108
: requireSuggester(std::move(requireSuggester))
109
{
110
}
111
112
virtual ~FileResolver() {}
113
114
virtual std::optional<SourceCode> readSource(const ModuleName& name) = 0;
115
116
virtual std::optional<ModuleInfo> resolveModule(const ModuleInfo* context, AstExpr* expr, const TypeCheckLimits& limits)
117
{
118
return std::nullopt;
119
}
120
121
virtual std::string getHumanReadableModuleName(const ModuleName& name) const
122
{
123
return name;
124
}
125
126
virtual std::optional<std::string> getEnvironmentForModule(const ModuleName& name) const
127
{
128
return std::nullopt;
129
}
130
131
std::optional<RequireSuggestions> getRequireSuggestions(const ModuleName& requirer, const std::optional<std::string>& pathString) const;
132
133
std::shared_ptr<RequireSuggester> requireSuggester;
134
};
135
136
struct NullFileResolver : FileResolver
137
{
138
std::optional<SourceCode> readSource(const ModuleName& name) override
139
{
140
return std::nullopt;
141
}
142
};
143
144
} // namespace Luau
145
146