Skip to content

Commit

Permalink
Scope.deleteVariable also deletes constants with that name.
Browse files Browse the repository at this point in the history
fixes #1089

Deleting a variable should ensure that the name doesn't resolve to
anything in the scope, so it makes sense to also delete any constant
with that name too.
  • Loading branch information
christianp committed Mar 1, 2024
1 parent 12c2ded commit 97d897c
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions runtime/scripts/jme.js
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,7 @@ Scope.prototype = /** @lends Numbas.jme.Scope.prototype */ {
deleteVariable: function(name) {
name = jme.normaliseName(name, this);
this.deleted.variables[name] = true;
this.deleted.constants[name] = true;
},
/** Mark the given function name as deleted from the scope.
*
Expand Down
1 change: 1 addition & 0 deletions tests/jme-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -10549,6 +10549,7 @@ Scope.prototype = /** @lends Numbas.jme.Scope.prototype */ {
deleteVariable: function(name) {
name = jme.normaliseName(name, this);
this.deleted.variables[name] = true;
this.deleted.constants[name] = true;
},
/** Mark the given function name as deleted from the scope.
*
Expand Down
1 change: 1 addition & 0 deletions tests/jme/jme-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
var n = evaluate('repeat(random(3..6),5)[4]').value;
closeEqual(assert, n>=3 && n<=6,true,'last item in repeat(random(3..6),5) is in the correct range');
deepCloseEqual(assert, evaluate('let(x, 2, y, 4, x+y for: y of: 1..3)').value.map(getValue), [3,4,5], 'let(x, 2, y, 4, x+y for: y of: 1..3) - for: replaces the mapped variable');
deepCloseEqual(assert, evaluate('i for: i of: 1..3').value.map(getValue), [1,2,3], 'i for: i of: 1..3 - for: replaces constants when used as the bound variable');
});

QUnit.test('wrapValue',function(assert) {
Expand Down
1 change: 1 addition & 0 deletions tests/numbas-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -10140,6 +10140,7 @@ Scope.prototype = /** @lends Numbas.jme.Scope.prototype */ {
deleteVariable: function(name) {
name = jme.normaliseName(name, this);
this.deleted.variables[name] = true;
this.deleted.constants[name] = true;
},
/** Mark the given function name as deleted from the scope.
*
Expand Down

0 comments on commit 97d897c

Please sign in to comment.