From d9c2bfa4623526a4ca3ef0157fab9178ccfeda93 Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Thu, 9 Mar 2023 11:11:20 -0600 Subject: [PATCH] Fix bug in keyword detections Resolves #123 --- src/JShrink/Minifier.php | 17 ++++++++++++++--- .../Resources/jshrink/input/regex_detection.js | 5 +++++ tests/Resources/jshrink/input/regex_division.js | 3 +++ .../jshrink/input/regex_keyword_similarity.js | 1 + .../Resources/jshrink/output/regex_detection.js | 1 + .../Resources/jshrink/output/regex_division.js | 1 + .../jshrink/output/regex_keyword_similarity.js | 1 + 7 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/Resources/jshrink/input/regex_detection.js create mode 100644 tests/Resources/jshrink/input/regex_division.js create mode 100644 tests/Resources/jshrink/input/regex_keyword_similarity.js create mode 100644 tests/Resources/jshrink/output/regex_detection.js create mode 100644 tests/Resources/jshrink/output/regex_division.js create mode 100644 tests/Resources/jshrink/output/regex_keyword_similarity.js diff --git a/src/JShrink/Minifier.php b/src/JShrink/Minifier.php index 10b035f..4986c53 100644 --- a/src/JShrink/Minifier.php +++ b/src/JShrink/Minifier.php @@ -285,7 +285,14 @@ protected function loop() if ($this->b == '/') { $valid_tokens = "(,=:[!&|?\n"; - if (strpos($valid_tokens, $this->last_char) !== false || strpos($valid_tokens, $this->a) !== false) { + + # Find last "real" token, excluding spaces. + $last_token = $this->a; + if ($last_token == " ") { + $last_token = $this->last_char; + } + + if (strpos($valid_tokens, $last_token) !== false) { // Regex can appear unquoted after these symbols $this->saveRegex(); } else if ($this->endsInKeyword()) { @@ -644,11 +651,15 @@ protected static function isAlphaNumeric($char) } protected function endsInKeyword() { + + # When this function is called A is not yet assigned to output. + $testOutput = $this->output . $this->a; + foreach(static::$keywords as $keyword) { - if (str_ends_with($this->output, $keyword)) { + if (str_ends_with($testOutput, $keyword)) { return true; } - if (str_ends_with($this->output, $keyword . " ")) { + if (str_ends_with($testOutput, $keyword . " ")) { return true; } } diff --git a/tests/Resources/jshrink/input/regex_detection.js b/tests/Resources/jshrink/input/regex_detection.js new file mode 100644 index 0000000..6833207 --- /dev/null +++ b/tests/Resources/jshrink/input/regex_detection.js @@ -0,0 +1,5 @@ +function test () { + return this.indeterminate + ? 100 + : (100 * (this.options.value - this.min)) / (this.options.max - this.min) +} diff --git a/tests/Resources/jshrink/input/regex_division.js b/tests/Resources/jshrink/input/regex_division.js new file mode 100644 index 0000000..6d503e7 --- /dev/null +++ b/tests/Resources/jshrink/input/regex_division.js @@ -0,0 +1,3 @@ +if (D > (F/2)) { + console.log('a') +} diff --git a/tests/Resources/jshrink/input/regex_keyword_similarity.js b/tests/Resources/jshrink/input/regex_keyword_similarity.js new file mode 100644 index 0000000..9f79ba7 --- /dev/null +++ b/tests/Resources/jshrink/input/regex_keyword_similarity.js @@ -0,0 +1 @@ +var value = currentRating/other \ No newline at end of file diff --git a/tests/Resources/jshrink/output/regex_detection.js b/tests/Resources/jshrink/output/regex_detection.js new file mode 100644 index 0000000..be38d46 --- /dev/null +++ b/tests/Resources/jshrink/output/regex_detection.js @@ -0,0 +1 @@ +function test(){return this.indeterminate?100:(100*(this.options.value-this.min))/(this.options.max-this.min)} \ No newline at end of file diff --git a/tests/Resources/jshrink/output/regex_division.js b/tests/Resources/jshrink/output/regex_division.js new file mode 100644 index 0000000..70fa287 --- /dev/null +++ b/tests/Resources/jshrink/output/regex_division.js @@ -0,0 +1 @@ +if(D>(F/2)){console.log('a')} \ No newline at end of file diff --git a/tests/Resources/jshrink/output/regex_keyword_similarity.js b/tests/Resources/jshrink/output/regex_keyword_similarity.js new file mode 100644 index 0000000..24796c2 --- /dev/null +++ b/tests/Resources/jshrink/output/regex_keyword_similarity.js @@ -0,0 +1 @@ +var value=currentRating/other \ No newline at end of file