javascript - Chrome JavaScript 控制台中换行符的逻辑

标签 javascript google-chrome google-chrome-devtools

我正在构建自己的应用程序,该应用程序具有用于用户脚本的 JavaScript 后端,并且正在实现类似于 Chrome 的 JavaScript 控制台的东西。

让我感到困惑的一件事是 Chrome 用来决定何时返回键触发命令执行以及何时自动继续换行的逻辑。

例如,如果你输入

function blah() { <return>

然后它继续到下一行以允许您继续键入函数,而

function blah() {] <return>

立即给出一个解析错误(所以它不只是保持开括号对的计数那么简单)

有没有人有任何命中(或者是否有足够熟悉 Chromium 源代码的人能够向我指出他们代码库中的特定逻辑)

最佳答案

这是 the source that decides what to do when Enter is pressed (谢谢,wOxxOm):

  async _enterKeyPressed(event) {
    if (event.altKey || event.ctrlKey || event.shiftKey)
      return;

    event.consume(true);

    this.clearAutocomplete();

    const str = this.text();
    if (!str.length)
      return;

    if (!this._isCaretAtEndOfPrompt()) {
      await this._appendCommand(str, true);
      return;
    }

    if (await ObjectUI.JavaScriptAutocomplete.isExpressionComplete(str))
      await this._appendCommand(str, true);
    else
      this._editor.newlineAndIndent();
    this._enterProcessedForTest();
  }

这里是 the source that determines if an expression is complete :

  static async isExpressionComplete(expression) {
    const currentExecutionContext = UI.context.flavor(SDK.ExecutionContext);
    if (!currentExecutionContext)
      return true;
    const result =
        await currentExecutionContext.runtimeModel.compileScript(expression, '', false, currentExecutionContext.id);
    if (!result.exceptionDetails)
      return true;
    const description = result.exceptionDetails.exception.description;
    return !description.startsWith('SyntaxError: Unexpected end of input') &&
        !description.startsWith('SyntaxError: Unterminated template literal');
  }

如果我的解释是正确的,看起来 DevTools 只是编译表达式并使用该结果来确定表达式是否完整。如果有异常或错误,则不完整。

关于javascript - Chrome JavaScript 控制台中换行符的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51581062/

相关文章:

javascript - 在 Windows 上提升 NodeJS/Electron 进程

javascript - 我的 svg 贝塞尔曲线的波浪效果这可能吗?

javascript - 当浏览器自动填写表单字段时,$parsers\$formatters 函数不会触发

python-3.x - 通过 Selenium 在 Chrome 上打开检查(按 F12)

javascript - 为什么 chrome 开发者控制台没有列出任何属性/方法?

javascript - 在 javascript 和 angularjs 翻译中编写 html 不起作用

javascript - IE 中的选择箭头下拉列表不同

css - Google Chrome "Failed parsing SourceMap": css. map (Web Essential)

javascript - Chrome 扩展程序内容脚本无法加载,并且没有错误。没有任何。连一点儿都没有

javascript - 在 Phonegap JQuery 移动设备上更改方向