Skip to content

Commit

Permalink
fix: time-naming issue #571 (#572)
Browse files Browse the repository at this point in the history
resolves #571
  • Loading branch information
butuzov committed Sep 12, 2021
1 parent 9b85893 commit d7e3d5e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
8 changes: 5 additions & 3 deletions rule/time-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ func (w *lintTimeNames) Visit(node ast.Node) ast.Visitor {
// timeSuffixes is a list of name suffixes that imply a time unit.
// This is not an exhaustive list.
var timeSuffixes = []string{
"Sec", "Secs", "Seconds",
"Hour", "Hours",
"Min", "Mins", "Minutes", "Minute",
"Sec", "Secs", "Seconds", "Second",
"Msec", "Msecs",
"Milli", "Millis", "Milliseconds",
"Usec", "Usecs", "Microseconds",
"Milli", "Millis", "Milliseconds", "Millisecond",
"Usec", "Usecs", "Microseconds", "Microsecond",
"MS", "Ms",
}

Expand Down
12 changes: 12 additions & 0 deletions test/time-naming_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test

import (
"testing"

"github.com/mgechev/revive/rule"
)

// TestTimeNamingRule rule.
func TestTimeNaming(t *testing.T) {
testRule(t, "time-naming", &rule.TimeNamingRule{})
}
46 changes: 46 additions & 0 deletions testdata/time-naming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package testdata

import "time"

var (
Hour = time.Hour // MATCH /var Hour is of type time.Duration; don't use unit-specific suffix "Hour"/
oneHour = time.Hour // MATCH /var oneHour is of type time.Duration; don't use unit-specific suffix "Hour"/
twoHours = 2 * time.Hour // MATCH /var twoHours is of type time.Duration; don't use unit-specific suffix "Hours"/
TenHours = 10 * time.Hour // MATCH /var TenHours is of type time.Duration; don't use unit-specific suffix "Hours"/
SixHours = 6 * time.Hour // MATCH /var SixHours is of type time.Duration; don't use unit-specific suffix "Hours"/

oneMin = time.Minute // MATCH /var oneMin is of type time.Duration; don't use unit-specific suffix "Min"/
Min = time.Minute // MATCH /var Min is of type time.Duration; don't use unit-specific suffix "Min"/
twoMin = 2 * time.Minute // MATCH /var twoMin is of type time.Duration; don't use unit-specific suffix "Min"/
SixMin = 6 * time.Minute // MATCH /var SixMin is of type time.Duration; don't use unit-specific suffix "Min"/
SixMins = 6 * time.Minute // MATCH /var SixMins is of type time.Duration; don't use unit-specific suffix "Mins"/
SixMinutes = 6 * time.Minute // MATCH /var SixMinutes is of type time.Duration; don't use unit-specific suffix "Minutes"/

oneSec = time.Second // MATCH /var oneSec is of type time.Duration; don't use unit-specific suffix "Sec"/
Sec = time.Second // MATCH /var Sec is of type time.Duration; don't use unit-specific suffix "Sec"/
SixSec = 6 * time.Second // MATCH /var SixSec is of type time.Duration; don't use unit-specific suffix "Sec"/
twoSecs = 2 * time.Second // MATCH /var twoSecs is of type time.Duration; don't use unit-specific suffix "Secs"/
SixSeconds = 6 * time.Second // MATCH /var SixSeconds is of type time.Duration; don't use unit-specific suffix "Seconds"/
oneSecond = time.Second // MATCH /var oneSecond is of type time.Duration; don't use unit-specific suffix "Second"/
Second = time.Second // MATCH /var Second is of type time.Duration; don't use unit-specific suffix "Second"/

SixMsec = 6 * time.Millisecond // MATCH /var SixMsec is of type time.Duration; don't use unit-specific suffix "Msec"/
oneMsec = time.Millisecond // MATCH /var oneMsec is of type time.Duration; don't use unit-specific suffix "Msec"/
SixMsecs = 6 * time.Millisecond // MATCH /var SixMsecs is of type time.Duration; don't use unit-specific suffix "Msecs"/
oneMilli = time.Millisecond // MATCH /var oneMilli is of type time.Duration; don't use unit-specific suffix "Milli"/
SixMillis = 6 * time.Millisecond // MATCH /var SixMillis is of type time.Duration; don't use unit-specific suffix "Millis"/
SixMilliseconds = 6 * time.Millisecond // MATCH /var SixMilliseconds is of type time.Duration; don't use unit-specific suffix "Milliseconds"/
oneMillisecond = time.Millisecond // MATCH /var oneMillisecond is of type time.Duration; don't use unit-specific suffix "Millisecond"/
Millisecond = time.Millisecond // MATCH /var Millisecond is of type time.Duration; don't use unit-specific suffix "Millisecond"/

oneUsec = 1 * time.Microsecond // MATCH /var oneUsec is of type time.Duration; don't use unit-specific suffix "Usec"/
twoUsec = 2 * time.Microsecond // MATCH /var twoUsec is of type time.Duration; don't use unit-specific suffix "Usec"/
SixUsec = 6 * time.Microsecond // MATCH /var SixUsec is of type time.Duration; don't use unit-specific suffix "Usec"/
SixUsecs = 6 * time.Microsecond // MATCH /var SixUsecs is of type time.Duration; don't use unit-specific suffix "Usecs"/
twoMicroseconds = 2 * time.Microsecond // MATCH /var twoMicroseconds is of type time.Duration; don't use unit-specific suffix "Microseconds"/
SixMicroseconds = 6 * time.Microsecond // MATCH /var SixMicroseconds is of type time.Duration; don't use unit-specific suffix "Microseconds"/
oneMicrosecond = 1 * time.Microsecond // MATCH /var oneMicrosecond is of type time.Duration; don't use unit-specific suffix "Microsecond"/
SixMS = 6 * time.Microsecond // MATCH /var SixMS is of type time.Duration; don't use unit-specific suffix "MS"/
oneMS = 1 * time.Microsecond // MATCH /var oneMS is of type time.Duration; don't use unit-specific suffix "MS"/

)

0 comments on commit d7e3d5e

Please sign in to comment.