我正在构建自己的应用程序,该应用程序具有用于用户脚本的 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/