fmt: fix map missing a comma after enum keys, leading to non parsable code (#13481)
parent
d3b8ac2e46
commit
f68144774d
|
@ -2110,16 +2110,25 @@ pub fn (mut f Fmt) map_init(node ast.MapInit) {
|
|||
f.indent++
|
||||
f.comments(node.pre_cmnts)
|
||||
mut max_field_len := 0
|
||||
mut skeys := []string{}
|
||||
for key in node.keys {
|
||||
if key.str().len > max_field_len {
|
||||
max_field_len = key.str().len
|
||||
skey := f.node_str(key).trim_space()
|
||||
skeys << skey
|
||||
if skey.len > max_field_len {
|
||||
max_field_len = skey.len
|
||||
}
|
||||
}
|
||||
for i, key in node.keys {
|
||||
f.expr(key)
|
||||
skey := skeys[i]
|
||||
f.write(skey)
|
||||
f.write(': ')
|
||||
f.write(strings.repeat(` `, max_field_len - key.str().len))
|
||||
f.write(strings.repeat(` `, max_field_len - skey.len))
|
||||
f.expr(node.vals[i])
|
||||
if key is ast.EnumVal && skey.starts_with('.') {
|
||||
// enforce the use of `,` for maps with short enum keys, otherwise there is ambiguity
|
||||
// when the values are struct values, and the code will no longer parse properly
|
||||
f.write(',')
|
||||
}
|
||||
f.comments(node.comments[i], prev_line: node.vals[i].pos().last_line, has_nl: false)
|
||||
f.writeln('')
|
||||
}
|
||||
|
|
|
@ -20,3 +20,8 @@ explicit_init := map[string]string{}
|
|||
explicit_init_with_value := {
|
||||
'abc': 0
|
||||
}
|
||||
|
||||
headers := http.new_header_from_map({
|
||||
.content_type: 'application/json',
|
||||
.authorization: 'Bearer abcdef',
|
||||
})
|
||||
|
|
|
@ -20,3 +20,8 @@ explicit_init := map[string]string{}
|
|||
explicit_init_with_value := map[string]int{
|
||||
'abc': 0
|
||||
}
|
||||
|
||||
headers := http.new_header_from_map({
|
||||
.content_type: 'application/json',
|
||||
.authorization: 'Bearer abcdef'
|
||||
})
|
||||
|
|
|
@ -12,7 +12,7 @@ fn test_map_init_with_enum_keys() {
|
|||
mut st := St{}
|
||||
|
||||
st.m = {
|
||||
.ea: 'a'
|
||||
.ea: 'a',
|
||||
}
|
||||
|
||||
println(st.m)
|
||||
|
|
|
@ -21,7 +21,7 @@ pub struct Result {}
|
|||
pub const (
|
||||
methods_with_form = [http.Method.post, .put, .patch]
|
||||
headers_close = http.new_custom_header_from_map({
|
||||
'Server': 'VWeb'
|
||||
'Server': 'VWeb'
|
||||
http.CommonHeader.connection.str(): 'close'
|
||||
}) or { panic('should never fail') }
|
||||
|
||||
|
|
Loading…
Reference in New Issue