ckknight / gorillascript

GorillaScript is a compile-to-JavaScript language designed to empower the user while attempting to prevent some common errors.
MIT License
300 stars 34 forks source link

String Key for Object Property #126

Open eeasley opened 11 years ago

eeasley commented 11 years ago

As far as I can tell, there's no convenient way to ensure that gorillascript's output javascript keys a property via string.

let foo = {\bar: \bar, baz: \baz, 'fred': 'fred', 'plugh thud': 'plugh thud'}
foo[\qux] := \qux
foo.quux := \quux
foo['corge'] := 'corge'
foo['grault garply'] := 'grault garply'

transpiles to

var foo;
foo = { bar: "bar", baz: "baz", fred: "fred", "plugh thud": "plugh thud" };
foo.qux = "qux";
foo.quux = "quux";
foo.corge = "corge";
foo["grault garply"] = "grault garply";

I would think that 'fred' and 'corge' at least should retain string keys (i.e. transpile to (... ,"fred": "fred", ...) and (foo["corge"] = "corge";)) for the sake of consistency with 'plugh thud' and 'grault garply'.

At least one instance where this matters is when compiling with the Google Closure Compiler. It treats string keys differently from dotted access. It will freely rename dotted access (e.g. foo.bar to x.y) but keeps string keys intact (e.g. foo["bar"] to x["bar"]).

ckknight commented 11 years ago

GorillaScript currently doesn't differentiate the two types of access, they're seen as equivalent and thus compile to the smallest possible representation.

I'm not sure if I want to introduce the extra complexity at this time for something that won't work in all cases. If you use a property, there's no way of converting { property x: { value: 0 } } to not use a string, and in cases where you use a reserved word like class, they're required to be quoted, so in the cases where you try to access those as dots, they'll have to convert.

This seems like a downside of the Closure Compiler. Surely it would be able to know that constant string access is equivalent to dot access.

eeasley commented 11 years ago

I'm not sure I understand the middle paragraph of your comment.

I think I'm asking for more transpiled output to be quoted. So things that are already quoted would stay quoted. But I'm suggesting/requesting that also properties (I'm just talking about simple gs/js object properties, not the special gorillascript property syntax) that are keyed with single quoted bracket notation in gorillascipt (e.g. foo['bar']) be keyed by quoted bracket notation in the output javascript (e.g. foo["bar"] rather than the current foo.bar).

Is your middle paragraph relevant in that scenario? Or am I hopelessly confused?

ckknight commented 11 years ago

It is relevant because there's no way to make sure that every non-quoted part stays non-quoted nor should one expect that every quoted part remain quoted.

Adding such functionality would require an overhaul of the internal access call to specify whether or not its child was quoted, and also require the JS compiler to have the same functionality, which I just don't see being worthwhile at this stage due to the issues I laid out.

eeasley commented 11 years ago

Okay. Thanks.