nushell / nushell/tree-sitter-nu
Help with finding ERRORs in tree-sitter-nu.
Nobody has claimed this yet.
- Dominant language
- Nushell
- Stars
- 180
- Forks
- 38
- Avg merge
- 12m
- Merged PRs (30d)
- 1
Description
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:
- Add the script below to your
$env.NU_LIB_DIRSdirectory - by default it is a folder called scripts in your configuration directory. - Import the script via
use tree-sitter-get-errors.nu - 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.nuCan take any number of files by nametree-sitter-get-errors --glob *nuCan accept globcommand_that_returns_paths | tree-sitter-get-errorsCan take stdin- Or any combination from the listed examples.
- You can pipe the output into
columnsto get a list of all files names with an error in them. (Optional) - 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-errorsscript, 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
- tree-sitter-get-errors.nu:
# Test files for tree-sitter errors.
#
# Prints a table with the filename where an error has occured.
# The list is the line numbers of each error in the format of:
# "$STARTING_ERROR_LINE $FINAL_ERROR_LINE"
#
# **Line numbers are offset by -1**: Treesitter parse lines starting from line 0.
#
# The stdin can also be used to accept paths (they are appended to the files).
export def main [
...files: path # Files to parse for error.
--glob (-g): string # Provide a glob to add to the files.
--threads (-t): int # Number of threads to use. Defaults at all.
--no-reduce (-R) # Instead of printing in a table, provide the result in table of lists.
]: [list<path> -> table, list<path> -> list] {
# Parameter assingment with globs & stdin.
let stdin = ($in | default [])
let files = ($files ++ $stdin) | path expand |
append (if $glob != null {glob ($glob | default "")}) | uniq
# Setting threads.
let threads = $threads | default 0
# Output
if $files == [] {return ("Run command with --help to see usage.")}
$files | par-each --threads $threads {|it| _tree-sitter-get-errors-single $it} |
where {|it| ($it | describe) != list<any>} |
if $no_reduce {$in} else {$in | reduce {|it, acc| $acc | merge $it}}
}
# 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})
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the proposed tree-sitter-get-errors.nu script and its tree-sitter parse command. Determine whether this repository should include or document the script, since the issue currently describes an external workflow rather than naming a repository change. Done should be a decided, actionable scope for improving error discovery.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- shell
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100