简体   繁体   English

Monaco 编辑器 - 与 JSHint 集成

[英]Monaco editor - integration with JSHint

Is there any way to integrate monaco editor with a jshint linting tool?有没有办法将 monaco 编辑器与 jshint linting 工具集成?

I know that monaco provides a possibility to set up compiler options, but they are not enough for me.我知道 monaco 提供了设置编译器选项的可能性,但它们对我来说还不够。 For instance, I would like to require semicolons at the end of the statements but can't find a way to do that.例如,我想在语句末尾使用分号,但找不到这样做的方法。

Ok, I have found one way, but I am still thinking if there is a better one.好的,我找到了一种方法,但我仍在思考是否有更好的方法。

Basically, I can run the JSHint analysis of my code manually.基本上,我可以手动运行我的代码的 JSHint 分析。

jshint.JSHINT(this.code, options, predef)

And afterwards based on the results I can create my custom model markers.然后根据结果,我可以创建我的自定义模型标记。 Something like:就像是:

let errors = jshint.JSHINT.data().errors.map(e => {
        return {
          startLineNumber: e.line,
          startColumn: e.character,
          endLineNumber: e.line,
          endColumn: e.character,
          message: e.raw,
          severity: e.code.startsWith('E') ? monaco.Severity.Error : monaco.Severity.Warning
        }
      })

And set model markers for my editor.并为我的编辑器设置模型标记。

monaco.editor.setModelMarkers(this.editor.getModel(), 'test', errors)

This works, although I still would like to customise the error markers, but maybe there is more natural way of doing it?这行得通,虽然我仍然想自定义错误标记,但也许有更自然的方法?

这是使用 jshint 的 monaco 编辑器的 JS linter: https ://github.com/arnaudpfu/monaco-js-linter

If you are looking for a pre-made solution, here is a JS linter for the monaco editor working with jshint: https://github.com/arnaudpfu/monaco-js-linter如果您正在寻找预制的解决方案,这里有一个 JS linter 供使用 jshint 的 monaco 编辑器使用: https://github.com/arnaudpfu/monaco-js-linter

First install it:首先安装它:

npm i monaco-js-linter

Then you can integrate the linter like this:然后你可以像这样集成 linter:

import monaco, { editor } from 'monaco-editor';
import JSMonacoLinter from 'monaco-js-linter';

// The Monaco Editor can be easily created, given an
// empty container and an options literal.
// Two members of the literal are "value" and "language".
// The editor takes the full size of its container.

const editor = monaco.editor.create(document.getElementById('container'), {
    value: 'js code here ...',
    language: 'javascript',
});

const linter = new JSMonacoLinter(editor, monaco);
linter.watch();

Hoping this helps !希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM