Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line break before function() provoke unexpected problem #142

Closed
9 tasks
pwaeckerle opened this issue Apr 21, 2022 · 2 comments
Closed
9 tasks

Line break before function() provoke unexpected problem #142

pwaeckerle opened this issue Apr 21, 2022 · 2 comments
Assignees

Comments

@pwaeckerle
Copy link

pwaeckerle commented Apr 21, 2022

Criteria of validation

All the below cases are tested with a specific flat test file: https://github.com/ThinkR-open/fusen/blob/main/inst/tests-templates/dev-template-function-name-linebreak.Rmd

The use of the following codes in a flat file do not affect the correct detection of the function real name :

  • linebreak, roxygen before
#' fake_name
real_name <- 
   function()
  • linebreak, comment before
#' fake_name
# a comment
real_name <- 
function()
  • comment between name and linebreak
#' fake_name
real_name <- # a comment
function()
  • commented line between name and linebreak
#' fake_name
real_name <- 
# a comment
function()
  • linebreak, nothing before
real_name <- 
   function()
  • no linebreak, comment before
#' fake_name
# a comment
real_name <- function()
  • no linebreak, code before
sqrt(1)
real_name <- function()
  • no linebreak, nothing before
real_name <- function()
  • linebreak, roxygen before, symbol "="
#' fake_name
real_name  =
   function()

Technical

Proposition 1 : detect the 1st assignment arrow in the code that is not in a comment or a roxygen
Propostion 2 : combine the line above in paste(line_before, line). A line break turns into a space, and there is no risk of catching a different name


Problem description

When adding a line break between the affectation sign and the function keyword, fusen does not handle correctly the flat file content.

This line break can be added by the user, but more surely by the RStudio reformat shortcut.

fusen shows warning messages that alerts the user :

Updating mypkg documentationLoading mypkg
Warning: [/home/rstudio/mypkg/R/my-function.R:8] @examples requires a value
Writing NAMESPACE
Writing NAMESPACE
Writing debug_fusen.Rd

In the output, the vignette is created as expected but the .R file has the name of the makdown title above the function description in the flat file.

Reprex

Working example

I use a minimal flat file with the following function description and example (no test) :

# My function

```{r function-debug_fusen}
#' debug_fusen
#'
#' @return 1
#' @export
#'
#' @examples
debug_fusen <- function() {
  1
}
debug_fusen()

When inflating, we have : a debug_fusen.R file in R\ and a vignette corresponding to the vignette_name attribute in the fusen::inflate() call.

Not working example

The same cas as below, with the simple addition of a line break before function :

# My function

```{r function-debug_fusen}
#' debug_fusen
#'
#' @return 1
#' @export
#'
#' @examples
debug_fusen <- 
  function() {
  1
}
debug_fusen()

When doing the inflate, we have :

  • Warning message (see above) regarding the absence of a function example,
  • The vignette updated (still well located thanks to the filename argument),
  • a my_function.R file created with no example in it.
@statnmap statnmap added this to the v0.4 milestone Apr 22, 2022
statnmap added a commit that referenced this issue Apr 29, 2022
Why?

- Some may name their functions on multiple lines

What?

- Modifiy test flat file and fix the inflate function

issue #142
@statnmap statnmap modified the milestones: v0.4, v0.5 Apr 29, 2022
@FlorenceMounier
Copy link
Contributor

FlorenceMounier commented Sep 30, 2022

Problem:
in R/inflate-utils.R, parsermd::rmd_node_code() splits code by reading line breaks (1 value = 1 line).

Proposal:
add a new regex that would detect the pattern "<-" in "my_fun <- " and bind this value with the value returned by the regex regex_isfunction that detects the value " function() {"

FlorenceMounier added a commit that referenced this issue Nov 23, 2022
tags: fix

Why?
When adding a line break between the affectation sign and the function keyword, fusen does not handle correctly the flat file content.

What?
- Create a new test-template Rmd file in "inst/tests-templates" that produces this bug with `inflate()` and other examples that should still work after parse_fun() modification
- Add a new unit test in "tests/testthat/test-inflate-part2.R" that runs the new test-template and test the proper inflate() of the functions from the template
- Modify parse_fun() to read properly the name of the function with linebreaks and comments

Issue #142
FlorenceMounier added a commit that referenced this issue Nov 28, 2022
tags : fix

Why?
a flat file may contain empty chunks labelled as "function"

What?
return NA when no function is recognized

Issue #142
FlorenceMounier added a commit that referenced this issue Nov 28, 2022
@FlorenceMounier FlorenceMounier linked a pull request Nov 28, 2022 that will close this issue
16 tasks
FlorenceMounier added a commit that referenced this issue Nov 30, 2022
tags: test, fix

Why?
- test if examples and tests are condidered properly with linebreaks after function name

What?
- add examples and tests in two functions from the template, with no @, with @export, with @nord because it changes the outputs of inflate()
- add tests in test-inflate-part2.R for /man and /test

in progress: the examples are not in the right place in the .R files

Issue #142
FlorenceMounier added a commit that referenced this issue Dec 12, 2022
tags: tests

Why?
make sure examples are present/absent when expected with all the different syntaxes from dev-template-tests.Rmd

What?
check the presence in the right place or absence of examples  for some functions from dev-template-tests.Rmd

Issue #142
FlorenceMounier added a commit that referenced this issue Dec 12, 2022
tags: test, fix

Why?
tests failed for the place of examples in .R

What?
try to fix how example_pos_start and example_pos_end are computed from parse_fun(), they are then used in add_fun_code_examples()

in progress: example is fixed for 2nd and 3rd function but not for 1st with no

Issue #142
FlorenceMounier added a commit that referenced this issue Dec 13, 2022
Why?
tests for R6 functions failed

What?
take acount of @nord addition in the code to define positions of function name and examples

in progress: now there is a problem with .onLoad function in template, its title is duplicated

Issue #142
statnmap added a commit that referenced this issue Feb 18, 2023
tags: feat, test

Why?

- Dealing with multi-lines functions writing is complex. A another way of exploration is to use `parse()` to get rid of comments and extra line breaks, which reduces the number of cases to deal with.

What?

- Get back to original version of `parse_fun()`.
- Use `parse(text = code)` to deal with first function and extract fun_name
- This breaks the "empty" chunks tests examples but this should be easier to deal with.

issue #142
@statnmap statnmap self-assigned this Feb 18, 2023
statnmap added a commit that referenced this issue Feb 19, 2023
tags: fix, test

Why?

- New way of dealing with multilines broke the deal with empty chunks and data chunks

What?

- Change the order of if() to account for special cases before function cases

issue #142
@statnmap
Copy link
Member

That should be good now.
Merged in: #186
Please tell us if your face again the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants