From f68144774d11b73609b89d9b3f89795aa0e4e4dd Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Wed, 16 Feb 2022 16:08:29 -0300 Subject: [PATCH] fmt: fix map missing a comma after enum keys, leading to non parsable code (#13481) --- vlib/v/fmt/fmt.v | 17 +++++++++++++---- vlib/v/fmt/tests/maps_expected.vv | 5 +++++ vlib/v/fmt/tests/maps_input.vv | 5 +++++ vlib/v/tests/map_init_with_enum_keys_test.v | 2 +- vlib/vweb/vweb.v | 2 +- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 0d4f3d0bee..c88dff1a8b 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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('') } diff --git a/vlib/v/fmt/tests/maps_expected.vv b/vlib/v/fmt/tests/maps_expected.vv index 2cb1fcab7d..dd2e3742b1 100644 --- a/vlib/v/fmt/tests/maps_expected.vv +++ b/vlib/v/fmt/tests/maps_expected.vv @@ -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', +}) diff --git a/vlib/v/fmt/tests/maps_input.vv b/vlib/v/fmt/tests/maps_input.vv index a380a73ea7..bafe3017f2 100644 --- a/vlib/v/fmt/tests/maps_input.vv +++ b/vlib/v/fmt/tests/maps_input.vv @@ -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' +}) diff --git a/vlib/v/tests/map_init_with_enum_keys_test.v b/vlib/v/tests/map_init_with_enum_keys_test.v index 067945126d..d0df5dd6fe 100644 --- a/vlib/v/tests/map_init_with_enum_keys_test.v +++ b/vlib/v/tests/map_init_with_enum_keys_test.v @@ -12,7 +12,7 @@ fn test_map_init_with_enum_keys() { mut st := St{} st.m = { - .ea: 'a' + .ea: 'a', } println(st.m) diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 479c98addf..2cdceab469 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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') }