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

Add text argument to lint #503

Merged
merged 29 commits into from
Apr 4, 2021
Merged

Add text argument to lint #503

merged 29 commits into from
Apr 4, 2021

Conversation

renkun-ken
Copy link
Collaborator

@renkun-ken renkun-ken commented Jun 25, 2020

Close #475

This PR adds a text= argument to lintr::lint so that user could lint a file with supplied text as if the file is being linted. This is particularly useful for code editors to lively lint a file being edited but not saved while keeping the .lintr config (if found) effective.

  • Add text= argument to lintr::lint
  • Test cases

@AshesITR
Copy link
Collaborator

AshesITR commented Nov 1, 2020

If filename contains a newline, it's treated as source code.
I like an explicit text argument more, but I think we should remove the implicit and undocumented inline_data feature if there is a proper text argument.

https://github.com/jimhester/lintr/blob/42b49c1ba6d74a4910419d7ef78df804914d2b4e/R/lint.R#L30-L36

@MichaelChirico
Copy link
Collaborator

@renkun-ken is this ready for review? if so please help to merge to HEAD 😄

@MichaelChirico MichaelChirico added this to the 3.0.0 milestone Jan 31, 2021
@AshesITR AshesITR linked an issue Feb 8, 2021 that may be closed by this pull request
@AshesITR AshesITR linked an issue Feb 15, 2021 that may be closed by this pull request
@renkun-ken
Copy link
Collaborator Author

renkun-ken commented Mar 29, 2021

I update this PR to latest master.

If filename contains a newline, it's treated as source code.
I like an explicit text argument more, but I think we should remove the implicit and undocumented inline_data feature if there is a proper text argument.

https://github.com/jimhester/lintr/blob/42b49c1ba6d74a4910419d7ef78df804914d2b4e/R/lint.R#L30-L36

I guess we might need to keep it that way for a while and might give a warning in the future if filename is detected to be used as inline code. In languageserver, we use this filename as code for quite a while.

Now the behavior is:

  1. If filename has a \n then it is regarded as code and filename becomes tempfile().
  2. If text is supplied, change filename to tempfile() if filename is missing, then lint text as if it is from filename.

If you think this behavior makes sense, then I'll update some test cases to fix the tests.

@renkun-ken
Copy link
Collaborator Author

I test the latest commit with languageserver using the text= argument, the linters that rely on filename (e.g. object_usage_linter) work now.

@renkun-ken
Copy link
Collaborator Author

@renkun-ken is this ready for review? if so please help to merge to HEAD 😄

Yes, please. This PR is ready for review and merge now.

@MichaelChirico
Copy link
Collaborator

please remove .DS_Store

R/lint.R Outdated Show resolved Hide resolved
R/lint.R Outdated Show resolved Hide resolved
R/lint.R Outdated Show resolved Hide resolved
R/lint.R Outdated
#' @param text Optional argument for supplying a string or lines or lines directly,
#' e.g. if the file is already in memory or linting is being done ad hoc.
#' If \code{text} is supplied but \code{filename} is missing, then \code{tempfile()}
#' will be used as the \code{filename}.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like <text> will be used as the file name, this reads like the output would have the tempfile path instead... could you clarify what we mean here?

Copy link
Collaborator Author

@renkun-ken renkun-ken Apr 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed a bit ambiguous here. Here I mean that if text is provided but filename is not, then we'll write the text to a temp file (same behavior as before when filename is detected to be code), but the lint result will show as <text> which I believe should be the desired output rather than showing the tempfile in the lint result.

Looks like I should update this doc here, maybe just removing the text without filename case in the doc looks enough since writing the text to a file is just an internal implementation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a string or lines or lines directly looks a bit redundant?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. Removed the last sentence

@MichaelChirico
Copy link
Collaborator

almost there, thanks!

MichaelChirico
MichaelChirico previously approved these changes Apr 4, 2021
@@ -119,6 +119,7 @@ test_that("lint() results from file or text should be consistent", {
"x<-1",
"x+1"
)
on.exit(unlink(file))
Copy link
Collaborator Author

@renkun-ken renkun-ken Apr 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't use on.exit() here because I want to ensure that lintr does not assume that the file exists when linting with both filename and text supplied. Therefore, I remove the file before linting so that if lint reads the file somehow it would fail as it should, which is the point of lint_from_text2.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, please revert

@MichaelChirico MichaelChirico merged commit a5d2c77 into master Apr 4, 2021
@MichaelChirico MichaelChirico deleted the lint-text branch April 4, 2021 06:29
@renkun-ken
Copy link
Collaborator Author

Thanks for the review!

@renkun-ken
Copy link
Collaborator Author

Looks like we forgot to add a news item. I'll create a quick PR for this soon.

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