我正在使用摩纳哥编辑器来编辑打字稿。有没有办法获得当前模型的 AST?是否可以修改树以便编辑器对更改做出反应?即我想为打字稿做简单的重构工具?
Monaco 不公开其 AST,但您可以使用jscodeshift代替:
const editor = monaco.editor.create(
document.querySelector("#editor"), {value: 'var foo;'})// editor content: var foo;
const newValue = jscodeshift(editor.getValue())
.findVariableDeclarators('foo')
.renameTo('bar')
.toSource();
editor.setValue(newValue); // editor content: var bar;
Run Code Online (Sandbox Code Playgroud)