Skip to content

Commit 4ee8213

Browse files
authored
do not capture 'arguments' when property name 'arguments' is met (#13600)
do not capture 'arguments' when property name 'arguments' is met
1 parent 9ced124 commit 4ee8213

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20222,7 +20222,8 @@ namespace ts {
2022220222
if (!isGeneratedIdentifier(node)) {
2022320223
node = getParseTreeNode(node, isIdentifier);
2022420224
if (node) {
20225-
return getReferencedValueSymbol(node) === argumentsSymbol;
20225+
const isPropertyName = node.parent.kind === SyntaxKind.PropertyAccessExpression && (<PropertyAccessExpression>node.parent).name === node;
20226+
return !isPropertyName && getReferencedValueSymbol(node) === argumentsSymbol;
2022620227
}
2022720228
}
2022820229

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ namespace ts {
585585
if (isGeneratedIdentifier(node)) {
586586
return node;
587587
}
588-
if (node.text !== "arguments" && !resolver.isArgumentsLocalBinding(node)) {
588+
if (node.text !== "arguments" || !resolver.isArgumentsLocalBinding(node)) {
589589
return node;
590590
}
591591
return convertedLoopState.argumentsName || (convertedLoopState.argumentsName = createUniqueName("arguments"));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [argumentsAsPropertyName.ts]
2+
// target: es5
3+
type MyType = {
4+
arguments: Array<string>
5+
}
6+
7+
declare function use(s: any);
8+
9+
function myFunction(myType: MyType) {
10+
for (let i = 0; i < 10; i++) {
11+
use(myType.arguments[i]);
12+
// create closure so that tsc will turn loop body into function
13+
const x = 5;
14+
[1, 2, 3].forEach(function(j) { use(x); })
15+
}
16+
}
17+
18+
//// [argumentsAsPropertyName.js]
19+
function myFunction(myType) {
20+
var _loop_1 = function (i) {
21+
use(myType.arguments[i]);
22+
// create closure so that tsc will turn loop body into function
23+
var x = 5;
24+
[1, 2, 3].forEach(function (j) { use(x); });
25+
};
26+
for (var i = 0; i < 10; i++) {
27+
_loop_1(i);
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=== tests/cases/compiler/argumentsAsPropertyName.ts ===
2+
// target: es5
3+
type MyType = {
4+
>MyType : Symbol(MyType, Decl(argumentsAsPropertyName.ts, 0, 0))
5+
6+
arguments: Array<string>
7+
>arguments : Symbol(arguments, Decl(argumentsAsPropertyName.ts, 1, 15))
8+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
9+
}
10+
11+
declare function use(s: any);
12+
>use : Symbol(use, Decl(argumentsAsPropertyName.ts, 3, 1))
13+
>s : Symbol(s, Decl(argumentsAsPropertyName.ts, 5, 21))
14+
15+
function myFunction(myType: MyType) {
16+
>myFunction : Symbol(myFunction, Decl(argumentsAsPropertyName.ts, 5, 29))
17+
>myType : Symbol(myType, Decl(argumentsAsPropertyName.ts, 7, 20))
18+
>MyType : Symbol(MyType, Decl(argumentsAsPropertyName.ts, 0, 0))
19+
20+
for (let i = 0; i < 10; i++) {
21+
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
22+
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
23+
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
24+
25+
use(myType.arguments[i]);
26+
>use : Symbol(use, Decl(argumentsAsPropertyName.ts, 3, 1))
27+
>myType.arguments : Symbol(arguments, Decl(argumentsAsPropertyName.ts, 1, 15))
28+
>myType : Symbol(myType, Decl(argumentsAsPropertyName.ts, 7, 20))
29+
>arguments : Symbol(arguments, Decl(argumentsAsPropertyName.ts, 1, 15))
30+
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
31+
32+
// create closure so that tsc will turn loop body into function
33+
const x = 5;
34+
>x : Symbol(x, Decl(argumentsAsPropertyName.ts, 11, 13))
35+
36+
[1, 2, 3].forEach(function(j) { use(x); })
37+
>[1, 2, 3].forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
38+
>forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
39+
>j : Symbol(j, Decl(argumentsAsPropertyName.ts, 12, 35))
40+
>use : Symbol(use, Decl(argumentsAsPropertyName.ts, 3, 1))
41+
>x : Symbol(x, Decl(argumentsAsPropertyName.ts, 11, 13))
42+
}
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
=== tests/cases/compiler/argumentsAsPropertyName.ts ===
2+
// target: es5
3+
type MyType = {
4+
>MyType : MyType
5+
6+
arguments: Array<string>
7+
>arguments : string[]
8+
>Array : T[]
9+
}
10+
11+
declare function use(s: any);
12+
>use : (s: any) => any
13+
>s : any
14+
15+
function myFunction(myType: MyType) {
16+
>myFunction : (myType: MyType) => void
17+
>myType : MyType
18+
>MyType : MyType
19+
20+
for (let i = 0; i < 10; i++) {
21+
>i : number
22+
>0 : 0
23+
>i < 10 : boolean
24+
>i : number
25+
>10 : 10
26+
>i++ : number
27+
>i : number
28+
29+
use(myType.arguments[i]);
30+
>use(myType.arguments[i]) : any
31+
>use : (s: any) => any
32+
>myType.arguments[i] : string
33+
>myType.arguments : string[]
34+
>myType : MyType
35+
>arguments : string[]
36+
>i : number
37+
38+
// create closure so that tsc will turn loop body into function
39+
const x = 5;
40+
>x : 5
41+
>5 : 5
42+
43+
[1, 2, 3].forEach(function(j) { use(x); })
44+
>[1, 2, 3].forEach(function(j) { use(x); }) : void
45+
>[1, 2, 3].forEach : (callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void
46+
>[1, 2, 3] : number[]
47+
>1 : 1
48+
>2 : 2
49+
>3 : 3
50+
>forEach : (callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void
51+
>function(j) { use(x); } : (j: number) => void
52+
>j : number
53+
>use(x) : any
54+
>use : (s: any) => any
55+
>x : 5
56+
}
57+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// target: es5
2+
type MyType = {
3+
arguments: Array<string>
4+
}
5+
6+
declare function use(s: any);
7+
8+
function myFunction(myType: MyType) {
9+
for (let i = 0; i < 10; i++) {
10+
use(myType.arguments[i]);
11+
// create closure so that tsc will turn loop body into function
12+
const x = 5;
13+
[1, 2, 3].forEach(function(j) { use(x); })
14+
}
15+
}

0 commit comments

Comments
 (0)