Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture 'arguments' in arrow functions in pre-ES6 targets #2764

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
139 changes: 108 additions & 31 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/compiler/diagnosticInformationMap.generated.ts
Expand Up @@ -358,11 +358,13 @@ module ts {
Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2: { code: 2493, category: DiagnosticCategory.Error, key: "Tuple type '{0}' with length '{1}' cannot be assigned to tuple with length '{2}'." },
Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher: { code: 2494, category: DiagnosticCategory.Error, key: "Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher." },
Type_0_is_not_an_array_type_or_a_string_type: { code: 2495, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type or a string type." },
The_arguments_object_cannot_be_referenced_in_an_arrow_function_Consider_using_a_standard_function_expression: { code: 2496, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression." },
The_arguments_object_cannot_be_referenced_in_an_arrow_function_below_ES6_Consider_using_a_standard_function_expression: { code: 2496, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression." },
External_module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct: { code: 2497, category: DiagnosticCategory.Error, key: "External module '{0}' resolves to a non-module entity and cannot be imported using this construct." },
External_module_0_uses_export_and_cannot_be_used_with_export_Asterisk: { code: 2498, category: DiagnosticCategory.Error, key: "External module '{0}' uses 'export =' and cannot be used with 'export *'." },
An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments: { code: 2499, category: DiagnosticCategory.Error, key: "An interface can only extend an identifier/qualified-name with optional type arguments." },
A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments: { code: 2500, category: DiagnosticCategory.Error, key: "A class can only implement an identifier/qualified-name with optional type arguments." },
Duplicate_identifier_arguments_Compiler_uses_variable_declaration_arguments_to_capture_arguments_reference: { code: 2501, category: DiagnosticCategory.Error, key: "Duplicate identifier '_arguments'. Compiler uses variable declaration '_arguments' to capture 'arguments' reference." },
Expression_resolves_to_variable_declaration_arguments_that_compiler_uses_to_capture_arguments_reference: { code: 2502, category: DiagnosticCategory.Error, key: "Expression resolves to variable declaration '_arguments' that compiler uses to capture 'arguments' reference." },
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/diagnosticMessages.json
Expand Up @@ -1419,7 +1419,7 @@
"category": "Error",
"code": 2495
},
"The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.": {
"The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just say "in ES3 or ES5"

"category": "Error",
"code": 2496
},
Expand All @@ -1439,6 +1439,14 @@
"category": "Error",
"code": 2500
},
"Duplicate identifier '_arguments'. Compiler uses variable declaration '_arguments' to capture 'arguments' reference.": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just change these to be templated text, and pass the relevant identifiers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way you can share the one for this and super

"category": "Error",
"code": 2501
},
"Expression resolves to variable declaration '_arguments' that compiler uses to capture 'arguments' reference.": {
"category": "Error",
"code": 2502
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
58 changes: 43 additions & 15 deletions src/compiler/emitter.ts
Expand Up @@ -1204,14 +1204,21 @@ var __param = this.__param || function(index, decorator) { return function (targ
}
}

function emitExpressionIdentifier(node: Identifier) {
let substitution = resolver.getExpressionNameSubstitution(node, getGeneratedNameForNode);
if (substitution) {
write(substitution);
}
else {
writeTextOfNode(currentSourceFile, node);
function emitExpressionIdentifier(node: Identifier, allowGeneratedIdentifiers: boolean): void {
if (allowGeneratedIdentifiers) {
if (resolver.isCapturedArgumentsIdentifier(node)) {
write("_arguments");
return;
}

let substitution = resolver.getExpressionNameSubstitution(node, getGeneratedNameForNode);
if (substitution) {
write(substitution);
return;
}
}

writeTextOfNode(currentSourceFile, node);
}

function getGeneratedNameForIdentifier(node: Identifier): string {
Expand All @@ -1235,15 +1242,26 @@ var __param = this.__param || function(index, decorator) { return function (targ
return;
}
}

if (!node.parent) {
write(node.text);
return;
}
else if (!isNotExpressionIdentifier(node)) {
emitExpressionIdentifier(node);

if (!isNotExpressionIdentifier(node)) {
emitExpressionIdentifier(node, /*allowGeneratedIdentifiers*/ allowGeneratedIdentifiers);
return;
}
else {
writeTextOfNode(currentSourceFile, node);

if (allowGeneratedIdentifiers && isDeclarationName(node)) {
if (resolver.isCapturedArgumentsIdentifier(node)) {
write("_arguments");
return;
}

}

writeTextOfNode(currentSourceFile, node);
}

function emitThis(node: Node) {
Expand Down Expand Up @@ -1518,7 +1536,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
emit((<PropertyAssignment>property).initializer);
}
else if (property.kind === SyntaxKind.ShorthandPropertyAssignment) {
emitExpressionIdentifier((<ShorthandPropertyAssignment>property).name);
emitExpressionIdentifier((<ShorthandPropertyAssignment>property).name, /*allowGeneratedIdentifiers*/ true);
}
else if (property.kind === SyntaxKind.MethodDeclaration) {
emitFunctionDeclaration(<MethodDeclaration>property);
Expand Down Expand Up @@ -1650,15 +1668,15 @@ var __param = this.__param || function(index, decorator) { return function (targ
else {
// Even though this is stored as identifier treat it as an expression
// Short-hand, { x }, is equivalent of normal form { x: x }
emitExpressionIdentifier(node.name);
emitExpressionIdentifier(node.name, /*allowGeneratedIdentifiers*/ true);
}
}
else if (resolver.getExpressionNameSubstitution(node.name, getGeneratedNameForNode)) {
// Emit identifier as an identifier
write(": ");
// Even though this is stored as identifier treat it as an expression
// Short-hand, { x }, is equivalent of normal form { x: x }
emitExpressionIdentifier(node.name);
emitExpressionIdentifier(node.name, /*allowGeneratedIdentifiers*/ true);
}
}

Expand Down Expand Up @@ -2474,7 +2492,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
emitNodeWithoutSourceMap(specifier.name);
emitEnd(specifier.name);
write(" = ");
emitExpressionIdentifier(name);
emitExpressionIdentifier(name, /*allowGeneratedIdentifiers*/ true);
write(";");
}
}
Expand Down Expand Up @@ -3004,6 +3022,16 @@ var __param = this.__param || function(index, decorator) { return function (targ
write("var _this = this;");
emitEnd(node);
}
else if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.CaptureArguments && !hasProperty(node.locals, "arguments")) {
// Only emit this prologue if 'arguments' has't been "shadowed"
// by a local binding named "arguments", since it will already
// have been declared and emitted that way elsewhere.
writeLine();
emitStart(node);
write("var _arguments = arguments;");
emitEnd(node);
}

}

function emitSignatureParameters(node: FunctionLikeDeclaration) {
Expand Down
22 changes: 12 additions & 10 deletions src/compiler/types.ts
Expand Up @@ -1267,6 +1267,7 @@ module ts {
writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
isCapturedArgumentsIdentifier(node: Identifier): void;
isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags): SymbolAccessiblityResult;
isEntityNameVisible(entityName: EntityName | Expression, enclosingDeclaration: Node): SymbolVisibilityResult;
// Returns the constant value this property access resolves to, or 'undefined' for a non-constant
Expand Down Expand Up @@ -1393,18 +1394,19 @@ module ts {
/* @internal */
export const enum NodeCheckFlags {
TypeChecked = 0x00000001, // Node has been type checked
LexicalThis = 0x00000002, // Lexical 'this' reference
CaptureThis = 0x00000004, // Lexical 'this' used in body
EmitExtends = 0x00000008, // Emit __extends
SuperInstance = 0x00000010, // Instance 'super' reference
SuperStatic = 0x00000020, // Static 'super' reference
ContextChecked = 0x00000040, // Contextual types have been assigned
LexicalThis = 0x00000002, // 'this' refers to a lexically captured '_this'.
CaptureThis = 0x00000004, // 'this' needs to be captured as '_this'
CaptureArguments = 0x00000008, // 'arguments' needs to be captured as '_arguments'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need a LexicalArguments flag too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because unlike with this, you can't limit yourself to specific sites that need to be replaced with _arguments - if arguments needs to be captured, then all lexical arguments need to be replaced.

EmitExtends = 0x00000010, // Emit __extends
SuperInstance = 0x00000020, // Instance 'super' reference
SuperStatic = 0x00000040, // Static 'super' reference
ContextChecked = 0x00000080, // Contextual types have been assigned

// Values for enum members have been computed, and any errors have been reported for them.
EnumValuesComputed = 0x00000080,
BlockScopedBindingInLoop = 0x00000100,
EmitDecorate = 0x00000200, // Emit __decorate
EmitParam = 0x00000400, // Emit __param helper for decorators
EnumValuesComputed = 0x00000100,
BlockScopedBindingInLoop = 0x00000200,
EmitDecorate = 0x00000400, // Emit __decorate
EmitParam = 0x00000800, // Emit __param helper for decorators
}

/* @internal */
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/utilities.ts
Expand Up @@ -331,7 +331,7 @@ module ts {
return node.kind === SyntaxKind.EnumDeclaration && isConst(node);
}

function walkUpBindingElementsAndPatterns(node: Node): Node {
export function walkUpBindingElementsAndPatterns(node: Node): Node {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this name. But you don't have to change it now. Just saying

while (node && (node.kind === SyntaxKind.BindingElement || isBindingPattern(node))) {
node = node.parent;
}
Expand Down Expand Up @@ -513,6 +513,16 @@ module ts {
}
}

export function getArgumentsContainer(node: Node, includeArrowFunctions: boolean) {
while (true) {
node = getThisContainer(node, includeArrowFunctions);

if (!node || isFunctionLike(node) || node.kind === SyntaxKind.SourceFile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it not the same as the "this" container?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y'know, in <ES6 mode, it might just be. See #2779.

return <FunctionLikeDeclaration | SourceFile>node;
}
}
}

export function getThisContainer(node: Node, includeArrowFunctions: boolean): Node {
while (true) {
node = node.parent;
Expand Down
@@ -1,37 +1,37 @@
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(2,15): error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(7,19): error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(13,13): error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts(19,15): error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01.ts(2,15): error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01.ts(7,19): error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01.ts(13,13): error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01.ts(19,15): error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.


==== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments.ts (4 errors) ====
==== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01.ts (4 errors) ====
var a = () => {
var arg = arguments[0]; // error
~~~~~~~~~
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
}

var b = function () {
var a = () => {
var arg = arguments[0]; // error
~~~~~~~~~
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
}
}

function baz() {
() => {
var arg = arguments[0];
~~~~~~~~~
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
}
}

function foo(inputFunc: () => void) { }
foo(() => {
var arg = arguments[0]; // error
~~~~~~~~~
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function. Consider using a standard function expression.
!!! error TS2496: The 'arguments' object cannot be referenced in an arrow function below ES6. Consider using a standard function expression.
});

function bar() {
Expand Down
@@ -1,4 +1,4 @@
//// [emitArrowFunctionWhenUsingArgumentsES6.ts]
//// [emitArrowFunctionWhenUsingArguments01.ts]
var a = () => {
var arg = arguments[0]; // error
}
Expand Down Expand Up @@ -31,28 +31,30 @@ function bar() {
}
}

//// [emitArrowFunctionWhenUsingArgumentsES6.js]
var a = () => {
//// [emitArrowFunctionWhenUsingArguments01.js]
var a = function () {
var arg = arguments[0]; // error
};
var b = function () {
var a = () => {
var arg = arguments[0]; // error
var _arguments = arguments;
var a = function () {
var arg = _arguments[0]; // error
};
};
function baz() {
(() => {
var arg = arguments[0];
var _arguments = arguments;
(function () {
var arg = _arguments[0];
});
}
function foo(inputFunc) { }
foo(() => {
foo(function () {
var arg = arguments[0]; // error
});
function bar() {
var arg = arguments[0]; // no error
}
(() => {
(function () {
function foo() {
var arg = arguments[0]; // no error
}
Expand Down
@@ -0,0 +1,40 @@
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01_ES6.ts(2,15): error TS2304: Cannot find name 'arguments'.
tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01_ES6.ts(19,15): error TS2304: Cannot find name 'arguments'.


==== tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments01_ES6.ts (2 errors) ====
var a = () => {
var arg = arguments[0]; // error
~~~~~~~~~
!!! error TS2304: Cannot find name 'arguments'.
}

var b = function () {
var a = () => {
var arg = arguments[0]; // error
}
}

function baz() {
() => {
var arg = arguments[0];
}
}

function foo(inputFunc: () => void) { }
foo(() => {
var arg = arguments[0]; // error
~~~~~~~~~
!!! error TS2304: Cannot find name 'arguments'.
});

function bar() {
var arg = arguments[0]; // no error
}


() => {
function foo() {
var arg = arguments[0]; // no error
}
}
@@ -1,4 +1,4 @@
//// [emitArrowFunctionWhenUsingArguments.ts]
//// [emitArrowFunctionWhenUsingArguments01_ES6.ts]
var a = () => {
var arg = arguments[0]; // error
}
Expand Down Expand Up @@ -31,7 +31,7 @@ function bar() {
}
}

//// [emitArrowFunctionWhenUsingArguments.js]
//// [emitArrowFunctionWhenUsingArguments01_ES6.js]
var a = () => {
var arg = arguments[0]; // error
};
Expand Down