Merge pull request #4274 from lydell/for-variables

Improve naming of generated 'i-variables'
This commit is contained in:
Simon Lydell 2016-06-19 12:00:33 +02:00 committed by GitHub
commit ec333a061b
3 changed files with 26 additions and 4 deletions

View File

@ -66,13 +66,20 @@
};
Scope.prototype.temporary = function(name, index, single) {
var diff, endCode, letter, newCode, num, startCode;
if (single == null) {
single = false;
}
if (single) {
return (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
startCode = name.charCodeAt(0);
endCode = 'z'.charCodeAt(0);
diff = endCode - startCode;
newCode = startCode + index % (diff + 1);
letter = String.fromCharCode(newCode);
num = Math.floor(index / (diff + 1));
return "" + letter + (num || '');
} else {
return name + (index || '');
return "" + name + (index || '');
}
};

View File

@ -66,9 +66,15 @@ Generate a temporary variable name at the given index.
temporary: (name, index, single=false) ->
if single
(index + parseInt name, 36).toString(36).replace /\d/g, 'a'
startCode = name.charCodeAt(0)
endCode = 'z'.charCodeAt(0)
diff = endCode - startCode
newCode = startCode + index % (diff + 1)
letter = String.fromCharCode(newCode)
num = index // (diff + 1)
"#{letter}#{num or ''}"
else
name + (index or '')
"#{name}#{index or ''}"
Gets the type of a variable.

View File

@ -464,3 +464,12 @@ test "#2367: super in for-loop", ->
@sum
eq 10, (new Bar).add 2, 3, 5
test "#4267: lots of for-loops in the same scope", ->
# This used to include the invalid JavaScript `var do = 0`.
code = """
do ->
#{Array(200).join('for [0..0] then\n ')}
true
"""
ok CoffeeScript.eval(code)