Skip to content

Commit 368dcfa

Browse files
Keep only first assignment as declaration (#1989)
1 parent b176374 commit 368dcfa

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ private AstVisitAction AddReference(SymbolReference symbol)
9494
_ => new ConcurrentBag<SymbolReference> { symbol },
9595
(_, existing) =>
9696
{
97+
// Keep only the first variable encountered as a declaration marked as such. This
98+
// keeps the first assignment without also counting every reassignment as a
99+
// declaration (cleaning up e.g. Code's outline view).
100+
if (symbol.Type is SymbolType.Variable && symbol.IsDeclaration
101+
&& existing.Any(i => i.IsDeclaration))
102+
{
103+
symbol = symbol with { IsDeclaration = false };
104+
}
105+
97106
existing.Add(symbol);
98107
return existing;
99108
});

test/PowerShellEditorServices.Test.Shared/References/SimpleFile.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function My-Function ($myInput)
55

66
$things = 4
77

8-
$things
8+
$things = 3
99

1010
My-Function $things
1111

test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ public async Task FindsFunctionDefinitionInWorkspace()
263263
[Fact]
264264
public async Task FindsVariableDefinition()
265265
{
266-
SymbolReference symbol = await GetDefinition(FindsVariableDefinitionData.SourceDetails).ConfigureAwait(true);
266+
IEnumerable<SymbolReference> definitions = await GetDefinitions(FindsVariableDefinitionData.SourceDetails).ConfigureAwait(true);
267+
SymbolReference symbol = Assert.Single(definitions); // Even though it's re-assigned
267268
Assert.Equal("var things", symbol.Id);
268269
Assert.Equal("$things", symbol.Name);
269270
Assert.Equal(SymbolType.Variable, symbol.Type);

0 commit comments

Comments
 (0)