google / google/jsonnet

Improvements to syntax for defining multiple locals

Open
#551 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Jsonnet
Stars
7.6k
Forks
475
PR merge metrics
No merged PRs in 30d

Description

Currently, if I wish to define a large list of locals (eg. "importing libraries" at the top of a file), I have two reasonable ways to format it:
```
local foo = import "foo.libsonnet";
local bar = import "bar.libsonnet";
local baz = import "baz.libsonnet";
```
```
local
foo = import "foo.libsonnet",
bar = import "bar.libsonnet",
baz = import "baz.libsonnet";
```

The first is annoying due to the repetition, it would be nice to be able to define many locals without saying "local" every time and the syntax `local x = 1, y = 2;` solves this problem nicely for single-line statements.

The second is annoying when spaced over many lines. You either need to remember to end the last line with a `;` instead of a `,` as above, or you can do a quasi-"closing brace"-style thing like so:
```
local
foo = import "foo.libsonnet",
bar = import "bar.libsonnet",
baz = import "baz.libsonnet"
;
```
However, now you need to remember not to include a trailing comma on the final line.

I see two fixes to this problem that I hope should be relatively easy:

1. Allow a redundant `,` before the `;`, ie. so that an expression like `local x = 1, y = 2,;` is valid. This allows a "trailing comma" in the quasi-closing-brace-style shown above, which will go a long way to making this not a pain when changing or re-ordering the list. This is the smallest change.

2. Allow a multiple-definition syntax like `local (x = 1, y = 2);`, with optional trailing commas. This would make my example:
```
local (
foo = import "foo.libsonnet",
bar = import "bar.libsonnet",
baz = import "baz.libsonnet",
);
```
This is the best option in my opinion as the parentheses indicate immediately to the user that the definitions form a "block" of things which the local keyword applies to, and it has a very visible end while allowing re-ordering or modification of the list of definitions without needing to worry about treating any one specially.

It is also reminiscent of the python syntax:
```py
from foo import (
bar,
baz,
)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.