Skip to content

Commit

Permalink
Support file formats that do not support multi-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ebilling committed Nov 19, 2019
1 parent ffac378 commit f250de8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ scannerloop:
if strings.HasPrefix(line, singleComment) {
// check if single comment is a prefix of multi comment
for _, ml := range language.multiLines {
if strings.HasPrefix(line, ml[0]) {
if ml[0] != "" && strings.HasPrefix(line, ml[0]) {
break singleloop
}
}
Expand All @@ -109,6 +109,10 @@ scannerloop:

isCode := false
lenLine := len(line)
if len(language.multiLines) == 1 && len(language.multiLines[0]) == 2 && language.multiLines[0][0] == "" {
onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue
}
for pos := 0; pos < lenLine; {
for _, ml := range language.multiLines {
begin, end := ml[0], ml[1]
Expand Down
46 changes: 46 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,52 @@ func TestAnalyzeFile4JavaWithCommentInCodeLine(t *testing.T) {
}
}

func TestAnalyzeFile4Makefile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "Makefile.am")
if err != nil {
t.Logf("ioutil.TempFile() error. err=[%v]", err)
return
}
defer os.Remove(tmpfile.Name())

tmpfile.Write([]byte(`# This is a simple Makefile with comments
.PHONY: test build
build:
mkdir -p bin
GO111MODULE=on go build -o ./bin/gocloc cmd/gocloc/main.go
# Another comment
update-package:
GO111MODULE=on go get -u github.com/hhatto/gocloc
run-example:
GO111MODULE=on go run examples/languages.go
GO111MODULE=on go run examples/files.go
test:
GO111MODULE=on go test -v
`))

language := NewLanguage("Makefile", []string{"#"}, [][]string{{"", ""}})
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 4 {
t.Errorf("invalid logic. blanks=%v", clocFile.Blanks)
}
if clocFile.Comments != 2 {
t.Errorf("invalid logic. comments=%v", clocFile.Comments)
}
if clocFile.Code != 11 {
t.Errorf("invalid logic. code=%v", clocFile.Code)
}
if clocFile.Lang != "Makefile" {
t.Errorf("invalid logic. lang=%v", clocFile.Lang)
}
}

func TestAnalayzeReader(t *testing.T) {
buf := bytes.NewBuffer([]byte(`#!/bin/python
Expand Down

0 comments on commit f250de8

Please sign in to comment.