vim-autoformat icon indicating copy to clipboard operation
vim-autoformat copied to clipboard

Can't get 'standard_javascript'-formatter to work.

Open jottr opened this issue 8 years ago • 6 comments

Note the config for autoformat in my .vimrc:

function! InstallFormatter(info)
  if a:info.status == 'installed' || a:info.force
    " Node
    :silent ! npm update -g js-beautify
    :silent ! npm update -g remark-cli
    :silent ! npm update -g standard
    " Python
    :silent ! pip install --user yapf
  endif
endfunction

Plug 'Chiel92/vim-autoformat', { 'do': function('InstallFormatter') }

let g:formatters_javascript = ['standard_javascript']

I'm not sure why I get a "Failing config"-error:

Using python 3 code...
Formatter standard_javascript has errors: b"standard: Use JavaScript Standard Style (https://standardjs.com)\nstandard:   <text>:18:10:
 'translate' is defined but never used.\n" Skipping.
Failing config: 'standard --fix --stdin'
Definition in 'g:formatdef_standard_javascript' was unsuccessful.
No format definitions were successful.
Removing trailing whitespace...
Retabbing...
Autoindenting...

After running :Autoformat the indentation is completely messed up, since standard indentation rules are not applied.

Interestingly the indentation is only messed up for the translate() function definition (see the log above). I am not seeing this issue with other javascript files.

$ vim --version 
NVIM 0.1.7
Build type: RelWithDebInfo
Compilation: /usr/local/Homebrew/Library/Homebrew/shims/super/clang -Wconversion -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g -DDISABLE_LOG -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wvla -fstack-protector-strong -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -DHAVE_CONFIG_H -I/tmp/neovim-20170423-57969-zlzig1/neovim-0.1.7/build/config -I/tmp/neovim-20170423-57969-zlzig1/neovim-0.1.7/src -I/usr/local/include -I/usr/local/include -I/usr/local/include -I/usr/local/include -I/usr/local/include -I/usr/local/include -I/usr/local/opt/gettext/include -I/usr/include -I/usr/include -I/tmp/neovim-20170423-57969-zlzig1/neovim-0.1.7/build/src/nvim/auto -I/tmp/neovim-20170423-57969-zlzig1/neovim-0.1.7/build/include
Übersetzt von [email protected]

Optional features included (+) or not (-): +acl   +iconv    +jemalloc +tui      
For differences from Vim, see :help vim-differences

     System-vimrc-Datei: "$VIM/sysinit.vim"
     Voreinstellung für $VIM: "/usr/local/Cellar/neovim/0.1.7/share/nvim"


jottr avatar Apr 30 '17 13:04 jottr

At first sight it seems to me that standard writes its messages to stderr, which make vim-autoformat think that the formatter has failed.

Is there a flag that can be passed to standard to adjust this behaviour?

chtenb avatar May 01 '17 10:05 chtenb

Given the following file:

# ./jstest.js
function foo() {
  console.log ('bar')
}
$ standard jstest.js
standard: Use JavaScript Standard Style (https://standardjs.com)
standard: Run `standard --fix` to automatically fix some problems.
  /private/tmp/jstest.js:1:10: 'foo' is defined but never used.
  /private/tmp/jstest.js:1:13: Missing space before function parentheses.
  /private/tmp/jstest.js:2:11: Unexpected space between function name and paren.

standard doesn't have a flag to set the standard stream, i.e. stderr or stdout. Testing where standard writes its messages: stderr:

$ standard jstest.js 2>/dev/null
  /private/tmp/jstest.js:1:10: 'foo' is defined but never used.
  /private/tmp/jstest.js:1:13: Missing space before function parentheses.
  /private/tmp/jstest.js:2:11: Unexpected space between function name and paren.

stdout:

$ standard jstest.js 1>/dev/null                                                                                                
standard: Use JavaScript Standard Style (https://standardjs.com)
standard: Run `standard --fix` to automatically fix some problems.

In vim with :Autoformat:

Using python 3 code...
Formatter standard_javascript has errors: b"standard: Use JavaScript Standard Style (https://standardjs.com)\nstandard:   <text>:1:10: 'foo' is defined but never used.\n" Skipping.
Failing config: 'standard --fix --stdin'
Definition in 'g:formatdef_standard_javascript' was unsuccessful.
No format definitions were successful.
Removing trailing whitespace...

Not sure configuring standard like so would be a good idea:

let g:formatdef_custom_standard = '"standard --fix --stdin 2>/dev/null"'

Thoughts?

jottr avatar May 01 '17 13:05 jottr

@bbqtd Has this never been an issue for you?

chtenb avatar May 02 '17 06:05 chtenb

It's probably because it's not installed globally, and the only one supporting a local lookup right now is eslint, so xo and standard don't get that same love.

See https://github.com/Chiel92/vim-autoformat/blob/e80935baff4221c34af0c9f200f9b48404dacdfa/plugin/defaults.vim#L165-L206

niftylettuce avatar Aug 14 '17 07:08 niftylettuce

I stumbled onto this issue. Did you find a work around?

let g:formatdef_custom_standard = '"standard --fix --stdin 2>/dev/null"'

Don't work because standard return code isn't 1.

kuon avatar Apr 28 '20 22:04 kuon