Skip to content

Fixed - was not working for variables in last line of heredoc. #14

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

Merged
merged 1 commit into from
Mar 19, 2014

Conversation

vsespb
Copy link
Contributor

@vsespb vsespb commented Mar 18, 2014

It seems that heredoc() method excludes terminator line, so no need to pop()
https://metacpan.org/pod/PPI::Token::HereDoc#heredoc

added test for the bug, also added additional test to extract_variables (just
to make sure our regexp deals with newlines right)

It seems that heredoc() method excludes terminator line, so no need to pop()
https://metacpan.org/pod/PPI::Token::HereDoc#heredoc

added test for the bug, also added additional test to extract_variables (just
to make sure our regexp deals with newlines right)
@guillaumeaubert
Copy link
Owner

Hi Victor,

Thank you for your pull request, and in particular for including a test :)

You found a really interesting bug in how PPI's tokenizer handles heredoc blocks. Due to this bug, quoted heredoc blocks (such as <<"HERE" or <<'HERE') include the terminator line in the output of ->heredoc():

$VAR1 = [
          '     SELECT $field_name
',
          '     FROM $table
',
          '\'HERE\'
'
        ];

But unquoted heredoc blocks (such as <<HERE) do not:

$VAR1 = [
          '     SELECT $field_name
',
          '     FROM $table
'
        ];

I must have been testing with quoted heredocs when I wrote the code, hence the original line I had there. I submitted a pull request to @adamkennedy to fix the underlying inconsistency and match the documentation: Perl-Critic/PPI#71.

In the meantime, I'm going to merge your pull request, and add a conditional on top to handle the inconsistency. Once the pull request is merged in PPI's repository, I will have to also add a test for PPI's version in that code, or require a minimum version of PPI.

guillaumeaubert added a commit that referenced this pull request Mar 19, 2014
Due to a bug in PPI::Token::Heredoc, $token->heredoc() does not include the
terminator line when the terminator is a simple bareword, but it includes the
terminator line in the other cases (single/double quoted , commands, escaped).

I wrote a patch to resolve this in PPI (see
Perl-Critic/PPI#71), but in the meantime this addresses
the inconsistency.
@guillaumeaubert guillaumeaubert merged commit 1dc0279 into guillaumeaubert:master Mar 19, 2014
@guillaumeaubert guillaumeaubert added this to the v1.3.0 milestone Mar 19, 2014
@vsespb
Copy link
Contributor Author

vsespb commented Mar 19, 2014

a bit strange. I've tried to add new test (and I commented out pop() for testing and removed all other tests):

## name somename
## failures 0
## cut

my $heredoc = <<'SELECT'.$x;

SELECT

and it seems heredoc contains "SELECT" in last element if the .run file ends with 0 or 1 newline. and does not contain "SELECT" if .run file ends with 2+ newlines or there are other statements after heredoc.

and this was not related to quoting of select.

vsespb added a commit to regru/Perl-Critic-Policy-ValuesAndExpressions-PreventSQLInjection that referenced this pull request Mar 19, 2014
guillaumeaubert added a commit that referenced this pull request Mar 20, 2014
This reverts commit 49b2c9b.

Upon further investigation, I found that the issue is not in PPI but in the
test cases in this module. I will commit a fix for those separately, but I am
reverting the PPI workaround and I have cancelled the PPI pull request
accordingly.
guillaumeaubert added a commit that referenced this pull request Mar 20, 2014
The closing terminator should not include quotes/backticks/etc, or it will not
be detected as a valid closing terminator and be included in the heredoc()
output instead, which is the real cause of the issues we've seen in GH-14.
@guillaumeaubert
Copy link
Owner

I investigated further and found the issue - your patch was indeed correct, and I reverted my later modification. The issue turned out to be in the test cases, which I fixed with eef8806.

@guillaumeaubert
Copy link
Owner

Regarding your new SELECT test case above - I can reproduce it, but only if there is no newline after SELECT. Can you confirm it breaks with 1 newline as well for you?

@vsespb
Copy link
Contributor Author

vsespb commented Mar 20, 2014

Can you confirm it breaks with 1 newline as well for you?

yes. I sent PR-17 as example. Also travis should confirm that it's failing.

It seems PPI problem indeed, but I think it's very low priority for your module. Affected:

  1. here doc is last line in file (so, it's not even a subroutine!)
  2. SELECT|UPDATE used as terminators
  3. it's false positive which is not so bad as false negative

other cases probably can include heredocs terminators with sigils, which is just waiting for trouble anyway.

@guillaumeaubert
Copy link
Owner

Thank you for opening GH-17 to track this PPI issue! I'm going to follow up there.

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 this pull request may close these issues.

2 participants