Skip to content

Commit 5e4a020

Browse files
authored
Move PrettyPrintErrorMessage to top-level nilaway package (#393)
1 parent 07fde29 commit 5e4a020

3 files changed

Lines changed: 24 additions & 42 deletions

File tree

nilaway.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package nilaway
1818

1919
import (
20+
"fmt"
21+
"regexp"
22+
2023
"go.uber.org/nilaway/accumulation"
2124
"go.uber.org/nilaway/config"
22-
"go.uber.org/nilaway/util"
2325
"go.uber.org/nilaway/util/analysishelper"
2426
"golang.org/x/tools/go/analysis"
2527
)
@@ -43,10 +45,29 @@ func run(p *analysis.Pass) (interface{}, error) {
4345
deferredErrors := pass.ResultOf[accumulation.Analyzer].([]analysis.Diagnostic)
4446
for _, e := range deferredErrors {
4547
if conf.PrettyPrint {
46-
e.Message = util.PrettyPrintErrorMessage(e.Message)
48+
e.Message = PrettyPrintErrorMessage(e.Message)
4749
}
4850
pass.Report(e)
4951
}
5052

5153
return nil, nil
5254
}
55+
56+
var codeReferencePattern = regexp.MustCompile("\\`(.*?)\\`")
57+
var pathPattern = regexp.MustCompile(`"(.*?)"`)
58+
var nilabilityPattern = regexp.MustCompile(`([\(|^\t](?i)(found\s|must\sbe\s)(nilable|nonnil)[\)]?)`)
59+
60+
// PrettyPrintErrorMessage is used in error reporting to post process and pretty print the output with colors
61+
func PrettyPrintErrorMessage(msg string) string {
62+
// TODO: below string parsing should not be required after is implemented
63+
errorStr := fmt.Sprintf("\x1b[%dm%s\x1b[0m", 31, "error: ") // red
64+
codeStr := fmt.Sprintf("\u001B[%dm%s\u001B[0m", 95, "`${1}`") // magenta
65+
pathStr := fmt.Sprintf("\u001B[%dm%s\u001B[0m", 36, "${1}") // cyan
66+
nilabilityStr := fmt.Sprintf("\u001B[%dm%s\u001B[0m", 1, "${1}") // bold
67+
68+
msg = nilabilityPattern.ReplaceAllString(msg, nilabilityStr)
69+
msg = codeReferencePattern.ReplaceAllString(msg, codeStr)
70+
msg = pathPattern.ReplaceAllString(msg, pathStr)
71+
msg = errorStr + msg
72+
return msg
73+
}

util/guard_nonce.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package util hosts the guard nonce types and functions to identify and track RichCheckEffects.
1516
package util
1617

1718
import (

util/util.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)