tree-sitter / tree-sitter-javascript

Javascript grammar for tree-sitter
MIT License
314 stars 108 forks source link

Bunch of errors caused by `e.innerHTML` in userscript #265

Closed johannesrld closed 9 months ago

johannesrld commented 9 months ago

The following piece of code is valid but it is parsed incorrectly:

(function() {
  window.addEventListener("load", (_) => {
    Array.prototype.slice
      .apply(document.querySelectorAll("div.usertext-body a"))
      .filter(a => a.innerText === "<image>").forEach(e => {
        e.innerHTML = "<img style=\"max-height: 45vh;\"/>";
        e.parentElement.querySelectorAll("span.res-freetext-expando").forEach(e =>
          e.remove()
        );
        e.lastElementChild.src = e.href;
      });
  });
})();

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

Typescript Playground link

The output of tree-sitter parse is the following: Installing tree-sitter and the parser was incredible inscrutable so i gave up trying to run the parse command, although neovims InspectTree Command might be the same thing, in which case there are a bunch of (ERROR) statements all throughout the tree, even in places arent anywhere near the offending line.

amaanq commented 9 months ago

Nope, neovim's InspectTree is not the same, as they use injections to improve parse output but which can be the cause of ERROR nodes. And even with neovim, I don't see any errors in InspectTree.

You can just install tree-sitter cli with cargo install tree-sitter, but again there is no parse error.