George Kemp
George Kemp

Reputation: 631

How to Disable Single Monaco Editor Syntax Rule

I have a use case where I have a monaco editor that allows users to write a custom function. This function must be in the format

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

because we then assign the output to a variable which we use to run data through.

However I'm getting the "Identifier Expected" error. Is there a way to disable this single syntax rule?

The error

Upvotes: 2

Views: 2147

Answers (3)

Stefan Dobre
Stefan Dobre

Reputation: 277

In the meantime, a proper API has been introduced to ignore certain errors. This should fix the above false-positive:

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

Upvotes: 3

Jon N
Jon N

Reputation: 1489

I have the same situation where I want to allow a return statement at the global level, because my script is a 'function'. Typescript flags the return as an error with 'A 'return' statement can only be used within a function body.'

A solution I found is to filter the model markers. This is god awful because of the string matching, but it works. There is a slight flicker in the UI as the decoration appears and then immediately disappears.

After you create the editor, subscribe to decoration changes:

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

Where you go and get the current markers, filter them, and put them back in the editor:

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)
    }
}

Hopefully we can at least get error codes to match on in the future. There might be more info in the marker that you could better filter on, have a look at the marker object in a debugger.

Upvotes: 0

Bharata
Bharata

Reputation: 14175

But it is not a Monaco Editor Syntax Rule!

It is the JavaScript error! And you get this error because if you write it like this:

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

then you will can not to use this function!

You have to write it with name like this:

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

Only, if you have this function as a parameter like here:

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

you can write it without a name. Or if you use it immediately like follows:

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

Upvotes: 1

Related Questions