Skip to content

Commit 6b6b309

Browse files
authored
Fix a bug where ForEach kept variable definitions across loop iterations (#12)
1 parent d1134cd commit 6b6b309

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
# v0.1.7
2+
3+
* Fixed a bug where `ForEach` kept variable definitions across loop iterations. An example of how this bug manifested is that adding to a "current-scope non-existant, parent-scope existant, current-scope variable" in a `ForEach` loop added to the previous loop-iteration value instead of redefining it each time.
4+
5+
16
# v0.1.6
27

3-
* [#7](https://github.com/harrisont/fbuild-vscode-lsp/issues/7) Wait for a delay before while the user is typing (debounce) before evaluating, to improve performance.
8+
* [#7](https://github.com/harrisont/fbuild-vscode-lsp/issues/7) Wait for a delay before updating (debounce), to improve performance.
49

510
# v0.1.5
611

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "fastbuild-support",
33
"displayName": "FASTBuild Support",
44
"description": "FASTBuild language support. Includes go-to definition, find references, variable evaluation, syntax errors, etc.",
5-
"version": "0.1.6",
5+
"version": "0.1.7",
66
"preview": true,
77
"publisher": "HarrisonT",
88
"author": {

server/src/evaluator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,8 +1157,8 @@ function evaluateStatements(statements: Statement[], context: EvaluationContext)
11571157
}
11581158

11591159
let error: Error | null = null;
1160-
context.scopeStack.withScope(() => {
1161-
for (const arrayItem of arrayItems) {
1160+
for (const arrayItem of arrayItems) {
1161+
context.scopeStack.withScope(() => {
11621162
const variable = context.scopeStack.setVariableInCurrentScope(evaluatedLoopVarNameValue, arrayItem, definition);
11631163

11641164
// The loop variable is a variable reference.
@@ -1176,8 +1176,8 @@ function evaluateStatements(statements: Statement[], context: EvaluationContext)
11761176
error = evaluatedStatementsAndMaybeError.error;
11771177
return;
11781178
}
1179-
}
1180-
});
1179+
});
1180+
}
11811181
if (error !== null) {
11821182
return new DataAndMaybeError(result, error);
11831183
}

server/src/test/2-evaluator.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,25 @@ describe('evaluator', () => {
994994
]);
995995
});
996996

997+
// Similar to the above test, but in a loop
998+
it('in a loop, adding to a current-scope non-existant, parent-scope existant, current-scope variable redefines it each time in the current scope to be the sum', () => {
999+
const input = `
1000+
.MyArray = {'a', 'b', 'c'}
1001+
.MyMessage = 'Base'
1002+
ForEach( .Item in .MyArray )
1003+
{
1004+
.MyMessage + '-$Item$'
1005+
Print( .MyMessage )
1006+
}
1007+
`;
1008+
assertEvaluatedVariablesValueEqual(input, [
1009+
['a', 'b', 'c'],
1010+
'a', 'Base', 'Base-a',
1011+
'b', 'Base', 'Base-b',
1012+
'c', 'Base', 'Base-c',
1013+
]);
1014+
});
1015+
9971016
it('adding to a current-scope non-existant, parent-scope existant, current-scope struct variable defines it in the current scope to be the sum', () => {
9981017
const input = `
9991018
.MyMessage = 'hello'

0 commit comments

Comments
 (0)