Parse error for whitespace (newline) following return statement
zambetti opened this issue · comments
Description:
When returning an anonymous object from a function, a parse error results if the opening brace is not on the same line as the return statement; this is sensitivity to whitespace unecmascriptlike.
In the two functions below functionA() is fine, but functionB() results in "Uncaught SyntaxError: Unexpected token ':'"
It is written in the specification that a line terminator is not allowed after a return, but this strangely enforces a particular brace style for return statements that does not match what is allowed elsewhere, for example assignments.
// OK
functionA()
{
return {
propertyA: true,
propertyB: true
};
}
// Not OK
function functionB()
{
return
{
propertyA: true,
propertyB: true
};
}
// OK
var myobject =
{
propertyA: true,
propertyB: true
};
That's just how the language works, I'm afraid.
Thank you for your prompt reply. I did find this stated in the specification, but it is a dynamic document so I thought I'd mention this as stylistically anomalous.
Allowing line-termination after return statements could (likely) be implemented while maintaining backward compatibility with the existing behavior.
There is no chance whatsoever that this behavior could be changed without breaking existing programs. Trivially, consider the many programs of the form
function f() {
doWork()
return
function doWork() {/*...*/
}
Note that the reason you're seeing a syntax error instead of ASI behavior is because the following is itself an error.
{
propertyA: true,
propertyB: true
};
In this context, the object literal would need to be wrapped in parens.
There is no chance whatsoever that this behavior could be changed without breaking existing programs. Trivially, consider the many programs of the form
function f() { doWork() return function doWork() {/*...*/ }
Maybe I'm misunderstanding this scenario, but assuming the code below is of the form you have in mind, what problem would be caused by a line-terminator after the return?
function functionC()
{
console.log('C');
functionD();
return {
propertyA: true,
propertyB: true
};
function functionD()
{
console.log('D');
}
}
functionC();
In this context, the object literal would need to be wrapped in parens.
It did not need to be wrapped in parens in the assignment below. Why should a line-terminator be accepted after an assignment and not accepted after return? Are these not analogous scenarios?
var myObject =
{
key1: 'A',
key2: 'B'
};
console.log(myObject);
I appreciate your interest here, but this issue tracker isn't the right place to get support for this kind of thing. Try StackOverflow or the discourse forum.
Thank you. I will have a look at the forum.