From 9017d3ef9ce06a3662f6c87becffa7d7143fb571 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Fri, 26 Jul 2019 11:52:34 -0400 Subject: [PATCH] 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(''))