【问题标题】:How to disable "special commands" in node.js REPL?如何在 node.js REPL 中禁用“特殊命令”?
【发布时间】:2015-06-13 09:07:04
【问题描述】:

问题 Node REPL 有一些"special commands",例如.break.save。我从不使用这些,但我确实非常经常尝试粘贴到格式如下的 REPL 代码中:

words.append('ul')
    .classed('my-class', true)
    .selectAll('li.new-class')
    .data((tuple, tupleIdx) => obj[tupleIdx])
    .enter()
    .append('li')
    .classed('new-class', true)
    .text(d => '' + foo(d));

(这是 d3.js 代码,但在使用 Promises 时会发生类似的事情,.then(...)s 从每一行开始。)

当节点在自己的行上看到.classed.then 时,当然会抱怨“无效的 REPL 关键字”,然后继续打印几个屏幕长的一系列错误消息。

脆弱的伪解决方法我已经在 Vim 中使用正则表达式解决了这个问题,它将右括号和点之间的任何空格移动到点之后(:%s/)\n\(\s*\)\./).\r\1/ 以确保完整性)但这很乏味而且我经常想从浏览器中复制粘贴,而不是切换到 Vim 来重新格式化某些代码。

问题是否有任何方法可以禁用节点 REPL “功能”,这些功能虽然出于善意,但与标准 JavaScript 实践相冲突,例如以点开头的行?

或者这对于终端应用程序来说太复杂了,如果是这样,有没有办法可以通过浏览器的 JS 控制台与节点 REPL 通信(不是 node-monkey,它只处理 console.log

PS。这个问题主要是关于以. 开头的行,但另一个这样的冲突是_(谢天谢地由n_ 解决)。

【问题讨论】:

    标签: javascript node.js console read-eval-print-loop


    【解决方案1】:

    这里有一些尝试性:使用node-copy-paste,我编写了一个小模块,允许我在修复以. 开头的行后paste() 剪贴板的内容和evals。

    这在paste.js:

    var cp = require('copy-paste'); // npm install node-copy-paste
    
    module.exports = function() {
      return eval(cp.paste().replace(/\n\s*\./g, "."));
    };
    

    然后在节点 REPL 中,paste = require('./paste'); paste() 会成功。非常脆弱,但它可能经常解决问题,因此很有价值。

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 2019-03-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2016-01-14
      相关资源
      最近更新 更多