-
Notifications
You must be signed in to change notification settings - Fork 148
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
[BUGFIX] Fix comment parsing to support multiple comments #663
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -220,18 +220,26 @@ public function parseCharacter($bIsForIdentifier) | |||||
} | ||||||
|
||||||
/** | ||||||
* @return array<int, Comment>|void | ||||||
* Consumes whitespace and comments and returns the comments. | ||||||
* If $withoutComment is true, only whitespace is consumed. | ||||||
* | ||||||
* @param bool $withoutComment Do not consume comments, only whitespace. | ||||||
* | ||||||
* @return array<int, Comment>|void List of comments. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can be even more specific (and omit the void return type, which is incorrect for this method and also is not allowed in union types):
Suggested change
|
||||||
* | ||||||
* @throws UnexpectedEOFException | ||||||
* @throws UnexpectedTokenException | ||||||
*/ | ||||||
public function consumeWhiteSpace(): array | ||||||
public function consumeWhiteSpace(bool $withoutComment = false): array | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a big fan of boolean function parameters that switch behavior. Instead, please let's have a separate, well-named method, e.g., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can then also move the new method to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would go even further and rename the current function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this would probably be a breaking change. So, I introduced the boolean function parameter to stay within the current wording (which I deem wrong) and still make the change non-breaking. I would consider having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of renaming this method! Would you be willing to do this as a (or more specific), two pre-patch PRs?
I'd be willing to review these two pre-PRs quickly so we can rebase on them. We also might want to mark the renamed method as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not a big fan of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, it seems I misread your comment and got confused. 🤯 Okay, so I'm fine with this plan:
We'll need to do this in a non-breaking way for the parts that we backport, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've thought about this some more and now think having it in |
||||||
{ | ||||||
$aComments = []; | ||||||
do { | ||||||
while (\preg_match('/\\s/isSu', $this->peek()) === 1) { | ||||||
$this->consume(1); | ||||||
} | ||||||
if ($withoutComment) { | ||||||
break; | ||||||
} | ||||||
if ($this->oParserSettings->bLenientParsing) { | ||||||
try { | ||||||
$oComment = $this->consumeComment(); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1161,13 +1161,16 @@ public function commentExtracting(): void | |
*/ | ||
public function flatCommentExtracting(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're planning to split the tests and testcases into smaller, more focused tests. To make this easier for us, please don't expand the test. Instead, could you please duplicate it into two separate tests, one with a single comment and one with multiple? Thanks! |
||
{ | ||
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}'); | ||
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}'); | ||
$doc = $parser->parse(); | ||
$contents = $doc->getContents(); | ||
$divRules = $contents[0]->getRules(); | ||
$comments = $divRules[0]->getComments(); | ||
self::assertCount(1, $comments); | ||
self::assertSame('Find Me!', $comments[0]->getComment()); | ||
$rule1Comments = $divRules[0]->getComments(); | ||
$rule2Comments = $divRules[1]->getComments(); | ||
self::assertCount(1, $rule1Comments); | ||
self::assertCount(1, $rule2Comments); | ||
self::assertEquals('Find Me!', $rule1Comments[0]->getComment()); | ||
self::assertEquals('Find Me Too!', $rule2Comments[0]->getComment()); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put code in comments in backticks so it gets displayed as code.