Skip to content

Commit 4cfccd3

Browse files
authored
Filter out "name" and "unresolved" nodes (#299)
* Properly exclude "name" key from s-expression, as it's basically noise that we don't need to evaluate * Update spec with example faulty duplication files, better tests
1 parent 74089bc commit 4cfccd3

File tree

4 files changed

+597
-8
lines changed

4 files changed

+597
-8
lines changed

lib/cc/engine/analyzers/go/main.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ class Main < CC::Engine::Analyzers::Base
2020
REQUEST_PATH = "/go"
2121
COMMENT_MATCHER = Sexp::Matcher.parse("(_ (comments ___) ___)")
2222

23-
def use_sexp_lines?
24-
false
25-
end
26-
2723
def transform_sexp(sexp)
2824
delete_comments!(sexp)
25+
sexp.delete_if { |node| node[0] == :name }
26+
end
27+
28+
def use_sexp_lines?
29+
false
2930
end
3031

3132
private

spec/cc/engine/analyzers/go/main_spec.rb

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ module CC::Engine::Analyzers
3636
"path" => "foo.go",
3737
"lines" => { "begin" => 6, "end" => 6 },
3838
})
39-
expect(json["remediation_points"]).to eq(820_000)
39+
expect(json["remediation_points"]).to eq(540_000)
4040
expect(json["other_locations"]).to eq([
4141
{"path" => "foo.go", "lines" => { "begin" => 7, "end" => 7} },
4242
])
4343
expect(json["content"]["body"]).to match(/This issue has a mass of 16/)
4444
expect(json["fingerprint"]).to eq("484ee5799eb0e6c933751cfa85ba33c3")
45-
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR)
45+
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MINOR)
4646
end
4747

4848
it "prints an issue for similar code" do
@@ -82,7 +82,7 @@ module CC::Engine::Analyzers
8282
"path" => "foo.go",
8383
"lines" => { "begin" => 5, "end" => 7 },
8484
})
85-
expect(json["remediation_points"]).to eq(1_540_000)
85+
expect(json["remediation_points"]).to eq(1_260_000)
8686
expect(json["other_locations"]).to eq([
8787
{"path" => "foo.go", "lines" => { "begin" => 9, "end" => 11} },
8888
{"path" => "foo.go", "lines" => { "begin" => 13, "end" => 15} },
@@ -127,17 +127,70 @@ module CC::Engine::Analyzers
127127
expect(issues).to be_empty
128128
end
129129

130+
it "does not flag entire file as issue" do
131+
create_source_file("foo.go", File.read(fixture_path("issue_6609_1.go")))
132+
create_source_file("bar.go", File.read(fixture_path("issue_6609_2.go")))
133+
issues = run_engine(engine_conf).strip.split("\0")
134+
issues.map! {|issue| JSON.parse issue}
135+
invalid_issues = issues.find_all{|issue| issue["location"]["lines"]["begin"] == 1}
136+
expect(invalid_issues).to be_empty, invalid_issues.map {|issue| issue["location"]}.join("\n")
137+
end
138+
130139
it "does not flag duplicate comments" do
131140
create_source_file("foo.go", <<-EOGO)
141+
// This is a comment.
142+
// This is a comment.
143+
// This is a comment.
144+
// This is also a comment.
145+
// This is also a comment.
146+
132147
package main
133148
149+
// import "fmt"
150+
151+
func main() {
152+
fmt.Println("This is a duplicate!")
153+
}
154+
155+
/* This is a multiline comment */
156+
/* This is a multiline comment */
157+
/* This is a also multiline comment */
158+
/* This is a also multiline comment */
159+
160+
// func add(x int, y int) int {
161+
// return x + y
162+
// }
163+
164+
// func add(x int, y int) int {
165+
// return x + y
166+
// }
167+
168+
// func add(x int, y int) int {
169+
// return x + y
170+
// }
171+
172+
// func add(x int, y int) int {
173+
// return x + y
174+
// }
175+
EOGO
176+
177+
create_source_file("bar.go", <<-EOGO)
134178
// This is a comment.
135179
// This is a comment.
136180
// This is a comment.
137181
// This is also a comment.
138182
// This is also a comment.
139183
184+
package main
185+
186+
// import "fmt"
187+
140188
func main() {
189+
// This is a comment.
190+
// This is a comment.
191+
// This is a comment.
192+
// This is also a comment.
193+
// This is also a comment.
141194
}
142195
143196
/* This is a multiline comment */
@@ -178,7 +231,7 @@ def engine_conf
178231
},
179232
'languages' => {
180233
'go' => {
181-
'mass_threshold' => 3,
234+
'mass_threshold' => 10,
182235
},
183236
},
184237
},

0 commit comments

Comments
 (0)