Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Make spaces around comment symbols optional to match
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Biville committed Jan 3, 2020
1 parent a47fdeb commit dd25e41
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
7 changes: 2 additions & 5 deletions internal/pkg/core/header_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ func combineRegexes(styles []CommentStyle, getLine func(CommentStyle) string) st
commentSymbol := getLine(style)
if line := commentSymbol; line != "" {
regex := escape(line)
if strings.HasSuffix(commentSymbol, " ") {
// right spaces may be formatted away
// make the right space optional
regex += "?"
}
// spaces may be formatted away - make the space optional
regex = strings.Replace(regex, " ", " ?", -1)
regexes = append(regexes, regex)
}
}
Expand Down
43 changes: 32 additions & 11 deletions internal/pkg/core/header_detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("Header detector", func() {
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 43}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 43}))
})
})

Expand All @@ -55,7 +55,7 @@ world`
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 44}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 44}))
})
})

Expand All @@ -74,7 +74,7 @@ world`
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 87}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 87}))
})
})

Expand All @@ -95,7 +95,7 @@ world`
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 50}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 50}))
})
})

Expand All @@ -118,7 +118,7 @@ world`
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 87}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 87}))
})
})

Expand All @@ -136,7 +136,7 @@ world`
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 51}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 51}))
})
})

Expand All @@ -154,7 +154,28 @@ world`
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 44}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 44}))
})
})

Context("with whitespaces variations with comment style symbols including whitespaces", func() {


const file = `/*
*
*some multi-line header
*with some text
*/
hello
world`

It("should detect it", func() {
regex, err := core.ComputeHeaderDetectionRegex(
[]string{"some multi-line header", "with some text"},
map[string]string{})

Expect(err).NotTo(HaveOccurred())
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 51}))
})
})

Expand Down Expand Up @@ -204,15 +225,15 @@ func main() {
It("matches the header opening comment line", func() {
regex := fmt.Sprintf("%s%s", core.Flags(), core.OpeningLine(styles))

Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 3}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 3}))
})

It("matches the header intermediate comment line", func() {
regex := fmt.Sprintf("%s%s", core.Flags(), core.MatchingLine(
" http://www.apache.org/licenses/LICENSE-2.0",
styles))

Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{233, 283}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{233, 283}))
})

It("computes a regex to match existing header", func() {
Expand All @@ -225,11 +246,11 @@ func main() {
regex, err := core.ComputeHeaderDetectionRegex(templateLines, templateParameters)

Expect(err).NotTo(HaveOccurred())
Expect(MatchLeftMostPositions(regex, file)).To(Equal([]int{0, 610}))
Expect(matchLeftMostPositions(regex, file)).To(Equal([]int{0, 610}))
})
})
})

func MatchLeftMostPositions(regex, contents string) []int {
func matchLeftMostPositions(regex, contents string) []int {
return regexp.MustCompile(regex).FindStringIndex(contents)
}

0 comments on commit dd25e41

Please sign in to comment.