From 84e3cff6c49192c340b6bb97f28a3935e0c732cb Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Tue, 19 Jul 2016 18:01:42 -0400 Subject: [PATCH] Preserve ES6 template literals We don't bother trying to remove whitespace in embedded expressions. - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals - http://es6-features.org/#CustomInterpolation --- src/JSMin/JSMin.php | 9 ++++++--- tests/Resources/minify/expected/es6-literal.js | 2 ++ tests/Resources/minify/input/es6-literal.js | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 tests/Resources/minify/expected/es6-literal.js create mode 100644 tests/Resources/minify/input/es6-literal.js diff --git a/src/JSMin/JSMin.php b/src/JSMin/JSMin.php index 4fc320b..bbf7c50 100644 --- a/src/JSMin/JSMin.php +++ b/src/JSMin/JSMin.php @@ -196,7 +196,8 @@ protected function action($command) // fallthrough intentional case self::ACTION_DELETE_A: // 2 $this->a = $this->b; - if ($this->a === "'" || $this->a === '"') { // string literal + if ($this->a === "'" || $this->a === '"' || $this->a === '`') { // string/template literal + $delimiter = $this->a; $str = $this->a; // in case needed for exception for(;;) { $this->output .= $this->a; @@ -206,7 +207,9 @@ protected function action($command) if ($this->a === $this->b) { // end quote break; } - if ($this->isEOF($this->a)) { + if ($delimiter === '`' && $this->a === "\n") { + // leave the newline + } elseif ($this->isEOF($this->a)) { $byte = $this->inputIndex - 1; throw new UnterminatedStringException( "JSMin: Unterminated String at byte {$byte}: {$str}"); @@ -216,7 +219,7 @@ protected function action($command) $this->output .= $this->a; $this->lastByteOut = $this->a; - $this->a = $this->get(); + $this->a = $this->get(); $str .= $this->a; } } diff --git a/tests/Resources/minify/expected/es6-literal.js b/tests/Resources/minify/expected/es6-literal.js new file mode 100644 index 0000000..79d0a1f --- /dev/null +++ b/tests/Resources/minify/expected/es6-literal.js @@ -0,0 +1,2 @@ +`line +break`+`he llo`;foo`hel( '');lo`;`he\nl\`lo`;(`he${one + two}`) \ No newline at end of file diff --git a/tests/Resources/minify/input/es6-literal.js b/tests/Resources/minify/input/es6-literal.js new file mode 100644 index 0000000..57aea26 --- /dev/null +++ b/tests/Resources/minify/input/es6-literal.js @@ -0,0 +1,2 @@ +`line +break` + `he llo`; foo`hel( '');lo`; `he\nl\`lo`; (`he${one + two}`)