google/CodeCity

$.utils.code.toSource is unnecessarily conservative about quoting property names

cpcallen opened this issue · 3 comments

$.utils.code.toSource({delete: 'foo'}) returns {'delete': "foo"}, but the single quotes around "delete" are not needed because although delete is a reserved word it is being used here as a property rather than variable name.

Similarly, $.utils.code.toSource({4: 4}) returns {'4': 4} when {4: 4} is legal.

The $.utils.code.quote function is doing this but it perhaps needs to be forked into different versions for different contexts. (Note that you can also say object.delete but not object.4.)

Whatever changes are made here should be back to server/code.js.

Reference:

See https://262.ecma-international.org/5.1/#sec-11.1.5 (ES5.1) and/or https://tc39.es/ecma262/#sec-object-initializer (ES20xx) for the syntax of an ObjectInitializer; note that PropertyName (ES5.1) / LiteralPropertyName (ES20xx) can be any of IdentifierName, StringLiteral or NumericLiteral, and that IdentifierName is not the same as Identifier (which does exclude ReservedWord).

No: I believe $.utils.code.quote is fine. The problem is that it is being called in certain cases where it does not need to be.

There is not presently any code in code.js that has this issue.

$.utils.code.isIdentifierName has been split out of $.utils.code.isIdentifier, and .toSource updated appropriately.

Unquoted numeric literals are not yet generated.