-
Notifications
You must be signed in to change notification settings - Fork 44
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
Fix up PPI::Token::insert_{after,before}. #79
base: master
Are you sure you want to change the base?
Conversation
I stumbled across this while trying to make Dist::Zilla::Plugin::PkgVersion work better with perlcritic. More info here: rjbs/Dist-Zilla#168 The comments for PPI::Token::insert_{after,before} say that they may only insert statements, but the code inexplicably requires PPI::Structures. I think that requiring a Statement is appropriate the right thing, this commit makes the code match the comment. The tests in t still pass. I'll submit a pull request and hope for feedback from folks wise.
On further study I see that the code does not match the comment in another sense: the comment would require a "non-significant" toekn but the code doesn't check for significance. Changing the elseif to this: } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) { causes t/ppi_element.t to fail thusly.
Here's the failing test: INSERT_AFTER: {
my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $string = $Document->find_first('Token::Quote');
isa_ok( $string, 'PPI::Token::Quote' );
is( $string->content, "'Hello World'", 'Got expected token' );
my $foo = PPI::Token::Word->new('foo');
isa_ok( $foo, 'PPI::Token::Word' );
is( $foo->content, 'foo', 'Created Word token' );
$string->insert_after( $foo );
is( $Document->serialize, "print 'Hello World'foo;",
'insert_after actually inserts' );
} The element stored into $foo is signficant, arguably it doesn't make sense to be allowed to insert it there. An insignificant Element (e.g. some whitespace) seems allowable though. Feedback? |
dcc718c
to
04bd318
Compare
Sorry for taking so long to get back to this. I'm tempted to include the PR as it is for 1.222, however looking over the code i find myself confused and have created issue #93 to try and clear up some questions about it. If @adamkennedy doesn't answer in the next week i will include this PR for 1.222, and if no answer is forthcoming later on, i will simply remove these restrictions. |
I'll find some time to day to check into this, it's possible the comments Nesting rules say that statements can't have a statement as a direct child, We need to make sure that remains enforced. Adam
|
After dumpering a bit it seems to make sense. Do i understand it right that the rules are like this, and the behavior of the insert functions follows from these?
If i understand that right, then the best fix would be to ensure that behavior is always enforced and more importantly, that this is documented usefully, with notes pointing out that one might need to insert after/before the parent instead of the element one is working on. |
Upon further reading, i see this is documented here: https://metacpan.org/pod/PPI#The-Document-Statement-and-Structure The wording is a bit ambiguous for my taste, but the insert method documentation absolutely should point there and explain things. |
Is be happy to have the wording cleaned up, I have too deep an This is probably one of them. Adam
|
Thanks Adam, i've amended #93 to make it a proper bug ticket, pointing at this to explain what needs to be done. So for this ticket then there remains only the task of figuring out whether the insert code in Token is correct with respect to its comment. |
370d081
to
d3a2287
Compare
07cd2bb
to
46e810f
Compare
ecfa93a
to
9532062
Compare
4c1a78c
to
947502d
Compare
I stumbled across this while trying to make
Dist::Zilla::Plugin::PkgVersion work better with perlcritic. More
info here.
The comments for PPI::Token::insert_{after,before} say that they may
only insert statements, but the code inexplicably requires
PPI::Structures.
Requiring a Statement a) seems reasonable to me (but...) and b) makes my use case easier.
This commit makes the code match the comment.
The tests in t still pass.
I'm hoping for feedback from folks wise.
PPI::Statement's code and comment require that a Statement be inserted. PPI::Structure's code and comments require a Structure.
PPI::Token is the only one who's code and comments don't match. It's possible that a better fix is to fix the comment.