jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.65k stars 1.58k forks source link

mistaken use of kebab case could use a better error message #3200

Open felix9 opened 2 weeks ago

felix9 commented 2 weeks ago

If json keys are kebab case ("last-name"), it's natural to try something like

jq '.last-name'

which emits a confusing error message:

jq: error: name/0 is not defined at <top-level>, line 1:

this seems like a common enough problem that it might be worth making a special error message for expressions that look like that.

(alternatively, the jq language could support kebab-case literals. this might break some scripts, but it seems unlikely that anyone would deliberately write an expression that looks like kebab-case)

itchyny commented 2 weeks ago

Looking for the function is done in the compilation phase, and it is difficult (or making the code messy) to tell that the function is the right hand side of a binary expression which has indexing in the left hand side and .... Also can we know the difference between .last - name and .last-name in the compilation phase?

felix9 commented 2 weeks ago

it looks to me like it wouldn't be too hard to add a heuristic check at the point of the error message. check if the source expression looks like unquoted skewer case, and suggest that might be the problem. I might have time/energy to write a POC patch this weekend

wader commented 2 weeks ago

For context me and @felix9 talked a bit about this on mastodon https://fosstodon.org/@dkub@woof.group/113455083464066624 specially that there is two cases, a compile time and runtime time case, and probably the compile time case is more common and might be an easier case for heuristics?