How to discern nodes based on their order? #675
-
I've been playing around with using ast-grep for Go's structured logging library slog to tame a growing zoo of different formats in log field names. In addition to predeclaring fields, it also supports variadically adding log fields by invoking The following Playground link demonstrates how to fix the first key and preserve the latter arguments. I'd be happy already if it would fix the last non-conforming key (second to last element in the Even better would be the option to fix all keys in one go, without iterating executions of ast-grep to get the last non-conforming key, but as I said, I'd be happy with the first already. I've read about On a similar note: The amount of args should always be odd 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @debugloop Thanks for you valuable feedback! I appreciate all your questions and reporting and they gave me many insights on how users are using ast-grep. For the use case in this post, I think it may be impossible to fix multiple nodes or nodes. At the moment, ast-grep fixing can only fix one target ast node at a time. And telling node's order is not possible at the moment either. :( I didn't support them because it may be too complex for the rule YAML. I can try using YAML rule to implement this change. But I would recommend to use programmatic API. Sadly, currently python API is still under development. #672 NodeJS API has not supported the Python language yet. |
Beta Was this translation helpful? Give feedback.
-
Since util rule is recursive, we can always roll out order-specific rules by hand. (alas, peano-number) In this case, we can define the key node as either
id: slog-kebab-dynamic-call
message: slog field does not use kebab-case
language: go
utils:
inside-slog:
pattern:
context: '{ slog.$FUNC($$$) }'
selector: argument_list
first-key:
not:
follows:
pattern: $_
stopBy:
pattern: $_
at-key:
kind: interpreted_string_literal
follows:
stopBy:
pattern: $_
pattern: $_
any:
- matches: first-key
- follows:
matches: at-key
stopBy:
pattern: $_
rule:
matches: at-key
pattern: $KEY
inside:
matches: inside-slog
constraints:
FUNC:
regex: '^(Debug|Info|Warn|Error)$'
KEY:
regex: '^".*?[A-Z._ /].*?"$'
transform:
BARE_KEY:
substring:
source: $KEY
startChar: 1
endChar: -1
KEBAB_KEY:
convert:
source: $BARE_KEY
toCase: kebabCase
fix: '"$KEBAB_KEY"'
I would think about how to express the "quantity/order" concept in YAML rule. But currently this is a workaround. |
Beta Was this translation helpful? Give feedback.
I forgot to update the selector field! I changed it to other things.
https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImdvIiwicXVlcnkiOiIoJCQkKSIsInJld3JpdGUiOiIiLCJjb25maWciOiJpZDogc2xvZy1rZWJhYi1keW5hbWljLWNhbGxcbm1lc3NhZ2U6IHNsb2cgZmllbGQgZG9lcyBub3QgdXNlIGtlYmFiLWNhc2Vcbmxhbmd1YWdlOiBnb1xudXRpbHM6XG4gIGluc2lkZS1zbG9nOlxuICAgIHBhdHRlcm46XG4gICAgICBjb250ZXh0OiAneyBzbG9nLiRGVU5DKCQkJCkgfSdcbiAgICAgIHNlbGVjdG9yOiBjYWxsX2V4cHJlc3Npb25cbiAgZmlyc3Qta2V5OlxuICAgIG5vdDpcbiAgICAgIGZvbGxvd3M6XG4gICAgICAgIHBhdHRlcm46ICRfXG4gICAgICAgIHN0b3BCeTpcbiAgICAgICAgICBwYXR0ZXJuOiAkX1xuICBhdC1rZXk6XG4gICAga2luZDogaW50ZXJwcmV0ZWRfc3RyaW5nX2xpdGVyYWxcbiAgICBmb2xsb3dzOlxuICAgICAgc3Rvc…