Anyone have a successful lint setup for js/ts?

this will create file without name clash at the same location: file=$(mktemp "$kak_buffile.XXXXXXXXX"), and the name will be stored in file, you then can format, pipe back, and delete it in single shell call.

I made it work without writing a temp file.

The eslint formatter json includes the fixed file in its output if --fix-dry-run is set. I then use jq to parse the JSON output:

define-command -hidden eslint-format %{
    evaluate-commands -draft -no-hooks -save-regs '|' %{
        # Select all to format
        execute-keys '%'

        # eslint does a fix-dry-run with a json formatter which results in a JSON output to stdout that includes the fixed file.
        # jq then extracts the fixed file output from the JSON. -j returns the raw output without any escaping.
        set-register '|' %{
            %sh{
                echo "$kak_selection" | \
                npx eslint --format json \
                           --fix-dry-run \
                           --stdin \
                           --stdin-filename "$kak_buffile" | \
                jq -j ".[].output"
            }
        }

        # Replace all with content from register:
        execute-keys '|<ret>'
    }
}

How can I restore the cursor position after formatting (a simple mark? somehow execute-keys 'Z' leaves me with an empty ^ so that execute-keys z afterwards returns an error)

I added this as an alternative to the Wiki: https://github.com/mawww/kakoune/wiki/Lint#javascript-eslint

1 Like

I think you can make it into a package

It is done, a successful lint/format setup for js/ts:

@jssee do you still need this? :wink:

I want to add it to the Wiki, but I am not sure where. Under Languages? The currently linked TypeScript repo doesn’t work with the latest Kakoune anyways, I could replace it. Kakoune has built-in support for TypeScript syntax highlighting by now anyways, if I see that correctly.

By the way, I have on issue that I cannot fix. The lint command requires the path to the globally installed node_modules. I cannot set it as an option with $kak_opt_... as it seems that won’t be available due to how lint.kak is implemented. Any way how that could be done better than asking users of the plugin to replace the whole lintcmd in case it isn’t the default path for them?

Maybe @robertmeta or @alexherbo2 have an idea? :thinking:

Anyways, I hope this will help others :blush:

I don’t think this should go to that wiki page, as it is about builtin language support (Unless you wish to contribute your work to Kakoune directly :slightly_smiling_face:) . I think you should add tags to the repo so it could be visible on plugin page at Kakoune - Plugins

Ok, it is done and working. npm list to the rescue. Unfortunately it is very slow, but now it should work on most systems and linting runs asynchronously anyways.

set window lintcmd 'run() { cat "$1" | npx eslint --format "$(npm list -g --depth=0 | head -1)/node_modules/eslint-formatter-kakoune/index.js" --stdin --stdin-filename "${kak_buffile}";} && run '

I am really happy with how this worked out in the end. Linting and formatting works very well now. Was not super straight-forward to set up :sweat_smile:

If anyone comes here looking for a working integration with eslint, please try the kak-jsts project I linked above :slight_smile:

I have done shockingly little to get my TS setup working well (linting, prettier, autocomplete, etc). Thanks to kak-lsp!

That sounds great. Did you integrate eslint with lsp-kak formatting and diagnostics?

This is great, thanks so much for putting it together