Add way for caller using interpolate option to find out whether we interpolated,...
authorNick Downing <nick@ndcode.org>
Wed, 12 Jan 2022 14:00:38 +0000 (01:00 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 12 Jan 2022 14:16:22 +0000 (01:16 +1100)
lib/minify.js
lib/output.js

index b8f0204..3cba903 100644 (file)
@@ -227,6 +227,8 @@ function minify(files, options) {
             var stream = OutputStream(output);
             toplevel.print(stream);
             result.code = stream.get();
+            if (stream.interpolate()) // Nick
+              result.interpolated = stream.get_interpolated()
             if (options.sourceMap) {
                 result.map = output.source_map.toString();
                 var url = options.sourceMap.url;
index 7c52e95..76cdf0a 100644 (file)
@@ -120,6 +120,7 @@ function OutputStream(options) {
     var newline_insert = -1;
     var stack;
     var OUTPUT;
+    var interpolated; // Nick
 
     function reset() {
         last = "";
@@ -128,6 +129,7 @@ function OutputStream(options) {
         stack = [];
         var str = OUTPUT;
         OUTPUT = "";
+        interpolated = false; // Nick
         return str;
     }
 
@@ -618,7 +620,13 @@ function OutputStream(options) {
         // Nick
         interpolate     : function() {
             return options.interpolate
-        }
+        },
+        get_interpolated : function() {
+            return interpolated
+        },
+        set_interpolated : function(value) {
+            interpolated = value
+        },
     };
 }
 
@@ -1769,8 +1777,8 @@ function OutputStream(options) {
         if (self.tag) self.tag.print(output);
         output.print("`");
         for (var i = 0; i < self.expressions.length; i++) {
-            if (output.interpolate) { // Nick
-                output.print(self.strings[i].replace("\\", "\\\\").replace("${", "\\${"));
+            if (output.interpolate()) { // Nick
+                output.print(self.strings[i].replaceAll("\\", "\\\\").replaceAll("${", "\\${"));
                 output.print("\\${");
             }
             else {
@@ -1780,8 +1788,8 @@ function OutputStream(options) {
             self.expressions[i].print(output);
             output.print("}");
         }
-        if (output.interpolate) // Nick
-            output.print(self.strings[i].replace("\\", "\\\\").replace("${", "\\${"));
+        if (output.interpolate()) // Nick
+            output.print(self.strings[i].replaceAll("\\", "\\\\").replaceAll("${", "\\${"));
         else
             output.print(self.strings[i]);
         output.print("`");
@@ -1845,11 +1853,10 @@ function OutputStream(options) {
 
     // Nick
     DEFPRINT(AST_Interpolate, function(output) {
-        //if (!output.interpolate()) // Nick
-        //    throw new Error("AST_Interpolate with interpolation turned off")
-        output.print("(${");
+        output.print("${");
         this.expression.print(output);
-        output.print("})");
+        output.print("}");
+        output.set_interpolated(true);
     });
 
     function force_statement(stat, output) {