Reserve _i and _n as names we often want to generate

This commit is contained in:
Cyrus Najmabadi
2015-03-13 16:45:58 -07:00
parent 5e85595df6
commit 285497edf8
3 changed files with 56 additions and 49 deletions

View File

@@ -2116,16 +2116,23 @@ module ts {
// Create a temporary variable with a unique unused name. The forLoopVariable parameter signals that the
// name should be one that is appropriate for a for loop variable.
function createTempVariable(location: Node, forLoopVariable?: boolean): Identifier {
let name = forLoopVariable ? "_i" : undefined;
while (true) {
if (name && !isExistingName(location, name)) {
break;
}
function createTempVariable(location: Node, preferredName?: string): Identifier {
let name = preferredName;
for ( ; !name || isExistingName(location, name); tempCount++) {
// _a .. _h, _j ... _z, _0, _1, ...
// Note that _i is skipped
name = "_" + (tempCount < 25 ? String.fromCharCode(tempCount + (tempCount < 8 ? 0 : 1) + CharacterCodes.a) : tempCount - 25);
tempCount++;
// Note: we avoid generating _i and _n as those are common names we want in other places.
var char = CharacterCodes.a + tempCount;
if (char === CharacterCodes.i || char === CharacterCodes.n) {
continue;
}
if (tempCount < 26) {
name = "_" + String.fromCharCode(char);
}
else {
name = "_" + (tempCount - 26);
}
}
// This is necessary so that a name generated via renameNonTopLevelLetAndConst will see the name
@@ -2144,8 +2151,8 @@ module ts {
tempVariables.push(name);
}
function createAndRecordTempVariable(location: Node): Identifier {
let temp = createTempVariable(location, /*forLoopVariable*/ false);
function createAndRecordTempVariable(location: Node, preferredName?: string): Identifier {
let temp = createTempVariable(location, preferredName);
recordTempDeclaration(temp);
return temp;
@@ -3580,10 +3587,10 @@ module ts {
//
// we don't want to emit a temporary variable for the RHS, just use it directly.
let rhsIsIdentifier = node.expression.kind === SyntaxKind.Identifier;
let counter = createTempVariable(node, /*forLoopVariable*/ true);
let rhsReference = rhsIsIdentifier ? <Identifier>node.expression : createTempVariable(node, /*forLoopVariable*/ false);
let counter = createTempVariable(node, /*preferredName*/ "_i");
let rhsReference = rhsIsIdentifier ? <Identifier>node.expression : createTempVariable(node);
var cachedLength = compilerOptions.cacheDownlevelForOfLength ? createTempVariable(node, /*forLoopVariable:*/ false) : undefined;
var cachedLength = compilerOptions.cacheDownlevelForOfLength ? createTempVariable(node, /*preferredName:*/ "_n") : undefined;
// This is the let keyword for the counter and rhsReference. The let keyword for
// the LHS will be emitted inside the body.
@@ -3668,7 +3675,7 @@ module ts {
else {
// It's an empty declaration list. This can only happen in an error case, if the user wrote
// for (let of []) {}
emitNodeWithoutSourceMap(createTempVariable(node, /*forLoopVariable*/ false));
emitNodeWithoutSourceMap(createTempVariable(node));
write(" = ");
emitNodeWithoutSourceMap(rhsIterationValue);
}
@@ -4251,7 +4258,7 @@ module ts {
if (languageVersion < ScriptTarget.ES6 && hasRestParameters(node)) {
let restIndex = node.parameters.length - 1;
let restParam = node.parameters[restIndex];
let tempName = createTempVariable(node, /*forLoopVariable*/ true).text;
let tempName = createTempVariable(node, /*preferredName:*/ "_i").text;
writeLine();
emitLeadingComments(restParam);
emitStart(restParam);