sourcegraph / tree-sitter-jsonnet

tree-sitter grammar for JSONNET
MIT License
16 stars 6 forks source link

[...] as key in a map rendered as ERROR #13

Closed folliehiyuki closed 1 year ago

folliehiyuki commented 1 year ago

A valid jsonnet file (shorten from https://jsonnet.org/learning/tutorial.html#parameterize-entire-config):

local prefix = 'Happy Hour ';
{
  [prefix + 'Mimosa']: 'Champagne',
}

The json output (using go-jsonnet v0.20.0):

{
   "Happy Hour Mimosa": "Champagne"
}

Here is the tree nodes, taken from Neovim's vim.treesitter.inspect_tree():

(expr) ; [1:1 - 4:1]
 (local_bind) ; [1:1 - 4:1]
  (local) ; [1:1 - 5]
  (bind) ; [1:7 - 28]
   (id) ; [1:7 - 12]
   (expr) ; [1:16 - 28]
    (string) ; [1:16 - 28]
     (string_start) ; [1:16 - 16]
     (string_content) ; [1:17 - 27]
     (string_end) ; [1:28 - 28]
  (expr) ; [2:1 - 4:1]
   (ERROR) ; [3:3 - 35]
    (expr) ; [3:4 - 20]
     left: (expr) ; [3:4 - 9]
      (id) ; [3:4 - 9]
     right: (expr) ; [3:13 - 20]
      (string) ; [3:13 - 20]
       (string_start) ; [3:13 - 13]
       (string_content) ; [3:14 - 19]
       (string_end) ; [3:20 - 20]
    (expr) ; [3:24 - 34]
     (string) ; [3:24 - 34]
      (string_start) ; [3:24 - 24]
      (string_content) ; [3:25 - 33]
      (string_end) ; [3:34 - 34]
folliehiyuki commented 1 year ago

Strange enough the line [if salted then 'garnish']: 'Salt', renders fine in the tree:

local Margarita(salted) = {
  ingredients: [
    { kind: 'Tequila Blanco', qty: 2 },
  ],
  [if salted then 'garnish']: 'Salt',
};
{
  Margarita: Margarita(true),
  'Margarita Unsalted': Margarita(false),
}

The inspected node tree:

(expr) ; [1:1 - 10:1]
 (local_bind) ; [1:1 - 10:1]
  (local) ; [1:1 - 5]
  (bind) ; [1:7 - 6:1]
   function: (id) ; [1:7 - 15]
   params: (params) ; [1:17 - 22]
    (param) ; [1:17 - 22]
     identifier: (id) ; [1:17 - 22]
   body: (expr) ; [1:27 - 6:1]
    (member) ; [2:3 - 4:3]
     (field) ; [2:3 - 4:3]
      (fieldname) ; [2:3 - 13]
       (id) ; [2:3 - 13]
      (expr) ; [2:16 - 4:3]
       (expr) ; [3:5 - 38]
        (member) ; [3:7 - 28]
         (field) ; [3:7 - 28]
          (fieldname) ; [3:7 - 10]
           (id) ; [3:7 - 10]
          (expr) ; [3:13 - 28]
           (string) ; [3:13 - 28]
            (string_start) ; [3:13 - 13]
            (string_content) ; [3:14 - 27]
            (string_end) ; [3:28 - 28]
        (member) ; [3:31 - 36]
         (field) ; [3:31 - 36]
          (fieldname) ; [3:31 - 33]
           (id) ; [3:31 - 33]
          (expr) ; [3:36 - 36]
           (number) ; [3:36 - 36]
    (member) ; [5:3 - 36]
     (field) ; [5:3 - 36]
      (fieldname) ; [5:3 - 28]
       (expr) ; [5:4 - 27]
        condition: (expr) ; [5:7 - 12]
         (id) ; [5:7 - 12]
        consequence: (expr) ; [5:19 - 27]
         (string) ; [5:19 - 27]
          (string_start) ; [5:19 - 19]
          (string_content) ; [5:20 - 26]
          (string_end) ; [5:27 - 27]
      (expr) ; [5:31 - 36]
       (string) ; [5:31 - 36]
        (string_start) ; [5:31 - 31]
        (string_content) ; [5:32 - 35]
        (string_end) ; [5:36 - 36]
  (expr) ; [7:1 - 10:1]
   (member) ; [8:3 - 28]
    (field) ; [8:3 - 28]
     (fieldname) ; [8:3 - 11]
      (id) ; [8:3 - 11]
     (expr) ; [8:14 - 28]
      (expr) ; [8:14 - 22]
       (id) ; [8:14 - 22]
      (args) ; [8:24 - 27]
       (expr) ; [8:24 - 27]
        (true) ; [8:24 - 27]
   (member) ; [9:3 - 40]
    (field) ; [9:3 - 40]
     (fieldname) ; [9:3 - 22]
      (string) ; [9:3 - 22]
       (string_start) ; [9:3 - 3]
       (string_content) ; [9:4 - 21]
       (string_end) ; [9:22 - 22]
     (expr) ; [9:25 - 40]
      (expr) ; [9:25 - 33]
       (id) ; [9:25 - 33]
      (args) ; [9:35 - 39]
       (expr) ; [9:35 - 39]
        (false) ; [9:35 - 39]

So I assume it's only detected as ERROR when it is the first key in the map object.

Duologic commented 1 year ago

Looks like I'm handling this in #15, let me add your examples as tests.

Duologic commented 1 year ago

Test cases pass. :tada:

folliehiyuki commented 1 year ago

Thanks for spending the time fixing my issue!

I think in the 2nd example you should put [if salted then 'garnish'] as the 1st key of the object, since in the current state it only fails to render in that situation.