nushell / tree-sitter-nu

A tree-sitter grammar for nu-lang, the language of nushell
MIT License
96 stars 24 forks source link

Help with finding ERRORs in tree-sitter-nu. #91

Open CabalCrow opened 3 months ago

CabalCrow commented 3 months ago

Related Problem

Because tree-sitter-nu is still WIP there are various errors still in it. The code could still be highlighted even when errors occur (except for the snippest of code affected by the error), so they might go unnoticed.

To deal with this it can be helpful for people to test their nushell scripts via tree-sitter-cli. This can be done manually (running tree-sitter parse on every file and checking for errors there) or through I've script (tree-sitter-get-errors.nu) that I've written that I would post below.

Steps for helping out:

  1. Add the script below to your $env.NU_LIB_DIRS directory - by default it is a folder called scripts in your configuration directory.
  2. Import the script via use tree-sitter-get-errors.nu
  3. You can then call tree-sitter-get-errors to find TS errors in your nushell files. Example usage:
    • tree-sitter-get-errors file1.nu file2.nu Can take any number of files by name
    • tree-sitter-get-errors --glob *nu Can accept glob
    • command_that_returns_paths | tree-sitter-get-errors Can take stdin
    • Or any combination from the listed examples.
  4. You can pipe the output into columns to get a list of all files names with an error in them. (Optional)
  5. Make an issue providing either the full text, or preferably a minimal snippet of the code that still provides an error. You can use the table output from the tree-sitter-get-errors script, to view a list of all error line ranges (LINES START AT 0) in each file to find the code snippet that creates the TS error.

Related script

Parse file for TS errors.

#

Returns table with one column being the file name.

The list elements are the line number ranges of the error.

export def _tree-sitter-get-errors-single [file: path] { tree-sitter parse $file | split row "\n" | where {|it| $it =~ '^\s++(ERROR'} | str replace -r '^\s++(ERROR [(\d++)[^[]++[(\d++).*+$' '$1 $2' |

Return a table with file name - relative if possible.

    wrap (try {$file | path relative-to (pwd)} catch {$file})

}

CabalCrow commented 3 months ago

Fix issue with the script not working in certian cases & add an additional flag for alternative output (-R it is better when a lot of files have errors).