1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fix repl.rli deprecation (#5178)

The `rli` property is just a reference to itself. It still exists
for legacy reasons but it will likely be removed in a future major
version. This makes sure everything works as expected.
This commit is contained in:
Ruben Bridgewater 2019-03-25 02:29:53 +01:00 committed by Geoffrey Booth
parent ec034e2673
commit 41b31c7293
4 changed files with 78 additions and 78 deletions

View file

@ -376,7 +376,7 @@ undeclared variable <code>__</code>.</p>
vm.runInContext js, context, filename
<span class="hljs-function">
<span class="hljs-title">addMultilineHandler</span> = <span class="hljs-params">(repl)</span> -&gt;</span>
{rli, inputStream, outputStream} = repl</pre></div></div>
{inputStream, outputStream} = repl</pre></div></div>
</li>
@ -412,15 +412,15 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> nodeLineListener = rli.listeners(<span class="hljs-string">'line'</span>)[<span class="hljs-number">0</span>]
rli.removeListener <span class="hljs-string">'line'</span>, nodeLineListener
rli.<span class="hljs-literal">on</span> <span class="hljs-string">'line'</span>, <span class="hljs-function"><span class="hljs-params">(cmd)</span> -&gt;</span>
<div class="content"><div class='highlight'><pre> nodeLineListener = repl.listeners(<span class="hljs-string">'line'</span>)[<span class="hljs-number">0</span>]
repl.removeListener <span class="hljs-string">'line'</span>, nodeLineListener
repl.<span class="hljs-literal">on</span> <span class="hljs-string">'line'</span>, <span class="hljs-function"><span class="hljs-params">(cmd)</span> -&gt;</span>
<span class="hljs-keyword">if</span> multiline.enabled
multiline.buffer += <span class="hljs-string">"<span class="hljs-subst">#{cmd}</span>\n"</span>
rli.setPrompt multiline.prompt
rli.prompt <span class="hljs-literal">true</span>
repl.setPrompt multiline.prompt
repl.prompt <span class="hljs-literal">true</span>
<span class="hljs-keyword">else</span>
rli.setPrompt origPrompt
repl.setPrompt origPrompt
nodeLineListener cmd
<span class="hljs-keyword">return</span></pre></div></div>
@ -456,8 +456,8 @@ undeclared variable <code>__</code>.</p>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> multiline.buffer.match <span class="hljs-regexp">/\n/</span>
multiline.enabled = <span class="hljs-keyword">not</span> multiline.enabled
rli.setPrompt origPrompt
rli.prompt <span class="hljs-literal">true</span>
repl.setPrompt origPrompt
repl.prompt <span class="hljs-literal">true</span>
<span class="hljs-keyword">return</span></pre></div></div>
</li>
@ -473,7 +473,7 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> rli.line? <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> rli.line.match <span class="hljs-regexp">/^\s*$/</span></pre></div></div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> repl.line? <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> repl.line.match <span class="hljs-regexp">/^\s*$/</span></pre></div></div>
</li>
@ -489,10 +489,10 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> multiline.enabled = <span class="hljs-keyword">not</span> multiline.enabled
rli.line = <span class="hljs-string">''</span>
rli.cursor = <span class="hljs-number">0</span>
rli.output.cursorTo <span class="hljs-number">0</span>
rli.output.clearLine <span class="hljs-number">1</span></pre></div></div>
repl.line = <span class="hljs-string">''</span>
repl.cursor = <span class="hljs-number">0</span>
repl.output.cursorTo <span class="hljs-number">0</span>
repl.output.clearLine <span class="hljs-number">1</span></pre></div></div>
</li>
@ -508,12 +508,12 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> multiline.buffer = multiline.buffer.replace <span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">'\uFF00'</span>
rli.emit <span class="hljs-string">'line'</span>, multiline.buffer
repl.emit <span class="hljs-string">'line'</span>, multiline.buffer
multiline.buffer = <span class="hljs-string">''</span>
<span class="hljs-keyword">else</span>
multiline.enabled = <span class="hljs-keyword">not</span> multiline.enabled
rli.setPrompt multiline.initialPrompt
rli.prompt <span class="hljs-literal">true</span>
repl.setPrompt multiline.initialPrompt
repl.prompt <span class="hljs-literal">true</span>
<span class="hljs-keyword">return</span></pre></div></div>
</li>
@ -580,7 +580,7 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> repl.rli.history = buffer.toString().split(<span class="hljs-string">'\n'</span>).reverse()</pre></div></div>
<div class="content"><div class='highlight'><pre> repl.history = buffer.toString().split(<span class="hljs-string">'\n'</span>).reverse()</pre></div></div>
</li>
@ -595,7 +595,7 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> repl.rli.history.pop() <span class="hljs-keyword">if</span> stat.size &gt; maxSize</pre></div></div>
<div class="content"><div class='highlight'><pre> repl.history.pop() <span class="hljs-keyword">if</span> stat.size &gt; maxSize</pre></div></div>
</li>
@ -610,13 +610,13 @@ undeclared variable <code>__</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> repl.rli.history.shift() <span class="hljs-keyword">if</span> repl.rli.history[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">''</span>
repl.rli.historyIndex = <span class="hljs-number">-1</span>
lastLine = repl.rli.history[<span class="hljs-number">0</span>]
<div class="content"><div class='highlight'><pre> repl.history.shift() <span class="hljs-keyword">if</span> repl.history[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">''</span>
repl.historyIndex = <span class="hljs-number">-1</span>
lastLine = repl.history[<span class="hljs-number">0</span>]
fd = fs.openSync filename, <span class="hljs-string">'a'</span>
repl.rli.addListener <span class="hljs-string">'line'</span>, <span class="hljs-function"><span class="hljs-params">(code)</span> -&gt;</span>
repl.addListener <span class="hljs-string">'line'</span>, <span class="hljs-function"><span class="hljs-params">(code)</span> -&gt;</span>
<span class="hljs-keyword">if</span> code <span class="hljs-keyword">and</span> code.length <span class="hljs-keyword">and</span> code <span class="hljs-keyword">isnt</span> <span class="hljs-string">'.history'</span> <span class="hljs-keyword">and</span> code <span class="hljs-keyword">isnt</span> <span class="hljs-string">'.exit'</span> <span class="hljs-keyword">and</span> lastLine <span class="hljs-keyword">isnt</span> code</pre></div></div>
</li>
@ -667,7 +667,7 @@ undeclared variable <code>__</code>.</p>
<div class="content"><div class='highlight'><pre> repl.commands[getCommandId(repl, <span class="hljs-string">'history'</span>)] =
help: <span class="hljs-string">'Show command history'</span>
action: <span class="hljs-function">-&gt;</span>
repl.outputStream.write <span class="hljs-string">"<span class="hljs-subst">#{repl.rli.history[..].reverse().join <span class="hljs-string">'\n'</span>}</span>\n"</span>
repl.outputStream.write <span class="hljs-string">"<span class="hljs-subst">#{repl.history[..].reverse().join <span class="hljs-string">'\n'</span>}</span>\n"</span>
repl.displayPrompt()
<span class="hljs-function">
<span class="hljs-title">getCommandId</span> = <span class="hljs-params">(repl, commandName)</span> -&gt;</span></pre></div></div>
@ -742,7 +742,7 @@ the REPL, the only applicable option is <code>transpile</code>.</p>
opts = merge replDefaults, opts
repl = nodeREPL.start opts
runInContext opts.prelude, repl.context, <span class="hljs-string">'prelude'</span> <span class="hljs-keyword">if</span> opts.prelude
repl.<span class="hljs-literal">on</span> <span class="hljs-string">'exit'</span>, <span class="hljs-function">-&gt;</span> repl.outputStream.write <span class="hljs-string">'\n'</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> repl.rli.closed
repl.<span class="hljs-literal">on</span> <span class="hljs-string">'exit'</span>, <span class="hljs-function">-&gt;</span> repl.outputStream.write <span class="hljs-string">'\n'</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> repl.closed
addMultilineHandler repl
addHistory repl, opts.historyFile, opts.historyMaxInputSize <span class="hljs-keyword">if</span> opts.historyFile</pre></div></div>

View file

@ -113,8 +113,8 @@
};
addMultilineHandler = function(repl) {
var inputStream, multiline, nodeLineListener, origPrompt, outputStream, ref, rli;
({rli, inputStream, outputStream} = repl);
var inputStream, multiline, nodeLineListener, origPrompt, outputStream, ref;
({inputStream, outputStream} = repl);
// Node 0.11.12 changed API, prompt is now _prompt.
origPrompt = (ref = repl._prompt) != null ? ref : repl.prompt;
multiline = {
@ -128,15 +128,15 @@
buffer: ''
};
// Proxy node's line listener
nodeLineListener = rli.listeners('line')[0];
rli.removeListener('line', nodeLineListener);
rli.on('line', function(cmd) {
nodeLineListener = repl.listeners('line')[0];
repl.removeListener('line', nodeLineListener);
repl.on('line', function(cmd) {
if (multiline.enabled) {
multiline.buffer += `${cmd}\n`;
rli.setPrompt(multiline.prompt);
rli.prompt(true);
repl.setPrompt(multiline.prompt);
repl.prompt(true);
} else {
rli.setPrompt(origPrompt);
repl.setPrompt(origPrompt);
nodeLineListener(cmd);
}
});
@ -149,28 +149,28 @@
// allow arbitrarily switching between modes any time before multiple lines are entered
if (!multiline.buffer.match(/\n/)) {
multiline.enabled = !multiline.enabled;
rli.setPrompt(origPrompt);
rli.prompt(true);
repl.setPrompt(origPrompt);
repl.prompt(true);
return;
}
// no-op unless the current line is empty
if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
if ((repl.line != null) && !repl.line.match(/^\s*$/)) {
return;
}
// eval, print, loop
multiline.enabled = !multiline.enabled;
rli.line = '';
rli.cursor = 0;
rli.output.cursorTo(0);
rli.output.clearLine(1);
repl.line = '';
repl.cursor = 0;
repl.output.cursorTo(0);
repl.output.clearLine(1);
// XXX: multiline hack
multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
rli.emit('line', multiline.buffer);
repl.emit('line', multiline.buffer);
multiline.buffer = '';
} else {
multiline.enabled = !multiline.enabled;
rli.setPrompt(multiline.initialPrompt);
rli.prompt(true);
repl.setPrompt(multiline.initialPrompt);
repl.prompt(true);
}
});
};
@ -189,20 +189,20 @@
fs.readSync(readFd, buffer, 0, size, stat.size - size);
fs.closeSync(readFd);
// Set the history on the interpreter
repl.rli.history = buffer.toString().split('\n').reverse();
repl.history = buffer.toString().split('\n').reverse();
if (stat.size > maxSize) {
// If the history file was truncated we should pop off a potential partial line
repl.rli.history.pop();
repl.history.pop();
}
if (repl.rli.history[0] === '') {
if (repl.history[0] === '') {
// Shift off the final blank newline
repl.rli.history.shift();
repl.history.shift();
}
repl.rli.historyIndex = -1;
lastLine = repl.rli.history[0];
repl.historyIndex = -1;
lastLine = repl.history[0];
} catch (error) {}
fd = fs.openSync(filename, 'a');
repl.rli.addListener('line', function(code) {
repl.addListener('line', function(code) {
if (code && code.length && code !== '.history' && code !== '.exit' && lastLine !== code) {
// Save the latest command in the file
fs.writeSync(fd, `${code}\n`);
@ -220,7 +220,7 @@
return repl.commands[getCommandId(repl, 'history')] = {
help: 'Show command history',
action: function() {
repl.outputStream.write(`${repl.rli.history.slice(0).reverse().join('\n')}\n`);
repl.outputStream.write(`${repl.history.slice(0).reverse().join('\n')}\n`);
return repl.displayPrompt();
}
};
@ -283,7 +283,7 @@
runInContext(opts.prelude, repl.context, 'prelude');
}
repl.on('exit', function() {
if (!repl.rli.closed) {
if (!repl.closed) {
return repl.outputStream.write('\n');
}
});

View file

@ -75,7 +75,7 @@ runInContext = (js, context, filename) ->
vm.runInContext js, context, filename
addMultilineHandler = (repl) ->
{rli, inputStream, outputStream} = repl
{inputStream, outputStream} = repl
# Node 0.11.12 changed API, prompt is now _prompt.
origPrompt = repl._prompt ? repl.prompt
@ -86,15 +86,15 @@ addMultilineHandler = (repl) ->
buffer: ''
# Proxy node's line listener
nodeLineListener = rli.listeners('line')[0]
rli.removeListener 'line', nodeLineListener
rli.on 'line', (cmd) ->
nodeLineListener = repl.listeners('line')[0]
repl.removeListener 'line', nodeLineListener
repl.on 'line', (cmd) ->
if multiline.enabled
multiline.buffer += "#{cmd}\n"
rli.setPrompt multiline.prompt
rli.prompt true
repl.setPrompt multiline.prompt
repl.prompt true
else
rli.setPrompt origPrompt
repl.setPrompt origPrompt
nodeLineListener cmd
return
@ -105,25 +105,25 @@ addMultilineHandler = (repl) ->
# allow arbitrarily switching between modes any time before multiple lines are entered
unless multiline.buffer.match /\n/
multiline.enabled = not multiline.enabled
rli.setPrompt origPrompt
rli.prompt true
repl.setPrompt origPrompt
repl.prompt true
return
# no-op unless the current line is empty
return if rli.line? and not rli.line.match /^\s*$/
return if repl.line? and not repl.line.match /^\s*$/
# eval, print, loop
multiline.enabled = not multiline.enabled
rli.line = ''
rli.cursor = 0
rli.output.cursorTo 0
rli.output.clearLine 1
repl.line = ''
repl.cursor = 0
repl.output.cursorTo 0
repl.output.clearLine 1
# XXX: multiline hack
multiline.buffer = multiline.buffer.replace /\n/g, '\uFF00'
rli.emit 'line', multiline.buffer
repl.emit 'line', multiline.buffer
multiline.buffer = ''
else
multiline.enabled = not multiline.enabled
rli.setPrompt multiline.initialPrompt
rli.prompt true
repl.setPrompt multiline.initialPrompt
repl.prompt true
return
# Store and load command history from a file
@ -139,17 +139,17 @@ addHistory = (repl, filename, maxSize) ->
fs.readSync readFd, buffer, 0, size, stat.size - size
fs.closeSync readFd
# Set the history on the interpreter
repl.rli.history = buffer.toString().split('\n').reverse()
repl.history = buffer.toString().split('\n').reverse()
# If the history file was truncated we should pop off a potential partial line
repl.rli.history.pop() if stat.size > maxSize
repl.history.pop() if stat.size > maxSize
# Shift off the final blank newline
repl.rli.history.shift() if repl.rli.history[0] is ''
repl.rli.historyIndex = -1
lastLine = repl.rli.history[0]
repl.history.shift() if repl.history[0] is ''
repl.historyIndex = -1
lastLine = repl.history[0]
fd = fs.openSync filename, 'a'
repl.rli.addListener 'line', (code) ->
repl.addListener 'line', (code) ->
if code and code.length and code isnt '.history' and code isnt '.exit' and lastLine isnt code
# Save the latest command in the file
fs.writeSync fd, "#{code}\n"
@ -163,7 +163,7 @@ addHistory = (repl, filename, maxSize) ->
repl.commands[getCommandId(repl, 'history')] =
help: 'Show command history'
action: ->
repl.outputStream.write "#{repl.rli.history[..].reverse().join '\n'}\n"
repl.outputStream.write "#{repl.history[..].reverse().join '\n'}\n"
repl.displayPrompt()
getCommandId = (repl, commandName) ->
@ -212,7 +212,7 @@ module.exports =
opts = merge replDefaults, opts
repl = nodeREPL.start opts
runInContext opts.prelude, repl.context, 'prelude' if opts.prelude
repl.on 'exit', -> repl.outputStream.write '\n' if not repl.rli.closed
repl.on 'exit', -> repl.outputStream.write '\n' if not repl.closed
addMultilineHandler repl
addHistory repl, opts.historyFile, opts.historyMaxInputSize if opts.historyFile
# Adapt help inherited from the node REPL

View file

@ -45,7 +45,7 @@ ctrlV = { ctrl: true, name: 'v'}
testRepl 'reads history file', (input, output, repl) ->
input.emitLine repl.rli.history[0]
input.emitLine repl.history[0]
eq '3', output.lastWrite()
testRepl "starts with coffee prompt", (input, output) ->