From 9ff5016729e6a022b60d3d10e47e2a75b9ddd263 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Sat, 6 May 2023 09:19:58 +0800 Subject: [PATCH] fix bug: enums of underscored number Signed-off-by: sdghchj --- package.go | 5 +++++ testdata/enums/consts/const.go | 1 + 2 files changed, 6 insertions(+) diff --git a/package.go b/package.go index bc25786ce..6b4e3bd0d 100644 --- a/package.go +++ b/package.go @@ -5,6 +5,7 @@ import ( "go/token" "reflect" "strconv" + "strings" ) // PackageDefinitions files and definition in a package. @@ -94,6 +95,10 @@ func (pkg *PackageDefinitions) evaluateConstValue(file *ast.File, iota int, expr case *ast.BasicLit: switch valueExpr.Kind { case token.INT: + // handle underscored number, such as 1_000_000 + if strings.ContainsRune(valueExpr.Value, '_') { + valueExpr.Value = strings.Replace(valueExpr.Value, "_", "", -1) + } // hexadecimal if len(valueExpr.Value) > 2 && valueExpr.Value[0] == '0' && valueExpr.Value[1] == 'x' { if x, err := strconv.ParseInt(valueExpr.Value[2:], 16, 64); err == nil { diff --git a/testdata/enums/consts/const.go b/testdata/enums/consts/const.go index 83dc97fa7..27bfb28f5 100644 --- a/testdata/enums/consts/const.go +++ b/testdata/enums/consts/const.go @@ -10,3 +10,4 @@ const octnum = 017 const nonescapestr = `aa\nbb\u8888cc` const escapestr = "aa\nbb\u8888cc" const escapechar = '\u8888' +const underscored = 1_000_000