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

String Key for Object Property #126

Open
eeasley opened this issue Jul 15, 2013 · 4 comments
Open

String Key for Object Property #126

eeasley opened this issue Jul 15, 2013 · 4 comments

Comments

@eeasley
Copy link

eeasley commented Jul 15, 2013

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
Copy link
Owner

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
Copy link
Author

eeasley commented Jul 16, 2013

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
Copy link
Owner

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
Copy link
Author

eeasley commented Jul 16, 2013

Okay. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants