From 28ed50bdecdc5a1b56b85e23daa51924b18efc5d Mon Sep 17 00:00:00 2001 From: Antoine Fabri Date: Sat, 15 Oct 2022 12:32:14 +0200 Subject: [PATCH 1/2] handle arg names correctly in deparse_call() --- R/deparse_call.R | 21 +- tests/testthat/_snaps/deparse_call.new.md | 248 ++++++++++++++++++++++ tests/testthat/test-deparse_call.R | 9 + 3 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/_snaps/deparse_call.new.md diff --git a/R/deparse_call.R b/R/deparse_call.R index 58773819..0dba41b0 100644 --- a/R/deparse_call.R +++ b/R/deparse_call.R @@ -131,13 +131,13 @@ deparse_call_impl <- function(call, one_liner = FALSE, indent = 0, pipe = FALSE, if (caller == "[" && length(call) > 1) { arg1 <- deparse_call_impl(call[[2]]) - other_args <- paste(vapply(call[-(1:2)], deparse_call_impl, character(1), one_liner = one_liner, indent = indent), collapse = ", ") + other_args <- deparse_named_args_to_string(call[-(1:2)], one_liner = one_liner, indent = indent) return(sprintf("%s[%s]", arg1, other_args)) } if (caller == "[[" && length(call) > 1) { arg1 <- deparse_call_impl(call[[2]]) - other_args <- paste(vapply(call[-(1:2)], deparse_call_impl, character(1), one_liner = one_liner, indent = indent), collapse = ", ") + other_args <- deparse_named_args_to_string(call[-(1:2)], one_liner = one_liner, indent = indent) return(sprintf("%s[[%s]]", arg1, other_args)) } @@ -175,10 +175,8 @@ deparse_call_impl <- function(call, one_liner = FALSE, indent = 0, pipe = FALSE, return(sprintf("%s |> %s(%s)", arg1, caller, paste(other_args, collapse = ", "))) } } - args <- vapply(call[-1], deparse_call_impl, character(1), one_liner = one_liner, indent = indent) - args <- paste(rlang::names2(args), "=", args) - args <- sub("^ = ", "", args) - sprintf("%s(%s)", caller, paste(args, collapse = ", ")) + args <- deparse_named_args_to_string(call[-1], one_liner = one_liner, indent = indent) + sprintf("%s(%s)", caller, args) } is_syntactic <- function(x) { @@ -197,4 +195,13 @@ is_infix_narrow <- function(x) { x %in% c("::", ":::", "$", "@", "^", ":") } - +# FIXME: better handling of indent, doesn't impact if we style +deparse_named_args_to_string <- function(args, one_liner, indent) { + args <- vapply(args, deparse_call_impl, character(1), one_liner = one_liner, indent = indent) + args <- paste(rlang::names2(args), "=", args) + args <- sub("^ = ", "", args) + # FIXME: the 80 is a bit arbitrary, since we don't account for indent and length of caller + if (one_liner || max(nchar(args)) < 80) return(paste(args, collapse = ", ")) + args <- paste(args, collapse = ",\n") + paste0("\n", args, "\n") +} diff --git a/tests/testthat/_snaps/deparse_call.new.md b/tests/testthat/_snaps/deparse_call.new.md new file mode 100644 index 00000000..5dd34745 --- /dev/null +++ b/tests/testthat/_snaps/deparse_call.new.md @@ -0,0 +1,248 @@ +# deparse_call() + + Code + deparse_call(call("+", 1, 2, 3), style = FALSE) + Output + [1] "`+`(1, 2, 3)" + Code + deparse_call(call("+", 1, 2), style = FALSE) + Output + [1] "1 + 2" + Code + deparse_call(call("+", 1), style = FALSE) + Output + [1] "+1" + Code + deparse_call(call("+"), style = FALSE) + Output + [1] "`+`()" + Code + deparse_call(call("$", "a", "b", "c"), style = FALSE) + Output + [1] "`$`(\"a\", \"b\", \"c\")" + Code + deparse_call(call("$", "a", "b"), style = FALSE) + Output + [1] "\"a\"$b" + Code + deparse_call(call("$", quote(a), "b"), style = FALSE) + Output + [1] "a$b" + Code + deparse_call(call("$", "a", 1), style = FALSE) + Output + [1] "`$`(\"a\", 1)" + Code + deparse_call(call("$", 1, "b"), style = FALSE) + Output + [1] "1$b" + Code + deparse_call(call("$"), style = FALSE) + Output + [1] "`$`()" + Code + deparse_call(call(":", 1, 2, 3), style = FALSE) + Output + [1] "`:`(1, 2, 3)" + Code + deparse_call(call(":", 1, 2), style = FALSE) + Output + [1] "1:2" + Code + deparse_call(call(":", 1), style = FALSE) + Output + [1] "`:`(1)" + Code + deparse_call(call(":"), style = FALSE) + Output + [1] "`:`()" + Code + deparse_call(call("[", 1, 2, 3), style = FALSE) + Output + [1] "1[2, 3]" + Code + deparse_call(call("[", 1, 2), style = FALSE) + Output + [1] "1[2]" + Code + deparse_call(call("[", 1), style = FALSE) + Output + [1] "1[]" + Code + deparse_call(call("["), style = FALSE) + Output + [1] "`[`()" + Code + deparse_call(call("[[", 1, 2, 3), style = FALSE) + Output + [1] "1[[2, 3]]" + Code + deparse_call(call("[[", 1, 2), style = FALSE) + Output + [1] "1[[2]]" + Code + deparse_call(call("[[", 1), style = FALSE) + Output + [1] "1[[]]" + Code + deparse_call(call("[["), style = FALSE) + Output + [1] "`[[`()" + Code + deparse_call(call("(", 1, 2), style = FALSE) + Output + [1] "`(`(1, 2)" + Code + deparse_call(call("(", 1), style = FALSE) + Output + [1] "(1)" + Code + deparse_call(call("("), style = FALSE) + Output + [1] "`(`()" + Code + deparse_call(call("{", 1, 2), style = FALSE) + Output + [1] "{\n 1\n 2\n}" + Code + deparse_call(call("{", 1, 2), one_liner = TRUE, style = FALSE) + Output + [1] "{1; 2}" + Code + deparse_call(quote({ + { + x + } + }), style = FALSE) + Output + [1] "{{ x }}" + Code + deparse_call(quote({ + { + 1 + } + }), style = FALSE) + Output + [1] "{\n {\n 1\n }\n}" + Code + deparse_call(quote({ + { + 1 + } + }), one_liner = TRUE, style = FALSE) + Output + [1] "{{1}}" + Code + deparse_call(call("non-syntactic", 1), style = FALSE) + Output + [1] "`non-syntactic`(1)" + Code + deparse_call(quote(foo(bar(baz(x), 1), arg = 2, empty = )), style = FALSE) + Output + [1] "foo(bar(baz(x), 1), arg = 2, empty = )" + Code + deparse_call(quote(foo(bar(baz(x), 1), arg = 2, empty = )), pipe = TRUE, style = FALSE) + Output + [1] "x |> baz() |> bar(1) |> foo(arg = 2, empty = )" + Code + deparse_call(quote(foo(a = 1, 2)), pipe = TRUE, style = FALSE) + Output + [1] "foo(a = 1, 2)" + Code + deparse_call(quote(function(x, y = 1, z = a) { + x + y + }), style = FALSE) + Output + [1] "function(x, y = 1, z = a) {\n x + y\n}" + Code + deparse_call(quote(function(x, y = 1, z = a) { + x + y + }), one_liner = TRUE, style = FALSE) + Output + [1] "function(x, y = 1, z = a) {x + y}" + Code + deparse_call(quote(if (cond) this else that), style = FALSE) + Output + [1] "if (cond) this else that" + Code + deparse_call(quote(if (cond) { + this + } else { + that + }), style = FALSE) + Output + [1] "if (cond) {\n this\n} else {\n that\n}" + Code + deparse_call(quote(while (cond) this), style = FALSE) + Output + [1] "while (cond) this" + Code + deparse_call(quote(while (cond) { + this + }), style = FALSE) + Output + [1] "while (cond) {\n this\n}" + Code + deparse_call(quote(for (i in this) that), style = FALSE) + Output + [1] "for (i in this) that" + Code + deparse_call(quote(for (i in this) { + that + }), style = FALSE) + Output + [1] "for (i in this) {\n that\n}" + Code + deparse_call(quote(repeat this), style = FALSE) + Output + [1] "repeat this" + Code + deparse_call(quote(repeat { + this + }), style = FALSE) + Output + [1] "repeat {\n this\n}" + Code + deparse_call(quote(`*a*`)) + Output + `*a*` + Code + deparse_call(quote(a(b = 1, c))) + Output + a(b = 1, c) + Code + deparse_call(quote(a[b = 1, c])) + Output + a[b = 1, c] + Code + deparse_call(quote(a[[b = 1, c]])) + Output + a[[b = 1, c]] + Code + deparse_call(quote(a( + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c))) + Output + a( + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c + ) + Code + deparse_call(quote(a[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c])) + Output + a[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c + ] + Code + deparse_call(quote(a[[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c]])) + Output + a[[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c + ]] + diff --git a/tests/testthat/test-deparse_call.R b/tests/testthat/test-deparse_call.R index 49384191..262c551a 100644 --- a/tests/testthat/test-deparse_call.R +++ b/tests/testthat/test-deparse_call.R @@ -51,5 +51,14 @@ test_that("deparse_call()", { # non syntatic symbols deparse_call(quote(`*a*`)) + + # brackets and function calls with names + deparse_call(quote(a(b=1, c))) + deparse_call(quote(a[b=1, c])) + deparse_call(quote(a[[b=1, c]])) + deparse_call(quote(a(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=1, c))) + deparse_call(quote(a[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=1, c])) + # looks odd, but that's on {styler} : https://github.com/r-lib/styler/issues/1029 + deparse_call(quote(a[[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=1, c]])) }) }) From f923547e16a674758296b654a0fc0bfe10bf46ea Mon Sep 17 00:00:00 2001 From: Antoine Fabri Date: Sat, 15 Oct 2022 12:36:12 +0200 Subject: [PATCH 2/2] Accept snapshot --- tests/testthat/_snaps/deparse_call.md | 39 ++++ tests/testthat/_snaps/deparse_call.new.md | 248 ---------------------- 2 files changed, 39 insertions(+), 248 deletions(-) delete mode 100644 tests/testthat/_snaps/deparse_call.new.md diff --git a/tests/testthat/_snaps/deparse_call.md b/tests/testthat/_snaps/deparse_call.md index 84fd9e40..5dd34745 100644 --- a/tests/testthat/_snaps/deparse_call.md +++ b/tests/testthat/_snaps/deparse_call.md @@ -206,4 +206,43 @@ deparse_call(quote(`*a*`)) Output `*a*` + Code + deparse_call(quote(a(b = 1, c))) + Output + a(b = 1, c) + Code + deparse_call(quote(a[b = 1, c])) + Output + a[b = 1, c] + Code + deparse_call(quote(a[[b = 1, c]])) + Output + a[[b = 1, c]] + Code + deparse_call(quote(a( + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c))) + Output + a( + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c + ) + Code + deparse_call(quote(a[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c])) + Output + a[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c + ] + Code + deparse_call(quote(a[[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c]])) + Output + a[[ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, + c + ]] diff --git a/tests/testthat/_snaps/deparse_call.new.md b/tests/testthat/_snaps/deparse_call.new.md deleted file mode 100644 index 5dd34745..00000000 --- a/tests/testthat/_snaps/deparse_call.new.md +++ /dev/null @@ -1,248 +0,0 @@ -# deparse_call() - - Code - deparse_call(call("+", 1, 2, 3), style = FALSE) - Output - [1] "`+`(1, 2, 3)" - Code - deparse_call(call("+", 1, 2), style = FALSE) - Output - [1] "1 + 2" - Code - deparse_call(call("+", 1), style = FALSE) - Output - [1] "+1" - Code - deparse_call(call("+"), style = FALSE) - Output - [1] "`+`()" - Code - deparse_call(call("$", "a", "b", "c"), style = FALSE) - Output - [1] "`$`(\"a\", \"b\", \"c\")" - Code - deparse_call(call("$", "a", "b"), style = FALSE) - Output - [1] "\"a\"$b" - Code - deparse_call(call("$", quote(a), "b"), style = FALSE) - Output - [1] "a$b" - Code - deparse_call(call("$", "a", 1), style = FALSE) - Output - [1] "`$`(\"a\", 1)" - Code - deparse_call(call("$", 1, "b"), style = FALSE) - Output - [1] "1$b" - Code - deparse_call(call("$"), style = FALSE) - Output - [1] "`$`()" - Code - deparse_call(call(":", 1, 2, 3), style = FALSE) - Output - [1] "`:`(1, 2, 3)" - Code - deparse_call(call(":", 1, 2), style = FALSE) - Output - [1] "1:2" - Code - deparse_call(call(":", 1), style = FALSE) - Output - [1] "`:`(1)" - Code - deparse_call(call(":"), style = FALSE) - Output - [1] "`:`()" - Code - deparse_call(call("[", 1, 2, 3), style = FALSE) - Output - [1] "1[2, 3]" - Code - deparse_call(call("[", 1, 2), style = FALSE) - Output - [1] "1[2]" - Code - deparse_call(call("[", 1), style = FALSE) - Output - [1] "1[]" - Code - deparse_call(call("["), style = FALSE) - Output - [1] "`[`()" - Code - deparse_call(call("[[", 1, 2, 3), style = FALSE) - Output - [1] "1[[2, 3]]" - Code - deparse_call(call("[[", 1, 2), style = FALSE) - Output - [1] "1[[2]]" - Code - deparse_call(call("[[", 1), style = FALSE) - Output - [1] "1[[]]" - Code - deparse_call(call("[["), style = FALSE) - Output - [1] "`[[`()" - Code - deparse_call(call("(", 1, 2), style = FALSE) - Output - [1] "`(`(1, 2)" - Code - deparse_call(call("(", 1), style = FALSE) - Output - [1] "(1)" - Code - deparse_call(call("("), style = FALSE) - Output - [1] "`(`()" - Code - deparse_call(call("{", 1, 2), style = FALSE) - Output - [1] "{\n 1\n 2\n}" - Code - deparse_call(call("{", 1, 2), one_liner = TRUE, style = FALSE) - Output - [1] "{1; 2}" - Code - deparse_call(quote({ - { - x - } - }), style = FALSE) - Output - [1] "{{ x }}" - Code - deparse_call(quote({ - { - 1 - } - }), style = FALSE) - Output - [1] "{\n {\n 1\n }\n}" - Code - deparse_call(quote({ - { - 1 - } - }), one_liner = TRUE, style = FALSE) - Output - [1] "{{1}}" - Code - deparse_call(call("non-syntactic", 1), style = FALSE) - Output - [1] "`non-syntactic`(1)" - Code - deparse_call(quote(foo(bar(baz(x), 1), arg = 2, empty = )), style = FALSE) - Output - [1] "foo(bar(baz(x), 1), arg = 2, empty = )" - Code - deparse_call(quote(foo(bar(baz(x), 1), arg = 2, empty = )), pipe = TRUE, style = FALSE) - Output - [1] "x |> baz() |> bar(1) |> foo(arg = 2, empty = )" - Code - deparse_call(quote(foo(a = 1, 2)), pipe = TRUE, style = FALSE) - Output - [1] "foo(a = 1, 2)" - Code - deparse_call(quote(function(x, y = 1, z = a) { - x + y - }), style = FALSE) - Output - [1] "function(x, y = 1, z = a) {\n x + y\n}" - Code - deparse_call(quote(function(x, y = 1, z = a) { - x + y - }), one_liner = TRUE, style = FALSE) - Output - [1] "function(x, y = 1, z = a) {x + y}" - Code - deparse_call(quote(if (cond) this else that), style = FALSE) - Output - [1] "if (cond) this else that" - Code - deparse_call(quote(if (cond) { - this - } else { - that - }), style = FALSE) - Output - [1] "if (cond) {\n this\n} else {\n that\n}" - Code - deparse_call(quote(while (cond) this), style = FALSE) - Output - [1] "while (cond) this" - Code - deparse_call(quote(while (cond) { - this - }), style = FALSE) - Output - [1] "while (cond) {\n this\n}" - Code - deparse_call(quote(for (i in this) that), style = FALSE) - Output - [1] "for (i in this) that" - Code - deparse_call(quote(for (i in this) { - that - }), style = FALSE) - Output - [1] "for (i in this) {\n that\n}" - Code - deparse_call(quote(repeat this), style = FALSE) - Output - [1] "repeat this" - Code - deparse_call(quote(repeat { - this - }), style = FALSE) - Output - [1] "repeat {\n this\n}" - Code - deparse_call(quote(`*a*`)) - Output - `*a*` - Code - deparse_call(quote(a(b = 1, c))) - Output - a(b = 1, c) - Code - deparse_call(quote(a[b = 1, c])) - Output - a[b = 1, c] - Code - deparse_call(quote(a[[b = 1, c]])) - Output - a[[b = 1, c]] - Code - deparse_call(quote(a( - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, - c))) - Output - a( - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, - c - ) - Code - deparse_call(quote(a[ - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, - c])) - Output - a[ - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, - c - ] - Code - deparse_call(quote(a[[ - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, - c]])) - Output - a[[ - bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1, - c - ]] -