Skip to content

Commit

Permalink
Generic/InlineControlStructure: remove redundant condition
Browse files Browse the repository at this point in the history
This commit removes a redundant condition that was added in fbea319 to
support JS braceless do-while loops. A subsequent commit added similar
code to support braceless do-while loops (plus for/while loops without
body) for PHP, but it also works for JS (13c803b).

There are a few syntax error cases that were handled by the code that is
removed by this commit and are not handled by the code introduced in
13c803b. Without the removed code, they are now handled in a if
condition right below. I added two tests with those cases to ensure the
sniff continues working as expected.
  • Loading branch information
rodrigoprimo authored and jrfnl committed Sep 30, 2024
1 parent 38b4646 commit 9168f53
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,6 @@ public function process(File $phpcsFile, $stackPtr)
return;
}
}

// In Javascript DO WHILE loops without curly braces are legal. This
// is only valid if a single statement is present between the DO and
// the WHILE. We can detect this by checking only a single semicolon
// is present between them.
if ($tokens[$stackPtr]['code'] === T_WHILE && $phpcsFile->tokenizerType === 'JS') {
$lastDo = $phpcsFile->findPrevious(T_DO, ($stackPtr - 1));
$lastSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1));
if ($lastDo !== false && $lastSemicolon !== false && $lastDo < $lastSemicolon) {
$precedingSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, ($lastSemicolon - 1));
if ($precedingSemicolon === false || $precedingSemicolon < $lastDo) {
return;
}
}
}
}//end if

if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Intentional parse error (missing closing parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

do i++; while (i < 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Intentional parse error (missing opening parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

do i++; while

0 comments on commit 9168f53

Please sign in to comment.