Skip to content

Commit fbea319

Browse files
committed
Add support for braceless do-while loops in Javascript.
1 parent dee5d3e commit fbea319

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

CodeSniffer/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
106106
}
107107
}
108108
}
109+
110+
// In Javascript DO WHILE loops without curly braces are legal. This
111+
// is only valid if a single statement is present between the DO and
112+
// the WHILE. We can detect this by checking only a single semicolon
113+
// is present between them.
114+
if ($phpcsFile->tokenizerType === 'JS') {
115+
$lastDo = $phpcsFile->findPrevious(T_DO, $stackPtr - 1);
116+
$lastSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, $stackPtr - 1);
117+
if ($lastDo !== false && $lastSemicolon !== false && $lastDo < $lastSemicolon) {
118+
$precedingSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, $lastSemicolon - 1);
119+
if ($precedingSemicolon === false || $precedingSemicolon < $lastDo) {
120+
return;
121+
}
122+
}
123+
}
109124
}
110125

111126
// This is a control structure without an opening brace,

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ while (something) print 'hello';
1717
do {
1818
i--;
1919
} while (something);
20+
21+
do i++; while (i < 5);

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ while (something) { print 'hello'; }
1717
do {
1818
i--;
1919
} while (something);
20+
21+
do { i++; } while (i < 5);

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
7171
11 => 1,
7272
13 => 1,
7373
15 => 1,
74+
21 => 1,
7475
);
7576
break;
7677
default:

0 commit comments

Comments
 (0)