From c6a863383a9fc0e326e2807eecfdeddacca2a614 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 3 May 2024 15:05:35 -0300 Subject: [PATCH 1/2] Generic/InlineControlStructure: rename JS test case file Doing this to be able to create tests with syntax errors on separate files. --- ...StructureUnitTest.js => InlineControlStructureUnitTest.1.js} | 0 ...tTest.js.fixed => InlineControlStructureUnitTest.1.js.fixed} | 0 .../Tests/ControlStructures/InlineControlStructureUnitTest.php | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename src/Standards/Generic/Tests/ControlStructures/{InlineControlStructureUnitTest.js => InlineControlStructureUnitTest.1.js} (100%) rename src/Standards/Generic/Tests/ControlStructures/{InlineControlStructureUnitTest.js.fixed => InlineControlStructureUnitTest.1.js.fixed} (100%) diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js similarity index 100% rename from src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js rename to src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js.fixed b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js.fixed similarity index 100% rename from src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js.fixed rename to src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js.fixed diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php index 5d42d1122a..afcaa3a335 100644 --- a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php @@ -81,7 +81,7 @@ public function getErrorList($testFile='') 278 => 1, ]; - case 'InlineControlStructureUnitTest.js': + case 'InlineControlStructureUnitTest.1.js': return [ 3 => 1, 7 => 1, From bc6ff825a558ab8b42330479e7c2190e27a7bde0 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 3 May 2024 15:15:52 -0300 Subject: [PATCH 2/2] Generic/InlineControlStructure: remove redundant condition 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. --- .../InlineControlStructureSniff.php | 15 --------------- .../InlineControlStructureUnitTest.2.js | 5 +++++ .../InlineControlStructureUnitTest.3.js | 5 +++++ 3 files changed, 10 insertions(+), 15 deletions(-) create mode 100644 src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.2.js create mode 100644 src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.3.js diff --git a/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php index bc4a916d20..ff1738328f 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php @@ -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 diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.2.js b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.2.js new file mode 100644 index 0000000000..e26c331270 --- /dev/null +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.2.js @@ -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 diff --git a/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.3.js b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.3.js new file mode 100644 index 0000000000..9ccedcda33 --- /dev/null +++ b/src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.3.js @@ -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