Reserved word property access and line breaks

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Reserved word property access and line breaks

Allen Wirfs-Brock-2
Consider the following JavaScript code fragment:

var x=foo.
if (a>b)  -bar;

If this showed up in real code, it would probably be either as a typo where either the actual property name for foo was left off or because of a mental lapse by an old Smalltalk programmer who was used to using . instead of ; to terminate statements. Regardless, it will validly parse identically to the following single line formulation:

var x=foo.if(a>b)-bar;

The ES5 grammar for a dotted property access is:

CallExpression ::
    CallExpression . IdentifierName

Where IdentifierName includes all possible identifiers including reserved words.  The above confusion could have been avoided if we had instead defined things as:

CallExpression ::
    CallExpression . Identifier
    CallExpression . [no LineTerminator here] ReservedWord

Is this the grammar we should have used for ES5?  It's too late to make this change in the ES5 spec.  but if we had consensus that this is how it should be specified in Harmony we might also get the emerging ES5 implementations to also follow this interpretations.

Thoughts?  It's kind of hooky to have semi-arbitrary restrictions on line terminator placement.  But we already have them in order to make semi-colon insertion more sane and arguably such restrictions reasonably go hand-in-hand with allowing context sensitive use of reserved words.

Allen
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Reserved word property access and line breaks

Oliver Hunt-2
It does seem reasonable to disallow new lines in that context.

--Oliver

On Aug 24, 2010, at 1:53 PM, Allen Wirfs-Brock wrote:

> Consider the following JavaScript code fragment:
>
> var x=foo.
> if (a>b)  -bar;
>
> If this showed up in real code, it would probably be either as a typo where either the actual property name for foo was left off or because of a mental lapse by an old Smalltalk programmer who was used to using . instead of ; to terminate statements. Regardless, it will validly parse identically to the following single line formulation:
>
> var x=foo.if(a>b)-bar;
>
> The ES5 grammar for a dotted property access is:
>
> CallExpression ::
>    CallExpression . IdentifierName
>
> Where IdentifierName includes all possible identifiers including reserved words.  The above confusion could have been avoided if we had instead defined things as:
>
> CallExpression ::
>    CallExpression . Identifier
>    CallExpression . [no LineTerminator here] ReservedWord
>
> Is this the grammar we should have used for ES5?  It's too late to make this change in the ES5 spec.  but if we had consensus that this is how it should be specified in Harmony we might also get the emerging ES5 implementations to also follow this interpretations.
>
> Thoughts?  It's kind of hooky to have semi-arbitrary restrictions on line terminator placement.  But we already have them in order to make semi-colon insertion more sane and arguably such restrictions reasonably go hand-in-hand with allowing context sensitive use of reserved words.
>
> Allen
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Reserved word property access and line breaks

Peter van der Zee
The impact seems minimal (how often would this occur in the wild). I don't like
adding restrictions to the language for the sake of protecting a possible
defect, but I don't really see this as a problem since it's not exactly a best
practice to use reserved keywords as property names anyways ;) (re: CSFLD)

- peter

> It does seem reasonable to disallow new lines in that context.
>
> --Oliver
>
> On Aug 24, 2010, at 1:53 PM, Allen Wirfs-Brock wrote:
>
>> Consider the following JavaScript code fragment:
>>
>> var x=foo.
>> if (a>b)  -bar;
>>
>> If this showed up in real code, it would probably be either as a
>> typo where either the actual property name for foo was left off
>> or because of a mental lapse by an old Smalltalk programmer who
>> was used to using . instead of ; to terminate statements.
>> Regardless, it will validly parse identically to the following
>> single line formulation:
>>
>> var x=foo.if(a>b)-bar;
>>
>> The ES5 grammar for a dotted property access is:
>>
>> CallExpression ::
>> CallExpression . IdentifierName
>>
>> Where IdentifierName includes all possible identifiers including
>> reserved words.  The above confusion could have been avoided if
>> we had instead defined things as:
>>
>> CallExpression ::
>> CallExpression . Identifier
>> CallExpression . [no LineTerminator here] ReservedWord
>>
>> Is this the grammar we should have used for ES5?  It's too late
>> to make this change in the ES5 spec.  but if we had consensus
>> that this is how it should be specified in Harmony we might also
>> get the emerging ES5 implementations to also follow this
>> interpretations.
>>
>> Thoughts?  It's kind of hooky to have semi-arbitrary restrictions
>> on line terminator placement.  But we already have them in order
>> to make semi-colon insertion more sane and arguably such
>> restrictions reasonably go hand-in-hand with allowing context
>> sensitive use of reserved words.
>>
>> Allen
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: Reserved word property access and line breaks

Douglas Crockford
  On 11:59 AM, Peter van der Zee wrote:
> I don't like
> adding restrictions to the language for the sake of protecting a possible
> defect, but I don't really see this as a problem since it's not exactly a best
> practice to use reserved keywords as property names anyways ;) (re: CSFLD)
I think that is one of the best possible reasons for modifying a language.

For what it's worth, JSLint does not tolerate any whitespace between .
and a property name.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Reserved word property access and line breaks

Jeff Walden-6
On 08/29/2010 03:28 PM, Douglas Crockford wrote:
> For what it's worth, JSLint does not tolerate any whitespace between . and a property name.

Tangential, but does JSLint then require either long sequences of property accesses and method calls to occur all on a single line or to always be broken with "." and subsequent property name starting new lines?  For example, if this example must include a line break, at that dot-location, is this the only way JSLint would permit it to be broken?

var b;
var a = b.c.d.e.f.g().h().i()
      .j().fdsfadsfdasfasdafds().basd();

(I actually prefer this to ending the line with ".", but the argument for dot-at-end seems reasonable enough to me that I'm surprised JSLint would mandate the other way.)

Jeff
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss