Skip to content

Commit

Permalink
Add support for braceless do-while loops in Javascript.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrenssen committed Sep 14, 2015
1 parent dee5d3e commit fbea319
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}
}

// 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 ($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;
}
}
}
}

// This is a control structure without an opening brace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ while (something) print 'hello';
do {
i--;
} while (something);

do i++; while (i < 5);
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ while (something) { print 'hello'; }
do {
i--;
} while (something);

do { i++; } while (i < 5);
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
11 => 1,
13 => 1,
15 => 1,
21 => 1,
);
break;
default:
Expand Down

0 comments on commit fbea319

Please # to comment.