簡體   English   中英

如何禁用單個摩納哥編輯器語法規則

[英]How to Disable Single Monaco Editor Syntax Rule

我有一個用例,我有一個 monaco 編輯器,允許用戶編寫自定義函數。 這個函數必須是這樣的格式

function (source, callback) {
    callback(source);
}

因為我們然后將輸出分配給我們用來運行數據的變量。

但是,我收到“預期標識符”錯誤。 有沒有辦法禁用這個單一的語法規則?

錯誤

同時,引入了適當的 API 來忽略某些錯誤。 這應該解決上述誤報:

monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
    diagnosticCodesToIgnore: [1003]
});

但這不是摩納哥編輯器語法規則!

這是 JavaScript 錯誤! 你會收到這個錯誤,因為如果你這樣寫:

function (source, callback)
{
    callback(source);
}

那你就不能使用這個功能了!

你必須用這樣的名字寫它:

function funcName(source, callback)
{
    callback(source);
}

僅當您將此函數作為參數時,如下所示:

anotherFunction(function(source, callback)
{
    callback(source);
});

你可以不寫名字。 或者,如果您立即使用它,如下所示:

(function(source, callback)
{
    callback(source);
})('Hello, world!', alert);

我有同樣的情況,我想在全局級別允許 return 語句,因為我的腳本是一個“函數”。 Typescript 將返回標記為錯誤,並帶有“A'return' 語句只能在函數體內使用。”

我發現的一個解決方案是過濾模型標記。 由於字符串匹配,這太糟糕了,但它有效。 當裝飾出現然后立即消失時,UI 中有輕微的閃爍。

創建編輯器后,訂閱裝飾更改:

   this.editor.onDidChangeModelDecorations( e => this.onDidChangeModelDecorations(e) )

你去哪里獲取當前標記,過濾它們,然后把它們放回編輯器:

onDidChangeModelDecorations(e: monaco.editor.IModelDecorationsChangedEvent)
{
    let model = this.editor.getModel()
    let markers = monaco.editor.getModelMarkers( { owner:'typescript', resource: model.uri} )

    // We have to filter out the error that the editor gives on global returns.
    // Unfortunately the error code is null in the marker, so we have option but to 
    // match on the text of the error.
    // It will be obvious if this regresses.
    let filtered = markers.filter( marker => marker.message != "A 'return' statement can only be used within a function body." )
    if (filtered.length != markers.length)
    {
        monaco.editor.setModelMarkers(model, 'typescript', filtered)
    }
}

希望我們至少可以在未來獲得匹配的錯誤代碼。 標記中可能有更多信息可以更好地過濾,請查看調試器中的標記對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM