forked from platanus/hound-linters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
shellcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "linters/base/options" | ||
require "linters/shellcheck/tokenizer" | ||
|
||
module Linters | ||
module Shellcheck | ||
class Options < Linters::Base::Options | ||
def command | ||
"shellcheck #{command_options} #{filepath}" | ||
end | ||
|
||
def tokenizer | ||
Tokenizer.new | ||
end | ||
|
||
def config_content; end | ||
|
||
private | ||
|
||
attr_reader :filepath, :raw_config_content | ||
|
||
def command_options | ||
excluded_rules = parsed_config["exclude"] | ||
if excluded_rules | ||
"-e #{excluded_rules.join(',')}" | ||
end | ||
end | ||
|
||
def parsed_config | ||
YAML.safe_load(config).to_h | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Linters | ||
module Shellcheck | ||
class Tokenizer | ||
VIOLATION_REGEX = /\A | ||
\s*In\s | ||
(?<file>.+)\sline\s | ||
(?<line_number>\d+).*\^--\s | ||
(?<code>\w+):\s | ||
(?<message>.*) | ||
\z/xm | ||
|
||
def parse(text) | ||
text.split("\n\n").map { |line| tokenize(line) }.compact | ||
end | ||
|
||
private | ||
|
||
def tokenize(line) | ||
matches = VIOLATION_REGEX.match(line) | ||
|
||
if matches | ||
line_number = matches[:line_number].to_i | ||
{ | ||
line: line_number, | ||
message: matches[:message].strip, | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "jobs/linters_job" | ||
|
||
RSpec.describe LintersJob, "for ShellCheck" do | ||
include LintersHelper | ||
|
||
context "when file contains violations" do | ||
it "reports violations" do | ||
expect_violations_in_file( | ||
config: "", | ||
content: content, | ||
filename: "foo/bar.sh", | ||
linter_name: "shellcheck", | ||
violations: [ | ||
{ | ||
line: 3, | ||
message: "Double quote to prevent globbing and word splitting.", | ||
}, | ||
{ | ||
line: 4, | ||
message: "foo appears unused. Verify it or export it.", | ||
}, | ||
], | ||
) | ||
end | ||
end | ||
|
||
context "when custom configuraton is provided" do | ||
it "respects the custom configuration" do | ||
config = <<~CONFIG | ||
exclude: | ||
- SC2086 # Double quote to prevent globbing and word splitting. | ||
CONFIG | ||
|
||
expect_violations_in_file( | ||
config: config, | ||
content: content, | ||
filename: "foo/bar.sh", | ||
linter_name: "shellcheck", | ||
violations: [ | ||
{ | ||
line: 4, | ||
message: "foo appears unused. Verify it or export it.", | ||
}, | ||
], | ||
) | ||
end | ||
end | ||
|
||
def content | ||
<<~SCRIPT | ||
#!/bin/sh | ||
hello_world () { | ||
echo $1 | ||
foo=42 | ||
} | ||
SCRIPT | ||
end | ||
end |