From 2c179f3db7ec10943d088ceaf2d7c1e605079df1 Mon Sep 17 00:00:00 2001
From: Anto Aravinth <anto.aravinth.cse@gmail.com>
Date: Sun, 18 Nov 2018 09:07:48 +0530
Subject: [PATCH] repl: handle buffered string logic on finish

---
 lib/readline.js | 6 +++---
 lib/repl.js     | 8 +++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/lib/readline.js b/lib/readline.js
index f22d84f1a0df77..b293292d83770e 100644
--- a/lib/readline.js
+++ b/lib/readline.js
@@ -348,9 +348,9 @@ Interface.prototype.undoHistory = function() {
 
 // If it's a multiline code, then add history
 // accordingly.
-Interface.prototype.multilineHistory = function() {
-  // check if we got a multiline code
-  if (this.multiline !== '' && this.terminal) {
+Interface.prototype.multilineHistory = function(clearBuffer) {
+  // if not clear buffer, add multiline history
+  if (!clearBuffer && this.terminal) {
     const dupIndex = this.history.indexOf(this.multiline);
     if (dupIndex !== -1) this.history.splice(dupIndex, 1);
     // Remove the last entered line as multiline
diff --git a/lib/repl.js b/lib/repl.js
index 3391a94396db11..030cb633d3dc57 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -668,6 +668,13 @@ function REPLServer(prompt,
         }
       }
 
+      // handle multiline history
+      if (self[kBufferedCommandSymbol].length)
+        REPLServer.super_.prototype.multilineHistory.call(self, false);
+      else {
+        REPLServer.super_.prototype.multilineHistory.call(self, true);
+      }
+
       // Clear buffer if no SyntaxErrors
       self.clearBufferedCommand();
       sawCtrlD = false;
@@ -774,7 +781,6 @@ exports.start = function(prompt,
 
 REPLServer.prototype.clearBufferedCommand = function clearBufferedCommand() {
   this[kBufferedCommandSymbol] = '';
-  REPLServer.super_.prototype.multilineHistory.call(this);
 };
 
 REPLServer.prototype.close = function close() {