Skip to content

Commit

Permalink
Merge pull request #125 from tedious/keyword_detection_bug
Browse files Browse the repository at this point in the history
Fix bug in keyword detections
  • Loading branch information
tedivm committed Mar 9, 2023
2 parents 0393388 + d9c2bfa commit 439459e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/JShrink/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Resources/jshrink/input/regex_detection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function test () {
return this.indeterminate
? 100
: (100 * (this.options.value - this.min)) / (this.options.max - this.min)
}
3 changes: 3 additions & 0 deletions tests/Resources/jshrink/input/regex_division.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (D > (F/2)) {
console.log('a')
}
1 change: 1 addition & 0 deletions tests/Resources/jshrink/input/regex_keyword_similarity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var value = currentRating/other
1 change: 1 addition & 0 deletions tests/Resources/jshrink/output/regex_detection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function test(){return this.indeterminate?100:(100*(this.options.value-this.min))/(this.options.max-this.min)}
1 change: 1 addition & 0 deletions tests/Resources/jshrink/output/regex_division.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if(D>(F/2)){console.log('a')}
1 change: 1 addition & 0 deletions tests/Resources/jshrink/output/regex_keyword_similarity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var value=currentRating/other

0 comments on commit 439459e

Please sign in to comment.