diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a1ad9f0344d..5f80444c73e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 && (node.parent).name === node; + return !isPropertyName && getReferencedValueSymbol(node) === argumentsSymbol; } } diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index d0b9ef7455f..d1dab8eeb7a 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -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")); diff --git a/tests/baselines/reference/argumentsAsPropertyName.js b/tests/baselines/reference/argumentsAsPropertyName.js new file mode 100644 index 00000000000..a4197c0a1d2 --- /dev/null +++ b/tests/baselines/reference/argumentsAsPropertyName.js @@ -0,0 +1,29 @@ +//// [argumentsAsPropertyName.ts] +// target: es5 +type MyType = { + arguments: Array +} + +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); + } +} diff --git a/tests/baselines/reference/argumentsAsPropertyName.symbols b/tests/baselines/reference/argumentsAsPropertyName.symbols new file mode 100644 index 00000000000..1e3e41abb6e --- /dev/null +++ b/tests/baselines/reference/argumentsAsPropertyName.symbols @@ -0,0 +1,43 @@ +=== tests/cases/compiler/argumentsAsPropertyName.ts === +// target: es5 +type MyType = { +>MyType : Symbol(MyType, Decl(argumentsAsPropertyName.ts, 0, 0)) + + arguments: Array +>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)) + } +} diff --git a/tests/baselines/reference/argumentsAsPropertyName.types b/tests/baselines/reference/argumentsAsPropertyName.types new file mode 100644 index 00000000000..d0aaaa1cc72 --- /dev/null +++ b/tests/baselines/reference/argumentsAsPropertyName.types @@ -0,0 +1,57 @@ +=== tests/cases/compiler/argumentsAsPropertyName.ts === +// target: es5 +type MyType = { +>MyType : MyType + + arguments: Array +>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 + } +} diff --git a/tests/cases/compiler/argumentsAsPropertyName.ts b/tests/cases/compiler/argumentsAsPropertyName.ts new file mode 100644 index 00000000000..ffecdaa1301 --- /dev/null +++ b/tests/cases/compiler/argumentsAsPropertyName.ts @@ -0,0 +1,15 @@ +// target: es5 +type MyType = { + arguments: Array +} + +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); }) + } +} \ No newline at end of file