gritql icon indicating copy to clipboard operation
gritql copied to clipboard

Whitespaces are significant

Open xzfc opened this issue 10 months ago • 1 comments

Example

This pattern:

language js
`switch(a){ }`

matches only the second line:

switch(a){}     // 1: no match
switch(a){ }    // 2: match
switch(a){  }   // 3: no match

Which is highly inconvenient when doing multi-line migrations as rules need to have precisely the same indentations as the code.

Clue

As I can see, this happen for node types that capture their descendants via "children" rather than "fields". E.g. it's an issue for switch_body: https://github.com/getgrit/gritql/blob/5af841f57592f287bdd736a87ec4008c77d88603/resources/node-types/javascript-node-types.json#L3157-L3175

But it's not an issue for, e.g., an array: https://github.com/getgrit/gritql/blob/5af841f57592f287bdd736a87ec4008c77d88603/resources/node-types/javascript-node-types.json#L308-L331

Workaround?

In the meantime, what workaround/quick fixes available to run multi-line migrations? I need some guidance.

  • Fix grammar.js and *-node-types.json to use "fields"?
  • Rewrite my rules using ast nodes instead of snippets?
  • Somehow fix spacing for "children"?

xzfc avatar Jan 30 '25 12:01 xzfc

The easiest solution is changing grammar.js but ideally we would fix spacing for children as well.

morgante avatar Jan 31 '25 07:01 morgante