<script src="https://uovol-resources.sunshineknows.com/tree-sitter/tree-sitter.js"></script>
<script>
const Parser = window.TreeSitter;
Parser.init().then(async () => {
console.log(123);
const parser = new Parser;
// tree-sitter-javascript2 是自己编译出来的。
// tree-sitter-javascript 是 github 仓库复制的
const JavaScript = await Parser.Language.load('https://uovol-resources.sunshineknows.com/tree-sitter/tree-sitter-javascript2.wasm');
parser.setLanguage(JavaScript);
const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
const callExpression = tree.rootNode.child(1).firstChild;
console.log(callExpression);
// Replace 'let' with 'const'
const newSourceCode = 'const x = 1; console.log(x);';
tree.edit({
startIndex: 0,
oldEndIndex: 3,
newEndIndex: 5,
startPosition: {row: 0, column: 0},
oldEndPosition: {row: 0, column: 3},
newEndPosition: {row: 0, column: 5},
});
const newTree = parser.parse(newSourceCode, tree);
console.log(newTree.rootNode.toString());
});
</script>
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ... </head>
<body>
</body>
控制台