From 5ad186a109325fd8802ee24f0aaccc867e6e99ca Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 8 Nov 2017 13:45:05 +1100 Subject: [PATCH] Fixed bug #1718 : Unclosed strings at EOF sometimes tokenized as T_WHITESPACE by the JS tokenizer --- package.xml | 1 + src/Tokenizers/JS.php | 37 ++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/package.xml b/package.xml index 13823922c2..220bf415ad 100644 --- a/package.xml +++ b/package.xml @@ -75,6 +75,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- Override the 'requiredSpacesBeforeColon' setting in a ruleset.xml file to change -- Default remains at 1 -- Thanks to Nikola Kovacs for the patch + - Fixed bug #1718 : Unclosed strings at EOF sometimes tokenized as T_WHITESPACE by the JS tokenizer - Fixed bug #1731 : Directory exclusions do not work as expected when a single file name is passed to phpcs diff --git a/src/Tokenizers/JS.php b/src/Tokenizers/JS.php index 2ac27c5e49..a8dfe04e3d 100644 --- a/src/Tokenizers/JS.php +++ b/src/Tokenizers/JS.php @@ -709,18 +709,33 @@ public function tokenize($string) }//end for if (empty($buffer) === false) { - // Buffer contains whitespace from the end of the file. - $tokens[] = array( - 'code' => T_WHITESPACE, - 'type' => 'T_WHITESPACE', - 'content' => str_replace("\n", $this->eolChar, $buffer), - ); + if ($inString !== '') { + // The string did not end before the end of the file, + // which means there was probably a syntax error somewhere. + $tokens[] = array( + 'code' => T_STRING, + 'type' => 'T_STRING', + 'content' => str_replace("\n", $this->eolChar, $buffer), + ); - if (PHP_CODESNIFFER_VERBOSITY > 1) { - $content = Util\Common::prepareForOutput($buffer); - echo "\t=> Added token T_WHITESPACE ($content)".PHP_EOL; - } - } + if (PHP_CODESNIFFER_VERBOSITY > 1) { + $content = Util\Common::prepareForOutput($buffer); + echo "\t=> Added token T_STRING ($content)".PHP_EOL; + } + } else { + // Buffer contains whitespace from the end of the file. + $tokens[] = array( + 'code' => T_WHITESPACE, + 'type' => 'T_WHITESPACE', + 'content' => str_replace("\n", $this->eolChar, $buffer), + ); + + if (PHP_CODESNIFFER_VERBOSITY > 1) { + $content = Util\Common::prepareForOutput($buffer); + echo "\t=> Added token T_WHITESPACE ($content)".PHP_EOL; + } + }//end if + }//end if $tokens[] = array( 'code' => T_CLOSE_TAG,