10
10
#include < optional>
11
11
#include < utility>
12
12
13
- static constexpr char kRequireErrorAmbiguous [] = " require path could not be resolved to a unique file" ;
14
- static constexpr char kRequireErrorGeneric [] = " error requiring module" ;
15
-
16
13
namespace Luau ::Require
17
14
{
18
15
19
16
using Error = std::optional<std::string>;
20
17
21
- static Error toError (NavigationContext::NavigateResult result)
22
- {
23
- if (result == NavigationContext::NavigateResult::Success)
24
- return std::nullopt;
25
- if (result == NavigationContext::NavigateResult::Ambiguous)
26
- return kRequireErrorAmbiguous ;
27
- else
28
- return kRequireErrorGeneric ;
29
- }
30
-
31
18
static std::string extractAlias (std::string_view path)
32
19
{
33
20
// To ignore the '@' alias prefix when processing the alias
@@ -53,7 +40,7 @@ Navigator::Status Navigator::navigate(std::string path)
53
40
{
54
41
std::replace (path.begin (), path.end (), ' \\ ' , ' /' );
55
42
56
- if (Error error = toError (navigationContext. reset (navigationContext. getRequirerIdentifier ()) ))
43
+ if (Error error = resetToRequirer ( ))
57
44
{
58
45
errorHandler.reportError (*error);
59
46
return Status::ErrorReported;
@@ -98,7 +85,7 @@ Error Navigator::navigateImpl(std::string_view path)
98
85
99
86
// If the alias is "@self", we reset to the requirer's context and
100
87
// navigate directly from there.
101
- if (Error error = toError (navigationContext. reset (navigationContext. getRequirerIdentifier ()) ))
88
+ if (Error error = resetToRequirer ( ))
102
89
return error;
103
90
if (Error error = navigateThroughPath (path))
104
91
return error;
@@ -114,7 +101,7 @@ Error Navigator::navigateImpl(std::string_view path)
114
101
115
102
if (pathType == PathType::RelativeToCurrent || pathType == PathType::RelativeToParent)
116
103
{
117
- if (Error error = toError (navigationContext. toParent () ))
104
+ if (Error error = navigateToParent (std::nullopt ))
118
105
return error;
119
106
if (Error error = navigateThroughPath (path))
120
107
return error;
@@ -133,6 +120,7 @@ Error Navigator::navigateThroughPath(std::string_view path)
133
120
components = splitPath (components.second );
134
121
}
135
122
123
+ std::optional<std::string> previousComponent;
136
124
while (!(components.first .empty () && components.second .empty ()))
137
125
{
138
126
if (components.first == " ." || components.first .empty ())
@@ -142,14 +130,15 @@ Error Navigator::navigateThroughPath(std::string_view path)
142
130
}
143
131
else if (components.first == " .." )
144
132
{
145
- if (Error error = toError (navigationContext. toParent () ))
133
+ if (Error error = navigateToParent (previousComponent ))
146
134
return error;
147
135
}
148
136
else
149
137
{
150
- if (Error error = toError (navigationContext. toChild ( std::string{components.first }) ))
138
+ if (Error error = navigateToChild ( std::string{components.first }))
151
139
return error;
152
140
}
141
+ previousComponent = components.first ;
153
142
components = splitPath (components.second );
154
143
}
155
144
@@ -167,11 +156,11 @@ Error Navigator::navigateToAlias(const std::string& alias, const std::string& va
167
156
}
168
157
else if (pathType == PathType::Aliased)
169
158
{
170
- return " @" + alias + " cannot point to other aliases " ;
159
+ return " alias \" @" + alias + " \" cannot point to an aliased path ( \" " + value + " \" ) " ;
171
160
}
172
161
else
173
162
{
174
- if (Error error = toError (navigationContext. jumpToAlias (value) ))
163
+ if (Error error = jumpToAlias (value))
175
164
return error;
176
165
}
177
166
@@ -189,7 +178,7 @@ Error Navigator::navigateToAndPopulateConfig(const std::string& desiredAlias)
189
178
{
190
179
std::optional<std::string> configContents = navigationContext.getConfig ();
191
180
if (!configContents)
192
- return " could not get configuration file contents" ;
181
+ return " could not get configuration file contents to resolve alias \" " + desiredAlias + " \" " ;
193
182
194
183
Luau::ConfigOptions opts;
195
184
Luau::ConfigOptions::AliasOptions aliasOpts;
@@ -205,4 +194,56 @@ Error Navigator::navigateToAndPopulateConfig(const std::string& desiredAlias)
205
194
return std::nullopt;
206
195
}
207
196
197
+ Error Navigator::resetToRequirer ()
198
+ {
199
+ NavigationContext::NavigateResult result = navigationContext.reset (navigationContext.getRequirerIdentifier ());
200
+ if (result == NavigationContext::NavigateResult::Success)
201
+ return std::nullopt;
202
+
203
+ std::string errorMessage = " could not reset to requiring context" ;
204
+ if (result == NavigationContext::NavigateResult::Ambiguous)
205
+ errorMessage += " (ambiguous)" ;
206
+ return errorMessage;
207
+ }
208
+
209
+ Error Navigator::jumpToAlias (const std::string& aliasPath)
210
+ {
211
+ NavigationContext::NavigateResult result = navigationContext.jumpToAlias (aliasPath);
212
+ if (result == NavigationContext::NavigateResult::Success)
213
+ return std::nullopt;
214
+
215
+ std::string errorMessage = " could not jump to alias \" " + aliasPath + " \" " ;
216
+ if (result == NavigationContext::NavigateResult::Ambiguous)
217
+ errorMessage += " (ambiguous)" ;
218
+ return errorMessage;
219
+ }
220
+
221
+ Error Navigator::navigateToParent (std::optional<std::string> previousComponent)
222
+ {
223
+ NavigationContext::NavigateResult result = navigationContext.toParent ();
224
+ if (result == NavigationContext::NavigateResult::Success)
225
+ return std::nullopt;
226
+
227
+ std::string errorMessage;
228
+ if (previousComponent)
229
+ errorMessage = " could not get parent of component \" " + *previousComponent + " \" " ;
230
+ else
231
+ errorMessage = " could not get parent of requiring context" ;
232
+ if (result == NavigationContext::NavigateResult::Ambiguous)
233
+ errorMessage += " (ambiguous)" ;
234
+ return errorMessage;
235
+ }
236
+
237
+ Error Navigator::navigateToChild (const std::string& component)
238
+ {
239
+ NavigationContext::NavigateResult result = navigationContext.toChild (component);
240
+ if (result == NavigationContext::NavigateResult::Success)
241
+ return std::nullopt;
242
+
243
+ std::string errorMessage = " could not resolve child component \" " + component + " \" " ;
244
+ if (result == NavigationContext::NavigateResult::Ambiguous)
245
+ errorMessage += " (ambiguous)" ;
246
+ return errorMessage;
247
+ }
248
+
208
249
} // namespace Luau::Require
0 commit comments