Add an option for ignoring the output of TSLint if a file contains a single blank line

This commit is contained in:
w0rp 2017-09-06 11:17:21 +01:00
parent ad1bd424fa
commit c277cdef8c
3 changed files with 138 additions and 0 deletions

View File

@ -5,6 +5,7 @@ call ale#Set('typescript_tslint_executable', 'tslint')
call ale#Set('typescript_tslint_config_path', '')
call ale#Set('typescript_tslint_rules_dir', '')
call ale#Set('typescript_tslint_use_global', 0)
call ale#Set('typescript_tslint_ignore_empty_files', 0)
function! ale_linters#typescript#tslint#GetExecutable(buffer) abort
return ale#node#FindExecutable(a:buffer, 'typescript_tslint', [
@ -13,6 +14,12 @@ function! ale_linters#typescript#tslint#GetExecutable(buffer) abort
endfunction
function! ale_linters#typescript#tslint#Handle(buffer, lines) abort
" Do not output any errors for empty files if the option is on.
if ale#Var(a:buffer, 'typescript_tslint_ignore_empty_files')
\&& getbufline(a:buffer, 1, '$') == ['']
return []
endif
let l:dir = expand('#' . a:buffer . ':p:h')
let l:output = []

View File

@ -30,6 +30,18 @@ g:ale_typescript_tslint_config_path *g:ale_typescript_tslint_config_path*
such path exists, this variable will be used instead.
g:ale_typescript_tslint_ignore_empty_files
*g:ale_typescript_tslint_ignore_empty_files*
*b:ale_typescript_tslint_ignore_empty_files*
Type: |Number|
Default: `0`
When set to `1`, ALE will not report any problems for empty files with
TSLint. ALE will still execute TSLint for the files, but ignore any problems
reported. This stops ALE from complaining about newly created files,
and files where lines have been added and then removed.
g:ale_typescript_tslint_rules_dir *g:ale_typescript_tslint_rules_dir*
*b:ale_typescript_tslint_rules_dir*
Type: |String|

View File

@ -1,9 +1,18 @@
Before:
Save g:ale_typescript_tslint_ignore_empty_files
unlet! g:ale_typescript_tslint_ignore_empty_files
unlet! b:ale_typescript_tslint_ignore_empty_files
runtime ale_linters/typescript/tslint.vim
call ale#test#SetDirectory('/testplugin/test/handler')
After:
Restore
unlet! b:ale_typescript_tslint_ignore_empty_files
call ale#test#RestoreDirectory()
call ale#linter#Reset()
@ -133,3 +142,113 @@ Execute(The tslint handler should handle empty output):
AssertEqual
\ [],
\ ale_linters#typescript#tslint#Handle(bufnr(''), [])
Execute(The tslint handler report errors for empty files by default):
call ale#test#SetFilename('app/test.ts')
AssertEqual
\ [
\ {
\ 'lnum': 2,
\ 'col': 1,
\ 'filename': expand('%:p:h') . '/test.ts',
\ 'end_lnum': 2,
\ 'type': 'E',
\ 'end_col': 1,
\ 'text': 'no-consecutive-blank-lines: Consecutive blank lines are forbidden',
\ },
\ ],
\ ale_linters#typescript#tslint#Handle(bufnr(''), [json_encode([{
\ 'endPosition': {
\ 'character': 0,
\ 'line': 1,
\ 'position': 1
\ },
\ 'failure': 'Consecutive blank lines are forbidden',
\ 'fix': [{
\ 'innerStart': 0,
\ 'innerLength': 1,
\ 'innerText': ''
\ }],
\ 'name': 'test.ts',
\ 'ruleName': 'no-consecutive-blank-lines',
\ 'ruleSeverity': 'ERROR',
\ 'startPosition': {
\ 'character': 0,
\ 'line': 1,
\ 'position': 1
\ }
\ }])])
Execute(The tslint handler should not report errors for empty files when the ignore option is on):
let b:ale_typescript_tslint_ignore_empty_files = 1
call ale#test#SetFilename('app/test.ts')
AssertEqual
\ [
\ ],
\ ale_linters#typescript#tslint#Handle(bufnr(''), [json_encode([{
\ 'endPosition': {
\ 'character': 0,
\ 'line': 1,
\ 'position': 1
\ },
\ 'failure': 'Consecutive blank lines are forbidden',
\ 'fix': [{
\ 'innerStart': 0,
\ 'innerLength': 1,
\ 'innerText': ''
\ }],
\ 'name': 'test.ts',
\ 'ruleName': 'no-consecutive-blank-lines',
\ 'ruleSeverity': 'ERROR',
\ 'startPosition': {
\ 'character': 0,
\ 'line': 1,
\ 'position': 1
\ }
\ }])])
Given typescript(A file with extra blank lines):
const x = 3
const y = 4
Execute(The tslint handler should report errors when the ignore option is on, but the file is not empty):
let b:ale_typescript_tslint_ignore_empty_files = 1
call ale#test#SetFilename('app/test.ts')
AssertEqual
\ [
\ {
\ 'lnum': 2,
\ 'col': 1,
\ 'filename': expand('%:p:h') . '/test.ts',
\ 'end_lnum': 2,
\ 'type': 'E',
\ 'end_col': 1,
\ 'text': 'no-consecutive-blank-lines: Consecutive blank lines are forbidden',
\ },
\ ],
\ ale_linters#typescript#tslint#Handle(bufnr(''), [json_encode([{
\ 'endPosition': {
\ 'character': 0,
\ 'line': 1,
\ 'position': 1
\ },
\ 'failure': 'Consecutive blank lines are forbidden',
\ 'fix': [{
\ 'innerStart': 0,
\ 'innerLength': 1,
\ 'innerText': ''
\ }],
\ 'name': 'test.ts',
\ 'ruleName': 'no-consecutive-blank-lines',
\ 'ruleSeverity': 'ERROR',
\ 'startPosition': {
\ 'character': 0,
\ 'line': 1,
\ 'position': 1
\ }
\ }])])