do not capture 'arguments' when property name 'arguments' is met (#13600)

do not capture 'arguments' when property name 'arguments' is met
This commit is contained in:
Vladimir Matveev 2017-01-20 19:59:26 -08:00 committed by GitHub
parent 9ced124579
commit 4ee8213dde
6 changed files with 147 additions and 2 deletions

View file

@ -20222,7 +20222,8 @@ namespace ts {
if (!isGeneratedIdentifier(node)) {
node = getParseTreeNode(node, isIdentifier);
if (node) {
return getReferencedValueSymbol(node) === argumentsSymbol;
const isPropertyName = node.parent.kind === SyntaxKind.PropertyAccessExpression && (<PropertyAccessExpression>node.parent).name === node;
return !isPropertyName && getReferencedValueSymbol(node) === argumentsSymbol;
}
}

View file

@ -585,7 +585,7 @@ namespace ts {
if (isGeneratedIdentifier(node)) {
return node;
}
if (node.text !== "arguments" && !resolver.isArgumentsLocalBinding(node)) {
if (node.text !== "arguments" || !resolver.isArgumentsLocalBinding(node)) {
return node;
}
return convertedLoopState.argumentsName || (convertedLoopState.argumentsName = createUniqueName("arguments"));

View file

@ -0,0 +1,29 @@
//// [argumentsAsPropertyName.ts]
// target: es5
type MyType = {
arguments: Array<string>
}
declare function use(s: any);
function myFunction(myType: MyType) {
for (let i = 0; i < 10; i++) {
use(myType.arguments[i]);
// create closure so that tsc will turn loop body into function
const x = 5;
[1, 2, 3].forEach(function(j) { use(x); })
}
}
//// [argumentsAsPropertyName.js]
function myFunction(myType) {
var _loop_1 = function (i) {
use(myType.arguments[i]);
// create closure so that tsc will turn loop body into function
var x = 5;
[1, 2, 3].forEach(function (j) { use(x); });
};
for (var i = 0; i < 10; i++) {
_loop_1(i);
}
}

View file

@ -0,0 +1,43 @@
=== tests/cases/compiler/argumentsAsPropertyName.ts ===
// target: es5
type MyType = {
>MyType : Symbol(MyType, Decl(argumentsAsPropertyName.ts, 0, 0))
arguments: Array<string>
>arguments : Symbol(arguments, Decl(argumentsAsPropertyName.ts, 1, 15))
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
}
declare function use(s: any);
>use : Symbol(use, Decl(argumentsAsPropertyName.ts, 3, 1))
>s : Symbol(s, Decl(argumentsAsPropertyName.ts, 5, 21))
function myFunction(myType: MyType) {
>myFunction : Symbol(myFunction, Decl(argumentsAsPropertyName.ts, 5, 29))
>myType : Symbol(myType, Decl(argumentsAsPropertyName.ts, 7, 20))
>MyType : Symbol(MyType, Decl(argumentsAsPropertyName.ts, 0, 0))
for (let i = 0; i < 10; i++) {
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
use(myType.arguments[i]);
>use : Symbol(use, Decl(argumentsAsPropertyName.ts, 3, 1))
>myType.arguments : Symbol(arguments, Decl(argumentsAsPropertyName.ts, 1, 15))
>myType : Symbol(myType, Decl(argumentsAsPropertyName.ts, 7, 20))
>arguments : Symbol(arguments, Decl(argumentsAsPropertyName.ts, 1, 15))
>i : Symbol(i, Decl(argumentsAsPropertyName.ts, 8, 12))
// create closure so that tsc will turn loop body into function
const x = 5;
>x : Symbol(x, Decl(argumentsAsPropertyName.ts, 11, 13))
[1, 2, 3].forEach(function(j) { use(x); })
>[1, 2, 3].forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
>forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
>j : Symbol(j, Decl(argumentsAsPropertyName.ts, 12, 35))
>use : Symbol(use, Decl(argumentsAsPropertyName.ts, 3, 1))
>x : Symbol(x, Decl(argumentsAsPropertyName.ts, 11, 13))
}
}

View file

@ -0,0 +1,57 @@
=== tests/cases/compiler/argumentsAsPropertyName.ts ===
// target: es5
type MyType = {
>MyType : MyType
arguments: Array<string>
>arguments : string[]
>Array : T[]
}
declare function use(s: any);
>use : (s: any) => any
>s : any
function myFunction(myType: MyType) {
>myFunction : (myType: MyType) => void
>myType : MyType
>MyType : MyType
for (let i = 0; i < 10; i++) {
>i : number
>0 : 0
>i < 10 : boolean
>i : number
>10 : 10
>i++ : number
>i : number
use(myType.arguments[i]);
>use(myType.arguments[i]) : any
>use : (s: any) => any
>myType.arguments[i] : string
>myType.arguments : string[]
>myType : MyType
>arguments : string[]
>i : number
// create closure so that tsc will turn loop body into function
const x = 5;
>x : 5
>5 : 5
[1, 2, 3].forEach(function(j) { use(x); })
>[1, 2, 3].forEach(function(j) { use(x); }) : void
>[1, 2, 3].forEach : (callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3
>forEach : (callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void
>function(j) { use(x); } : (j: number) => void
>j : number
>use(x) : any
>use : (s: any) => any
>x : 5
}
}

View file

@ -0,0 +1,15 @@
// target: es5
type MyType = {
arguments: Array<string>
}
declare function use(s: any);
function myFunction(myType: MyType) {
for (let i = 0; i < 10; i++) {
use(myType.arguments[i]);
// create closure so that tsc will turn loop body into function
const x = 5;
[1, 2, 3].forEach(function(j) { use(x); })
}
}