@@ -137,6 +137,7 @@ var rewrites = []struct {
137137 {"formatdocstrings" , formatDocstrings , scopeBoth },
138138 {"reorderarguments" , reorderArguments , scopeBoth },
139139 {"editoctal" , editOctals , scopeBoth },
140+ {"editfloat" , editFloats , scopeBoth },
140141}
141142
142143// leaveAlone reports whether any of the nodes on the stack are marked
@@ -1399,6 +1400,35 @@ func editOctals(f *File, _ *Rewriter) {
13991400 })
14001401}
14011402
1403+ // editFloats inserts '0' before the decimal point in floats and normalizes the
1404+ // exponent part to lowercase, no plus sign, and no leading zero.
1405+ func editFloats (f * File , _ * Rewriter ) {
1406+ Walk (f , func (expr Expr , stack []Expr ) {
1407+ l , ok := expr .(* LiteralExpr )
1408+ if ! ok {
1409+ return
1410+ }
1411+ if ! strings .ContainsRune (l .Token , '.' ) {
1412+ return
1413+ }
1414+ if strings .HasPrefix (l .Token , "." ) {
1415+ l .Token = "0" + l .Token
1416+ }
1417+ if ! strings .ContainsAny (l .Token , "eE" ) {
1418+ return
1419+ }
1420+ parts := strings .SplitN (l .Token , "e" , 2 )
1421+ if len (parts ) != 2 {
1422+ parts = strings .SplitN (l .Token , "E" , 2 )
1423+ }
1424+ if len (parts ) != 2 {
1425+ // Invalid float, skip rewriting.
1426+ return
1427+ }
1428+ l .Token = parts [0 ] + "e" + strings .TrimLeft (parts [1 ], "0+" )
1429+ })
1430+ }
1431+
14021432// removeParens removes trivial parens
14031433func removeParens (f * File , _ * Rewriter ) {
14041434 var simplify func (expr Expr , stack []Expr ) Expr
0 commit comments