Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit f1254b0

Browse files
author
Mikhail Arkhipov
committed
Function scope check
1 parent 1a78270 commit f1254b0

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/Analysis/Engine/Impl/ModuleAnalysis.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ public VariablesResult GetVariables(Expression expr, SourceLocation location, st
239239
foreach (var s in scope.EnumerateTowardsGlobal) {
240240
var scopeVariables = GetVariablesInScope(name, s).Distinct();
241241
variables = variables.Union(scopeVariables);
242-
var args = scopeVariables.Where(v => IsFunctionArgument(v.Variable));
243-
if(args.Any()) {
242+
var args = scopeVariables.Where(v => IsFunctionArgument(s, v));
243+
if (args.Any()) {
244244
break;
245245
}
246246
}
@@ -255,7 +255,7 @@ public VariablesResult GetVariables(Expression expr, SourceLocation location, st
255255
if (definitions.Length > 0) {
256256
var defsToRefs = definitions.Skip(1).Select(v => new AnalysisVariable(v.Variable, VariableType.Reference, v.Location));
257257
variables = definitions.Take(1).Concat(others.Concat(defsToRefs));
258-
}
258+
}
259259
}
260260
return new VariablesResult(variables, unit.Tree);
261261
}
@@ -270,8 +270,17 @@ public VariablesResult GetVariables(Expression expr, SourceLocation location, st
270270
return new VariablesResult(variables, unit.Tree);
271271
}
272272

273-
private bool IsFunctionArgument(IVariableDefinition v)
274-
=> v?.Types?.MaybeEnumerate().FirstOrDefault() is ParameterInfo;
273+
private bool IsFunctionArgument(IScope scope, IAnalysisVariable v) {
274+
if (v.Variable?.Types?.MaybeEnumerate().FirstOrDefault() is ParameterInfo) {
275+
return true;
276+
}
277+
if (scope is FunctionScope funcScope) {
278+
var def = funcScope.Function.FunctionDefinition;
279+
// TODO: Use indexes rather than lines to check location
280+
return v.Location.StartLine == def.GetStart(def.GlobalParent).Line;
281+
}
282+
return false;
283+
}
275284

276285
private IEnumerable<IAnalysisVariable> GetVariablesInScope(NameExpression name, IScope scope) {
277286
var result = new List<IAnalysisVariable>();

src/Analysis/Engine/Test/AnalysisTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,6 +3923,7 @@ def f(x = x):
39233923
using (var server = await CreateServerAsync()) {
39243924
var uri = TestData.GetDefaultModuleUri();
39253925
await server.OpenDefaultDocumentAndGetAnalysisAsync(code);
3926+
39263927
var referencesx1 = await server.SendFindReferences(uri, 2, 6);
39273928
var referencesx2 = await server.SendFindReferences(uri, 2, 10);
39283929
var referencesx3 = await server.SendFindReferences(uri, 3, 4);
@@ -4980,6 +4981,7 @@ public async Task CrossModule(int[] permutation) {
49804981

49814982
await server.SendDidOpenTextDocument(uris[permutation[0]], contents[permutation[0]]);
49824983
await server.SendDidOpenTextDocument(uris[permutation[1]], contents[permutation[1]]);
4984+
await server.WaitForCompleteAnalysisAsync(CancellationToken.None);
49834985

49844986
var analysis = await server.GetAnalysisAsync(uris[0]);
49854987
analysis.Should().HaveVariable("module2").WithValue<IModuleInfo>()
@@ -5586,6 +5588,7 @@ def mydec(self, x):
55865588
await server.SendDidOpenTextDocument(uris[permutation[0]], contents[permutation[0]]);
55875589
await server.SendDidOpenTextDocument(uris[permutation[1]], contents[permutation[1]]);
55885590

5591+
await server.WaitForCompleteAnalysisAsync(CancellationToken.None);
55895592
var analysis1 = await server.GetAnalysisAsync(uris[0]);
55905593
var analysis2 = await server.GetAnalysisAsync(uris[1]);
55915594

0 commit comments

Comments
 (0)