Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/overcommit/hook/pre_commit/ts_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ module Overcommit::Hook::PreCommit
# Runs `tslint` against modified TypeScript files.
# @see http://palantir.github.io/tslint/
class TsLint < Base
# example message:
# "src/file/anotherfile.ts[298, 1]: exceeds maximum line length of 140"
# or
# "ERROR: src/AccountController.ts[4, 28]: expected call-signature to have a typedef"
MESSAGE_REGEX = /^(?<type>.+: )?(?<file>.+?(?=\[))[^\d]+(?<line>\d+).*?/

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp
return :pass if result.success? && output.empty?

# example message:
# src/file/anotherfile.ts[298, 1]: exceeds maximum line length of 140
output_lines = output.split("\n").map(&:strip).reject(&:empty?)
type_categorizer = ->(type) { type.nil? || type.include?('ERROR') ? :error : :warning }

extract_messages(
output.split("\n"),
/^(?<file>.+?(?=\[))[^\d]+(?<line>\d+).*?/
output_lines,
MESSAGE_REGEX,
type_categorizer
)
end
end
Expand Down
24 changes: 22 additions & 2 deletions spec/overcommit/hook/pre_commit/ts_lint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,32 @@

context 'and it reports an error' do
before do
result.stub(:stdout).and_return([
result.stub(:stdout).and_return(
'src/file/anotherfile.ts[298, 1]: exceeds maximum line length of 140'
].join("\n"))
)
end

it { should fail_hook }
end

context 'and it reports an error with an "ERROR" severity' do
before do
result.stub(:stdout).and_return(
'ERROR: src/AccountController.ts[4, 28]: expected call-signature to have a typedef'
)
end

it { should fail_hook }
end

context 'and it reports an warning' do
before do
result.stub(:stdout).and_return(
'WARNING: src/AccountController.ts[4, 28]: expected call-signature to have a typedef'
)
end

it { should warn }
end
end
end