From 9017d3ef9ce06a3662f6c87becffa7d7143fb571 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Fri, 26 Jul 2019 11:52:34 -0400 Subject: [PATCH 1/4] Add StandardJS for TypeScript linting and fixing. --- ale_linters/typescript/standard.vim | 30 +++++++++++++ autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/standardts.vim | 24 +++++++++++ doc/ale-typescript.txt | 27 ++++++++++++ doc/ale.txt | 1 + supported-tools.md | 1 + .../test_standardts_command_callback.vader | 43 +++++++++++++++++++ .../test_standardts_fixer_callback.vader | 31 +++++++++++++ 8 files changed, 162 insertions(+) create mode 100644 ale_linters/typescript/standard.vim create mode 100644 autoload/ale/fixers/standardts.vim create mode 100644 test/command_callback/test_standardts_command_callback.vader create mode 100644 test/fixers/test_standardts_fixer_callback.vader diff --git a/ale_linters/typescript/standard.vim b/ale_linters/typescript/standard.vim new file mode 100644 index 00000000..184d9727 --- /dev/null +++ b/ale_linters/typescript/standard.vim @@ -0,0 +1,30 @@ +" Author: Ahmed El Gabri <@ahmedelgabri> +" Description: standardjs for typescript files + +call ale#Set('typescript_standard_executable', 'standard') +call ale#Set('typescript_standard_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('typescript_standard_options', '') + +function! ale_linters#typescript#standard#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'typescript_standard', [ + \ 'node_modules/standard/bin/cmd.js', + \ 'node_modules/.bin/standard', + \]) +endfunction + +function! ale_linters#typescript#standard#GetCommand(buffer) abort + let l:executable = ale_linters#typescript#standard#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'typescript_standard_options') + + return ale#node#Executable(a:buffer, l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' --stdin %s' +endfunction + +" standard uses eslint and the output format is the same +call ale#linter#Define('typescript', { +\ 'name': 'standard', +\ 'executable': function('ale_linters#typescript#standard#GetExecutable'), +\ 'command': function('ale_linters#typescript#standard#GetCommand'), +\ 'callback': 'ale#handlers#eslint#Handle', +\}) diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 2a96a7d6..4f795dba 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -135,6 +135,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['ruby'], \ 'description': 'Fix ruby files with standardrb --fix', \ }, +\ 'standardts': { +\ 'function': 'ale#fixers#standardts#Fix', +\ 'suggested_filetypes': ['typescript'], +\ 'description': 'Fix TypeScript files using standard --fix', +\ }, \ 'stylelint': { \ 'function': 'ale#fixers#stylelint#Fix', \ 'suggested_filetypes': ['css', 'sass', 'scss', 'sugarss', 'stylus'], diff --git a/autoload/ale/fixers/standardts.vim b/autoload/ale/fixers/standardts.vim new file mode 100644 index 00000000..5ab8771d --- /dev/null +++ b/autoload/ale/fixers/standardts.vim @@ -0,0 +1,24 @@ +" Description: Fixing files with Standard for typescript. + +call ale#Set('typescript_standard_executable', 'standard') +call ale#Set('typescript_standard_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('typescript_standard_options', '') + +function! ale#fixers#standardts#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'typescript_standard', [ + \ 'node_modules/standard/bin/cmd.js', + \ 'node_modules/.bin/standard', + \]) +endfunction + +function! ale#fixers#standardts#Fix(buffer) abort + let l:executable = ale#fixers#standardts#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'typescript_standard_options') + + return { + \ 'command': ale#node#Executable(a:buffer, l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' --fix %t', + \ 'read_temporary_file': 1, + \} +endfunction diff --git a/doc/ale-typescript.txt b/doc/ale-typescript.txt index 7dc59820..2c50d119 100644 --- a/doc/ale-typescript.txt +++ b/doc/ale-typescript.txt @@ -16,6 +16,33 @@ prettier *ale-typescript-prettier* See |ale-javascript-prettier| for information about the available options. +=============================================================================== +standard *ale-typescript-standard* + +g:ale_typescript_standard_executable *g:ale_typescript_standard_executable* + *b:ale_typescript_standard_executable* + Type: |String| + Default: `'standard'` + + See |ale-integrations-local-executables| + + +g:ale_typescript_standard_options *g:ale_typescript_standard_options* + *b:ale_typescript_standard_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to standard. + + +g:ale_typescript_standard_use_global *g:ale_typescript_standard_use_global* + *b:ale_typescript_standard_use_global* + Type: |Number| + Default: `get(g:, 'ale_use_global_executables', 0)` + + See |ale-integrations-local-executables| + + =============================================================================== tslint *ale-typescript-tslint* diff --git a/doc/ale.txt b/doc/ale.txt index 291e90fb..8b4aabfb 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2522,6 +2522,7 @@ documented in additional help files. typescript..............................|ale-typescript-options| eslint................................|ale-typescript-eslint| prettier..............................|ale-typescript-prettier| + standard..............................|ale-typescript-standard| tslint................................|ale-typescript-tslint| tsserver..............................|ale-typescript-tsserver| vala....................................|ale-vala-options| diff --git a/supported-tools.md b/supported-tools.md index c302d38c..c52b84eb 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -475,6 +475,7 @@ formatting. * [eslint](http://eslint.org/) * [fecs](http://fecs.baidu.com/) * [prettier](https://github.com/prettier/prettier) + * [standard](http://standardjs.com/) * [tslint](https://github.com/palantir/tslint) * [tsserver](https://github.com/Microsoft/TypeScript/wiki/Standalone-Server-%28tsserver%29) * typecheck diff --git a/test/command_callback/test_standardts_command_callback.vader b/test/command_callback/test_standardts_command_callback.vader new file mode 100644 index 00000000..d769e712 --- /dev/null +++ b/test/command_callback/test_standardts_command_callback.vader @@ -0,0 +1,43 @@ +Before: + call ale#assert#SetUpLinterTest('typescript', 'standard') + call ale#test#SetFilename('testfile.js') + unlet! b:executable + +After: + call ale#assert#TearDownLinterTest() + +Execute(bin/cmd.js paths should be preferred): + call ale#test#SetFilename('standard-test-files/with-cmd/testfile.js') + + let b:executable = ale#path#Simplify( + \ g:dir + \ . '/standard-test-files/with-cmd/node_modules/standard/bin/cmd.js' + \) + + AssertLinter b:executable, + \ (has('win32') ? 'node.exe ' : '') + \ . ale#Escape(b:executable) + \ . ' --stdin %s' + +Execute(.bin directories should be used too): + call ale#test#SetFilename('standard-test-files/with-bin/testfile.js') + + let b:executable = ale#path#Simplify( + \ g:dir + \ . '/standard-test-files/with-bin/node_modules/.bin/standard' + \) + + AssertLinter b:executable, ale#Escape(b:executable) . ' --stdin %s' + +Execute(The global executable should be used otherwise): + AssertLinter 'standard', ale#Escape('standard') . ' --stdin %s' + +Execute(The global executable should be configurable): + let b:ale_typescript_standard_executable = 'foobar' + + AssertLinter 'foobar', ale#Escape('foobar') . ' --stdin %s' + +Execute(The options should be configurable): + let b:ale_typescript_standard_options = '--wat' + + AssertLinter 'standard', ale#Escape('standard') . ' --wat --stdin %s' diff --git a/test/fixers/test_standardts_fixer_callback.vader b/test/fixers/test_standardts_fixer_callback.vader new file mode 100644 index 00000000..092465f5 --- /dev/null +++ b/test/fixers/test_standardts_fixer_callback.vader @@ -0,0 +1,31 @@ +Before: + call ale#test#SetDirectory('/testplugin/test/fixers') + + unlet! b:ale_typescript_standard_executable + unlet! b:ale_typescript_standard_options + +After: + call ale#test#RestoreDirectory() + +Execute(The executable path should be correct): + call ale#test#SetFilename('../eslint-test-files/react-app/subdir/testfile.js') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': (has('win32') ? 'node.exe ' : '') + \ . ale#Escape(ale#path#Simplify(g:dir . '/../eslint-test-files/react-app/node_modules/standard/bin/cmd.js')) + \ . ' --fix %t', + \ }, + \ ale#fixers#standardts#Fix(bufnr('')) + +Execute(Custom options should be supported): + let b:ale_typescript_standard_use_global = 1 + let b:ale_typescript_standard_options = '--foo-bar' + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape('standard') . ' --foo-bar --fix %t', + \ }, + \ ale#fixers#standardts#Fix(bufnr('')) From 79e9ae45507aab97d8f40063cf07a4ea6e69b6f7 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Fri, 26 Jul 2019 12:37:50 -0400 Subject: [PATCH 2/4] Make sure README and docs are synced. --- doc/ale-supported-languages-and-tools.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index e2379b70..227b6461 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -466,6 +466,7 @@ Notes: * `eslint` * `fecs` * `prettier` + * `standard` * `tslint` * `tsserver` * `typecheck` From cf5120ba756b339ef78a285d46814fa13b04aafd Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Mon, 21 Oct 2019 20:23:33 -0400 Subject: [PATCH 3/4] Remove standardts fixer in favor of allowing standard.vim fixer to handle JavaScript or TypeScript options. --- autoload/ale/fix/registry.vim | 5 --- autoload/ale/fixers/standard.vim | 9 +++++- autoload/ale/fixers/standardts.vim | 24 -------------- .../test_standardts_fixer_callback.vader | 31 ------------------- 4 files changed, 8 insertions(+), 61 deletions(-) delete mode 100644 autoload/ale/fixers/standardts.vim delete mode 100644 test/fixers/test_standardts_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 4f795dba..2a96a7d6 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -135,11 +135,6 @@ let s:default_registry = { \ 'suggested_filetypes': ['ruby'], \ 'description': 'Fix ruby files with standardrb --fix', \ }, -\ 'standardts': { -\ 'function': 'ale#fixers#standardts#Fix', -\ 'suggested_filetypes': ['typescript'], -\ 'description': 'Fix TypeScript files using standard --fix', -\ }, \ 'stylelint': { \ 'function': 'ale#fixers#stylelint#Fix', \ 'suggested_filetypes': ['css', 'sass', 'scss', 'sugarss', 'stylus'], diff --git a/autoload/ale/fixers/standard.vim b/autoload/ale/fixers/standard.vim index 77712d40..731a4f46 100644 --- a/autoload/ale/fixers/standard.vim +++ b/autoload/ale/fixers/standard.vim @@ -14,7 +14,14 @@ endfunction function! ale#fixers#standard#Fix(buffer) abort let l:executable = ale#fixers#standard#GetExecutable(a:buffer) - let l:options = ale#Var(a:buffer, 'javascript_standard_options') + let l:filetype = getbufvar(a:buffer, '&filetype') + let l:options_type = 'javascript_standard_options' + + if l:filetype =~# 'typescript' + let l:options_type = 'typescript_standard_options' + endif + + let l:options = ale#Var(a:buffer, l:options_type) return { \ 'command': ale#node#Executable(a:buffer, l:executable) diff --git a/autoload/ale/fixers/standardts.vim b/autoload/ale/fixers/standardts.vim deleted file mode 100644 index 5ab8771d..00000000 --- a/autoload/ale/fixers/standardts.vim +++ /dev/null @@ -1,24 +0,0 @@ -" Description: Fixing files with Standard for typescript. - -call ale#Set('typescript_standard_executable', 'standard') -call ale#Set('typescript_standard_use_global', get(g:, 'ale_use_global_executables', 0)) -call ale#Set('typescript_standard_options', '') - -function! ale#fixers#standardts#GetExecutable(buffer) abort - return ale#node#FindExecutable(a:buffer, 'typescript_standard', [ - \ 'node_modules/standard/bin/cmd.js', - \ 'node_modules/.bin/standard', - \]) -endfunction - -function! ale#fixers#standardts#Fix(buffer) abort - let l:executable = ale#fixers#standardts#GetExecutable(a:buffer) - let l:options = ale#Var(a:buffer, 'typescript_standard_options') - - return { - \ 'command': ale#node#Executable(a:buffer, l:executable) - \ . (!empty(l:options) ? ' ' . l:options : '') - \ . ' --fix %t', - \ 'read_temporary_file': 1, - \} -endfunction diff --git a/test/fixers/test_standardts_fixer_callback.vader b/test/fixers/test_standardts_fixer_callback.vader deleted file mode 100644 index 092465f5..00000000 --- a/test/fixers/test_standardts_fixer_callback.vader +++ /dev/null @@ -1,31 +0,0 @@ -Before: - call ale#test#SetDirectory('/testplugin/test/fixers') - - unlet! b:ale_typescript_standard_executable - unlet! b:ale_typescript_standard_options - -After: - call ale#test#RestoreDirectory() - -Execute(The executable path should be correct): - call ale#test#SetFilename('../eslint-test-files/react-app/subdir/testfile.js') - - AssertEqual - \ { - \ 'read_temporary_file': 1, - \ 'command': (has('win32') ? 'node.exe ' : '') - \ . ale#Escape(ale#path#Simplify(g:dir . '/../eslint-test-files/react-app/node_modules/standard/bin/cmd.js')) - \ . ' --fix %t', - \ }, - \ ale#fixers#standardts#Fix(bufnr('')) - -Execute(Custom options should be supported): - let b:ale_typescript_standard_use_global = 1 - let b:ale_typescript_standard_options = '--foo-bar' - - AssertEqual - \ { - \ 'read_temporary_file': 1, - \ 'command': ale#Escape('standard') . ' --foo-bar --fix %t', - \ }, - \ ale#fixers#standardts#Fix(bufnr('')) From ea91209a6685faa0275dc5f8735836ced8b08e4b Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Mon, 21 Oct 2019 20:24:22 -0400 Subject: [PATCH 4/4] Allow the use of StandardX for StandardJS linting and fixing. See https://github.com/standard/standardx --- ale_linters/javascript/standard.vim | 1 + ale_linters/typescript/standard.vim | 1 + autoload/ale/fixers/standard.vim | 1 + 3 files changed, 3 insertions(+) diff --git a/ale_linters/javascript/standard.vim b/ale_linters/javascript/standard.vim index 203a803e..1990adce 100644 --- a/ale_linters/javascript/standard.vim +++ b/ale_linters/javascript/standard.vim @@ -7,6 +7,7 @@ call ale#Set('javascript_standard_options', '') function! ale_linters#javascript#standard#GetExecutable(buffer) abort return ale#node#FindExecutable(a:buffer, 'javascript_standard', [ + \ 'node_modules/standardx/bin/cmd.js', \ 'node_modules/standard/bin/cmd.js', \ 'node_modules/semistandard/bin/cmd.js', \ 'node_modules/.bin/standard', diff --git a/ale_linters/typescript/standard.vim b/ale_linters/typescript/standard.vim index 184d9727..da8f14eb 100644 --- a/ale_linters/typescript/standard.vim +++ b/ale_linters/typescript/standard.vim @@ -7,6 +7,7 @@ call ale#Set('typescript_standard_options', '') function! ale_linters#typescript#standard#GetExecutable(buffer) abort return ale#node#FindExecutable(a:buffer, 'typescript_standard', [ + \ 'node_modules/standardx/bin/cmd.js', \ 'node_modules/standard/bin/cmd.js', \ 'node_modules/.bin/standard', \]) diff --git a/autoload/ale/fixers/standard.vim b/autoload/ale/fixers/standard.vim index 731a4f46..cffa9f9d 100644 --- a/autoload/ale/fixers/standard.vim +++ b/autoload/ale/fixers/standard.vim @@ -7,6 +7,7 @@ call ale#Set('javascript_standard_options', '') function! ale#fixers#standard#GetExecutable(buffer) abort return ale#node#FindExecutable(a:buffer, 'javascript_standard', [ + \ 'node_modules/standardx/bin/cmd.js', \ 'node_modules/standard/bin/cmd.js', \ 'node_modules/.bin/standard', \])