@@ -87,6 +87,8 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
87
87
self . useGlobal = ! ! useGlobal ;
88
88
self . ignoreUndefined = ! ! ignoreUndefined ;
89
89
90
+ self . _inTemplateLiteral = false ;
91
+
90
92
// just for backwards compat, see github.com/joyent/node/pull/7127
91
93
self . rli = this ;
92
94
@@ -102,7 +104,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
102
104
} ) ;
103
105
} catch ( e ) {
104
106
debug ( 'parse error %j' , code , e ) ;
105
- if ( isRecoverableError ( e ) )
107
+ if ( isRecoverableError ( e , self ) )
106
108
err = new Recoverable ( e ) ;
107
109
else
108
110
err = e ;
@@ -226,7 +228,13 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
226
228
debug ( 'line %j' , cmd ) ;
227
229
sawSIGINT = false ;
228
230
var skipCatchall = false ;
229
- cmd = trimWhitespace ( cmd ) ;
231
+
232
+ // leading whitespaces in template literals should not be trimmed.
233
+ if ( self . _inTemplateLiteral ) {
234
+ self . _inTemplateLiteral = false ;
235
+ } else {
236
+ cmd = trimWhitespace ( cmd ) ;
237
+ }
230
238
231
239
// Check to see if a REPL keyword was used. If it returns true,
232
240
// display next prompt and return.
@@ -928,10 +936,17 @@ REPLServer.prototype.convertToContext = function(cmd) {
928
936
929
937
// If the error is that we've unexpectedly ended the input,
930
938
// then let the user try to recover by adding more input.
931
- function isRecoverableError ( e ) {
932
- return e &&
933
- e . name === 'SyntaxError' &&
934
- / ^ ( U n e x p e c t e d e n d o f i n p u t | U n e x p e c t e d t o k e n : ) / . test ( e . message ) ;
939
+ function isRecoverableError ( e , self ) {
940
+ if ( e && e . name === 'SyntaxError' ) {
941
+ var message = e . message ;
942
+ if ( message === 'Unterminated template literal' ||
943
+ message === 'Missing } in template expression' ) {
944
+ self . _inTemplateLiteral = true ;
945
+ return true ;
946
+ }
947
+ return / ^ ( U n e x p e c t e d e n d o f i n p u t | U n e x p e c t e d t o k e n : ) / . test ( message ) ;
948
+ }
949
+ return false ;
935
950
}
936
951
937
952
function Recoverable ( err ) {
0 commit comments