Skip to content

Add text argument to lint - #503

Merged
MichaelChirico merged 29 commits into
masterfrom
lint-text
Apr 4, 2021
Merged

MichaelChirico merged 29 commits into
masterfrom
lint-text

Conversation

@renkun-ken

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

Copy link
Copy Markdown
Collaborator

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

@renkun-ken
renkun-ken requested a review from jimhester as a code owner June 25, 2020 02:52
@AshesITR

AshesITR commented Nov 1, 2020

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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

renkun-ken commented Mar 29, 2021 •

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

please remove .DS_Store

Comment thread R/lint.R Resolved
Comment thread R/lint.R Resolved
Comment thread R/lint.R Resolved
Comment thread R/lint.R Resolved
Comment thread 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
Copy Markdown
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?

@renkun-ken renkun-ken Apr 4, 2021 •

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

almost there, thanks!

MichaelChirico
MichaelChirico previously approved these changes Apr 4, 2021
Comment thread tests/testthat/test-lint_file.R Outdated
"x<-1",
"x+1"
)
on.exit(unlink(file))

@renkun-ken renkun-ken Apr 4, 2021 •

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
Collaborator Author

Thanks for the review!

@renkun-ken

Copy link
Copy Markdown
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