0

Given a tree-sitter tree for some HTML elements:

<script>console.log('should parse as js')</script>
<script async defer>console.log('works')</script>
(script_element
  (start_tag 
    (tag_name)) 
  (raw_text
    ; ... etc
  )
  (end_tag
    (tag_name)))
(script_element
  (start_tag
    (tag_name)))
    (attribute 
      (attribute_name))
    (attribute
      (attribute_name))
  (raw_text
    ; ... etc
  )
  (end_tag
    (tag_name)))

Broken Query

This query fails when used in neovim's tree-sitter html grammar injections.scm, returning an invalid field error

(script_element
   (start_tag !attribute))

How would I query for <script> elements which do not have any attributes? Is it necessary for the grammar to assign a field name to the child in order for queries to negate it?

Benny Powers
  • 5,398
  • 4
  • 32
  • 55

1 Answers1

0

This was answered on GitHub discussions: https://github.com/nvim-treesitter/nvim-treesitter/discussions/2582

The gist of this answer is that you query for a script_element for which the tag name is the last child, so there are thus no other children such as attributes:

(script_element (start_tag (tag_name) .)) @foo
Yet Another User
  • 2,627
  • 3
  • 18
  • 27