Skip to content

Commit 285497e

Browse files
Reserve _i and _n as names we often want to generate
1 parent 5e85595 commit 285497e

3 files changed

Lines changed: 51 additions & 44 deletions

File tree

src/compiler/emitter.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,16 +2116,23 @@ module ts {
21162116

21172117
// Create a temporary variable with a unique unused name. The forLoopVariable parameter signals that the
21182118
// name should be one that is appropriate for a for loop variable.
2119-
function createTempVariable(location: Node, forLoopVariable?: boolean): Identifier {
2120-
let name = forLoopVariable ? "_i" : undefined;
2121-
while (true) {
2122-
if (name && !isExistingName(location, name)) {
2123-
break;
2124-
}
2119+
function createTempVariable(location: Node, preferredName?: string): Identifier {
2120+
let name = preferredName;
2121+
for ( ; !name || isExistingName(location, name); tempCount++) {
21252122
// _a .. _h, _j ... _z, _0, _1, ...
2126-
// Note that _i is skipped
2127-
name = "_" + (tempCount < 25 ? String.fromCharCode(tempCount + (tempCount < 8 ? 0 : 1) + CharacterCodes.a) : tempCount - 25);
2128-
tempCount++;
2123+
2124+
// Note: we avoid generating _i and _n as those are common names we want in other places.
2125+
var char = CharacterCodes.a + tempCount;
2126+
if (char === CharacterCodes.i || char === CharacterCodes.n) {
2127+
continue;
2128+
}
2129+
2130+
if (tempCount < 26) {
2131+
name = "_" + String.fromCharCode(char);
2132+
}
2133+
else {
2134+
name = "_" + (tempCount - 26);
2135+
}
21292136
}
21302137

21312138
// This is necessary so that a name generated via renameNonTopLevelLetAndConst will see the name
@@ -2144,8 +2151,8 @@ module ts {
21442151
tempVariables.push(name);
21452152
}
21462153

2147-
function createAndRecordTempVariable(location: Node): Identifier {
2148-
let temp = createTempVariable(location, /*forLoopVariable*/ false);
2154+
function createAndRecordTempVariable(location: Node, preferredName?: string): Identifier {
2155+
let temp = createTempVariable(location, preferredName);
21492156
recordTempDeclaration(temp);
21502157

21512158
return temp;
@@ -3580,10 +3587,10 @@ module ts {
35803587
//
35813588
// we don't want to emit a temporary variable for the RHS, just use it directly.
35823589
let rhsIsIdentifier = node.expression.kind === SyntaxKind.Identifier;
3583-
let counter = createTempVariable(node, /*forLoopVariable*/ true);
3584-
let rhsReference = rhsIsIdentifier ? <Identifier>node.expression : createTempVariable(node, /*forLoopVariable*/ false);
3590+
let counter = createTempVariable(node, /*preferredName*/ "_i");
3591+
let rhsReference = rhsIsIdentifier ? <Identifier>node.expression : createTempVariable(node);
35853592

3586-
var cachedLength = compilerOptions.cacheDownlevelForOfLength ? createTempVariable(node, /*forLoopVariable:*/ false) : undefined;
3593+
var cachedLength = compilerOptions.cacheDownlevelForOfLength ? createTempVariable(node, /*preferredName:*/ "_n") : undefined;
35873594

35883595
// This is the let keyword for the counter and rhsReference. The let keyword for
35893596
// the LHS will be emitted inside the body.
@@ -3668,7 +3675,7 @@ module ts {
36683675
else {
36693676
// It's an empty declaration list. This can only happen in an error case, if the user wrote
36703677
// for (let of []) {}
3671-
emitNodeWithoutSourceMap(createTempVariable(node, /*forLoopVariable*/ false));
3678+
emitNodeWithoutSourceMap(createTempVariable(node));
36723679
write(" = ");
36733680
emitNodeWithoutSourceMap(rhsIterationValue);
36743681
}
@@ -4251,7 +4258,7 @@ module ts {
42514258
if (languageVersion < ScriptTarget.ES6 && hasRestParameters(node)) {
42524259
let restIndex = node.parameters.length - 1;
42534260
let restParam = node.parameters[restIndex];
4254-
let tempName = createTempVariable(node, /*forLoopVariable*/ true).text;
4261+
let tempName = createTempVariable(node, /*preferredName:*/ "_i").text;
42554262
writeLine();
42564263
emitLeadingComments(restParam);
42574264
emitStart(restParam);

tests/baselines/reference/taggedTemplateStringsTypeArgumentInference.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,31 +145,31 @@ function someGenerics4(strs, n, f) {
145145
// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type
146146
function someGenerics5(strs, n, f) {
147147
}
148-
(_n = ["", " ", ""], _n.raw = ["", " ", ""], someGenerics5(_n, 4, function () {
148+
(_o = ["", " ", ""], _o.raw = ["", " ", ""], someGenerics5(_o, 4, function () {
149149
return null;
150150
}));
151-
(_o = ["", "", ""], _o.raw = ["", "", ""], someGenerics5(_o, '', function () {
151+
(_p = ["", "", ""], _p.raw = ["", "", ""], someGenerics5(_p, '', function () {
152152
return 3;
153153
}));
154-
(_p = ["", "", ""], _p.raw = ["", "", ""], someGenerics5(_p, null, null));
154+
(_q = ["", "", ""], _q.raw = ["", "", ""], someGenerics5(_q, null, null));
155155
// Generic tag with multiple arguments of function types that each have parameters of the same generic type
156156
function someGenerics6(strs, a, b, c) {
157157
}
158-
(_q = ["", "", "", ""], _q.raw = ["", "", "", ""], someGenerics6(_q, function (n) {
158+
(_r = ["", "", "", ""], _r.raw = ["", "", "", ""], someGenerics6(_r, function (n) {
159159
return n;
160160
}, function (n) {
161161
return n;
162162
}, function (n) {
163163
return n;
164164
}));
165-
(_r = ["", "", "", ""], _r.raw = ["", "", "", ""], someGenerics6(_r, function (n) {
165+
(_s = ["", "", "", ""], _s.raw = ["", "", "", ""], someGenerics6(_s, function (n) {
166166
return n;
167167
}, function (n) {
168168
return n;
169169
}, function (n) {
170170
return n;
171171
}));
172-
(_s = ["", "", "", ""], _s.raw = ["", "", "", ""], someGenerics6(_s, function (n) {
172+
(_t = ["", "", "", ""], _t.raw = ["", "", "", ""], someGenerics6(_t, function (n) {
173173
return n;
174174
}, function (n) {
175175
return n;
@@ -179,21 +179,21 @@ function someGenerics6(strs, a, b, c) {
179179
// Generic tag with multiple arguments of function types that each have parameters of different generic type
180180
function someGenerics7(strs, a, b, c) {
181181
}
182-
(_t = ["", "", "", ""], _t.raw = ["", "", "", ""], someGenerics7(_t, function (n) {
182+
(_u = ["", "", "", ""], _u.raw = ["", "", "", ""], someGenerics7(_u, function (n) {
183183
return n;
184184
}, function (n) {
185185
return n;
186186
}, function (n) {
187187
return n;
188188
}));
189-
(_u = ["", "", "", ""], _u.raw = ["", "", "", ""], someGenerics7(_u, function (n) {
189+
(_v = ["", "", "", ""], _v.raw = ["", "", "", ""], someGenerics7(_v, function (n) {
190190
return n;
191191
}, function (n) {
192192
return n;
193193
}, function (n) {
194194
return n;
195195
}));
196-
(_v = ["", "", "", ""], _v.raw = ["", "", "", ""], someGenerics7(_v, function (n) {
196+
(_w = ["", "", "", ""], _w.raw = ["", "", "", ""], someGenerics7(_w, function (n) {
197197
return n;
198198
}, function (n) {
199199
return n;
@@ -204,15 +204,15 @@ function someGenerics7(strs, a, b, c) {
204204
function someGenerics8(strs, n) {
205205
return n;
206206
}
207-
var x = (_w = ["", ""], _w.raw = ["", ""], someGenerics8(_w, someGenerics7));
208-
(_x = ["", "", "", ""], _x.raw = ["", "", "", ""], x(_x, null, null, null));
207+
var x = (_x = ["", ""], _x.raw = ["", ""], someGenerics8(_x, someGenerics7));
208+
(_y = ["", "", "", ""], _y.raw = ["", "", "", ""], x(_y, null, null, null));
209209
// Generic tag with multiple parameters of generic type passed arguments with no best common type
210210
function someGenerics9(strs, a, b, c) {
211211
return null;
212212
}
213-
var a9a = (_y = ["", "", "", ""], _y.raw = ["", "", "", ""], someGenerics9(_y, '', 0, []));
213+
var a9a = (_z = ["", "", "", ""], _z.raw = ["", "", "", ""], someGenerics9(_z, '', 0, []));
214214
var a9a;
215-
var a9e = (_z = ["", "", "", ""], _z.raw = ["", "", "", ""], someGenerics9(_z, undefined, {
215+
var a9e = (_0 = ["", "", "", ""], _0.raw = ["", "", "", ""], someGenerics9(_0, undefined, {
216216
x: 6,
217217
z: new Date()
218218
}, {
@@ -221,7 +221,7 @@ var a9e = (_z = ["", "", "", ""], _z.raw = ["", "", "", ""], someGenerics9(_z, u
221221
}));
222222
var a9e;
223223
// Generic tag with multiple parameters of generic type passed arguments with a single best common type
224-
var a9d = (_0 = ["", "", "", ""], _0.raw = ["", "", "", ""], someGenerics9(_0, {
224+
var a9d = (_1 = ["", "", "", ""], _1.raw = ["", "", "", ""], someGenerics9(_1, {
225225
x: 3
226226
}, {
227227
x: 6
@@ -231,9 +231,9 @@ var a9d = (_0 = ["", "", "", ""], _0.raw = ["", "", "", ""], someGenerics9(_0, {
231231
var a9d;
232232
// Generic tag with multiple parameters of generic type where one argument is of type 'any'
233233
var anyVar;
234-
var a = (_1 = ["", "", "", ""], _1.raw = ["", "", "", ""], someGenerics9(_1, 7, anyVar, 4));
234+
var a = (_2 = ["", "", "", ""], _2.raw = ["", "", "", ""], someGenerics9(_2, 7, anyVar, 4));
235235
var a;
236236
// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any'
237-
var arr = (_2 = ["", "", "", ""], _2.raw = ["", "", "", ""], someGenerics9(_2, [], null, undefined));
237+
var arr = (_3 = ["", "", "", ""], _3.raw = ["", "", "", ""], someGenerics9(_3, [], null, undefined));
238238
var arr;
239-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
239+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3;

tests/baselines/reference/taggedTemplateStringsWithOverloadResolution3.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,26 @@ var s = (_k = ["", ""], _k.raw = ["", ""], fn3(_k, 4));
103103
var s = (_l = ["", "", "", ""], _l.raw = ["", "", "", ""], fn3(_l, '', '', ''));
104104
var n = (_m = ["", "", "", ""], _m.raw = ["", "", "", ""], fn3(_m, '', '', 3));
105105
// Generic overloads with differing arity tagging with argument count that doesn't match any overload
106-
(_n = [""], _n.raw = [""], fn3(_n)); // Error
106+
(_o = [""], _o.raw = [""], fn3(_o)); // Error
107107
function fn4() {
108108
}
109109
// Generic overloads with constraints tagged with types that satisfy the constraints
110-
(_o = ["", "", ""], _o.raw = ["", "", ""], fn4(_o, '', 3));
111-
(_p = ["", "", ""], _p.raw = ["", "", ""], fn4(_p, 3, ''));
112-
(_q = ["", "", ""], _q.raw = ["", "", ""], fn4(_q, 3, undefined));
113-
(_r = ["", "", ""], _r.raw = ["", "", ""], fn4(_r, '', null));
110+
(_p = ["", "", ""], _p.raw = ["", "", ""], fn4(_p, '', 3));
111+
(_q = ["", "", ""], _q.raw = ["", "", ""], fn4(_q, 3, ''));
112+
(_r = ["", "", ""], _r.raw = ["", "", ""], fn4(_r, 3, undefined));
113+
(_s = ["", "", ""], _s.raw = ["", "", ""], fn4(_s, '', null));
114114
// Generic overloads with constraints called with type arguments that do not satisfy the constraints
115-
(_s = ["", "", ""], _s.raw = ["", "", ""], fn4(_s, null, null)); // Error
115+
(_t = ["", "", ""], _t.raw = ["", "", ""], fn4(_t, null, null)); // Error
116116
// Generic overloads with constraints called without type arguments but with types that do not satisfy the constraints
117-
(_t = ["", "", ""], _t.raw = ["", "", ""], fn4(_t, true, null));
118-
(_u = ["", "", ""], _u.raw = ["", "", ""], fn4(_u, null, true));
117+
(_u = ["", "", ""], _u.raw = ["", "", ""], fn4(_u, true, null));
118+
(_v = ["", "", ""], _v.raw = ["", "", ""], fn4(_v, null, true));
119119
function fn5() {
120120
return undefined;
121121
}
122-
(_v = ["", ""], _v.raw = ["", ""], fn5(_v, function (n) {
122+
(_w = ["", ""], _w.raw = ["", ""], fn5(_w, function (n) {
123123
return n.toFixed();
124124
})); // will error; 'n' should have type 'string'.
125-
(_w = ["", ""], _w.raw = ["", ""], fn5(_w, function (n) {
125+
(_x = ["", ""], _x.raw = ["", ""], fn5(_x, function (n) {
126126
return n.substr(0);
127127
}));
128-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
128+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;

0 commit comments

Comments
 (0)