aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/queries/ecma
diff options
context:
space:
mode:
authorChristian Clason <c.clason@uni-graz.at>2023-06-12 09:54:30 -0600
committerChristian Clason <c.clason@uni-graz.at>2025-05-12 18:43:40 +0200
commit692b051b09935653befdb8f7ba8afdb640adf17b (patch)
tree167162b6b129ae04f68c5735078521a72917c742 /runtime/queries/ecma
parentfeat(c-family): inherit injections (diff)
downloadnvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.gz
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.bz2
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.lz
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.xz
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.zst
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.zip
feat!: drop modules, general refactor and cleanup
Diffstat (limited to 'runtime/queries/ecma')
-rw-r--r--runtime/queries/ecma/folds.scm24
-rw-r--r--runtime/queries/ecma/highlights.scm392
-rw-r--r--runtime/queries/ecma/indents.scm82
-rw-r--r--runtime/queries/ecma/injections.scm203
-rw-r--r--runtime/queries/ecma/locals.scm42
5 files changed, 743 insertions, 0 deletions
diff --git a/runtime/queries/ecma/folds.scm b/runtime/queries/ecma/folds.scm
new file mode 100644
index 000000000..a348f3444
--- /dev/null
+++ b/runtime/queries/ecma/folds.scm
@@ -0,0 +1,24 @@
+[
+ (arguments)
+ (for_in_statement)
+ (for_statement)
+ (while_statement)
+ (arrow_function)
+ (function_expression)
+ (function_declaration)
+ (class_declaration)
+ (method_definition)
+ (do_statement)
+ (with_statement)
+ (switch_statement)
+ (switch_case)
+ (switch_default)
+ (import_statement)+
+ (if_statement)
+ (try_statement)
+ (catch_clause)
+ (array)
+ (object)
+ (generator_function)
+ (generator_function_declaration)
+] @fold
diff --git a/runtime/queries/ecma/highlights.scm b/runtime/queries/ecma/highlights.scm
new file mode 100644
index 000000000..cec2f4e3e
--- /dev/null
+++ b/runtime/queries/ecma/highlights.scm
@@ -0,0 +1,392 @@
+; Types
+; Javascript
+; Variables
+;-----------
+(identifier) @variable
+
+; Properties
+;-----------
+(property_identifier) @variable.member
+
+(shorthand_property_identifier) @variable.member
+
+(private_property_identifier) @variable.member
+
+(object_pattern
+ (shorthand_property_identifier_pattern) @variable)
+
+(object_pattern
+ (object_assignment_pattern
+ (shorthand_property_identifier_pattern) @variable))
+
+; Special identifiers
+;--------------------
+((identifier) @type
+ (#lua-match? @type "^[A-Z]"))
+
+((identifier) @constant
+ (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$"))
+
+((shorthand_property_identifier) @constant
+ (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$"))
+
+((identifier) @variable.builtin
+ (#any-of? @variable.builtin "arguments" "module" "console" "window" "document"))
+
+((identifier) @type.builtin
+ (#any-of? @type.builtin
+ "Object" "Function" "Boolean" "Symbol" "Number" "Math" "Date" "String" "RegExp" "Map" "Set"
+ "WeakMap" "WeakSet" "Promise" "Array" "Int8Array" "Uint8Array" "Uint8ClampedArray" "Int16Array"
+ "Uint16Array" "Int32Array" "Uint32Array" "Float32Array" "Float64Array" "ArrayBuffer" "DataView"
+ "Error" "EvalError" "InternalError" "RangeError" "ReferenceError" "SyntaxError" "TypeError"
+ "URIError"))
+
+(statement_identifier) @label
+
+; Function and method definitions
+;--------------------------------
+(function_expression
+ name: (identifier) @function)
+
+(function_declaration
+ name: (identifier) @function)
+
+(generator_function
+ name: (identifier) @function)
+
+(generator_function_declaration
+ name: (identifier) @function)
+
+(method_definition
+ name: [
+ (property_identifier)
+ (private_property_identifier)
+ ] @function.method)
+
+(method_definition
+ name: (property_identifier) @constructor
+ (#eq? @constructor "constructor"))
+
+(pair
+ key: (property_identifier) @function.method
+ value: (function_expression))
+
+(pair
+ key: (property_identifier) @function.method
+ value: (arrow_function))
+
+(assignment_expression
+ left: (member_expression
+ property: (property_identifier) @function.method)
+ right: (arrow_function))
+
+(assignment_expression
+ left: (member_expression
+ property: (property_identifier) @function.method)
+ right: (function_expression))
+
+(variable_declarator
+ name: (identifier) @function
+ value: (arrow_function))
+
+(variable_declarator
+ name: (identifier) @function
+ value: (function_expression))
+
+(assignment_expression
+ left: (identifier) @function
+ right: (arrow_function))
+
+(assignment_expression
+ left: (identifier) @function
+ right: (function_expression))
+
+; Function and method calls
+;--------------------------
+(call_expression
+ function: (identifier) @function.call)
+
+(call_expression
+ function: (member_expression
+ property: [
+ (property_identifier)
+ (private_property_identifier)
+ ] @function.method.call))
+
+(call_expression
+ function: (await_expression
+ (identifier) @function.call))
+
+(call_expression
+ function: (await_expression
+ (member_expression
+ property: [
+ (property_identifier)
+ (private_property_identifier)
+ ] @function.method.call)))
+
+; Builtins
+;---------
+((identifier) @module.builtin
+ (#eq? @module.builtin "Intl"))
+
+((identifier) @function.builtin
+ (#any-of? @function.builtin
+ "eval" "isFinite" "isNaN" "parseFloat" "parseInt" "decodeURI" "decodeURIComponent" "encodeURI"
+ "encodeURIComponent" "require"))
+
+; Constructor
+;------------
+(new_expression
+ constructor: (identifier) @constructor)
+
+; Decorators
+;----------
+(decorator
+ "@" @attribute
+ (identifier) @attribute)
+
+(decorator
+ "@" @attribute
+ (call_expression
+ (identifier) @attribute))
+
+(decorator
+ "@" @attribute
+ (member_expression
+ (property_identifier) @attribute))
+
+(decorator
+ "@" @attribute
+ (call_expression
+ (member_expression
+ (property_identifier) @attribute)))
+
+; Literals
+;---------
+[
+ (this)
+ (super)
+] @variable.builtin
+
+((identifier) @variable.builtin
+ (#eq? @variable.builtin "self"))
+
+[
+ (true)
+ (false)
+] @boolean
+
+[
+ (null)
+ (undefined)
+] @constant.builtin
+
+[
+ (comment)
+ (html_comment)
+] @comment @spell
+
+((comment) @comment.documentation
+ (#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$"))
+
+(hash_bang_line) @keyword.directive
+
+((string_fragment) @keyword.directive
+ (#eq? @keyword.directive "use strict"))
+
+(string) @string
+
+(template_string) @string
+
+(escape_sequence) @string.escape
+
+(regex_pattern) @string.regexp
+
+(regex_flags) @character.special
+
+(regex
+ "/" @punctuation.bracket) ; Regex delimiters
+
+(number) @number
+
+((identifier) @number
+ (#any-of? @number "NaN" "Infinity"))
+
+; Punctuation
+;------------
+[
+ ";"
+ "."
+ ","
+ ":"
+] @punctuation.delimiter
+
+[
+ "--"
+ "-"
+ "-="
+ "&&"
+ "+"
+ "++"
+ "+="
+ "&="
+ "/="
+ "**="
+ "<<="
+ "<"
+ "<="
+ "<<"
+ "="
+ "=="
+ "==="
+ "!="
+ "!=="
+ "=>"
+ ">"
+ ">="
+ ">>"
+ "||"
+ "%"
+ "%="
+ "*"
+ "**"
+ ">>>"
+ "&"
+ "|"
+ "^"
+ "??"
+ "*="
+ ">>="
+ ">>>="
+ "^="
+ "|="
+ "&&="
+ "||="
+ "??="
+ "..."
+] @operator
+
+(binary_expression
+ "/" @operator)
+
+(ternary_expression
+ [
+ "?"
+ ":"
+ ] @keyword.conditional.ternary)
+
+(unary_expression
+ [
+ "!"
+ "~"
+ "-"
+ "+"
+ ] @operator)
+
+(unary_expression
+ [
+ "delete"
+ "void"
+ ] @keyword.operator)
+
+[
+ "("
+ ")"
+ "["
+ "]"
+ "{"
+ "}"
+] @punctuation.bracket
+
+(template_substitution
+ [
+ "${"
+ "}"
+ ] @punctuation.special) @none
+
+; Imports
+;----------
+(namespace_import
+ "*" @character.special
+ (identifier) @module)
+
+(namespace_export
+ "*" @character.special
+ (identifier) @module)
+
+(export_statement
+ "*" @character.special)
+
+; Keywords
+;----------
+[
+ "if"
+ "else"
+ "switch"
+ "case"
+] @keyword.conditional
+
+[
+ "import"
+ "from"
+ "as"
+ "export"
+] @keyword.import
+
+[
+ "for"
+ "of"
+ "do"
+ "while"
+ "continue"
+] @keyword.repeat
+
+[
+ "break"
+ "const"
+ "debugger"
+ "extends"
+ "get"
+ "let"
+ "set"
+ "static"
+ "target"
+ "var"
+ "with"
+] @keyword
+
+"class" @keyword.type
+
+[
+ "async"
+ "await"
+] @keyword.coroutine
+
+[
+ "return"
+ "yield"
+] @keyword.return
+
+"function" @keyword.function
+
+[
+ "new"
+ "delete"
+ "in"
+ "instanceof"
+ "typeof"
+] @keyword.operator
+
+[
+ "throw"
+ "try"
+ "catch"
+ "finally"
+] @keyword.exception
+
+(export_statement
+ "default" @keyword)
+
+(switch_default
+ "default" @keyword.conditional)
diff --git a/runtime/queries/ecma/indents.scm b/runtime/queries/ecma/indents.scm
new file mode 100644
index 000000000..d56741670
--- /dev/null
+++ b/runtime/queries/ecma/indents.scm
@@ -0,0 +1,82 @@
+[
+ (arguments)
+ (array)
+ (binary_expression)
+ (class_body)
+ (export_clause)
+ (formal_parameters)
+ (named_imports)
+ (object)
+ (object_pattern)
+ (parenthesized_expression)
+ (return_statement)
+ (statement_block)
+ (switch_case)
+ (switch_default)
+ (switch_statement)
+ (template_substitution)
+ (ternary_expression)
+] @indent.begin
+
+(arguments
+ (call_expression) @indent.begin)
+
+(binary_expression
+ (call_expression) @indent.begin)
+
+(expression_statement
+ (call_expression) @indent.begin)
+
+(arrow_function
+ body: (_) @_body
+ (#not-kind-eq? @_body "statement_block")) @indent.begin
+
+(assignment_expression
+ right: (_) @_right
+ (#not-kind-eq? @_right "arrow_function" "function")) @indent.begin
+
+(variable_declarator
+ value: (_) @_value
+ (#not-kind-eq? @_value "arrow_function" "call_expression" "function")) @indent.begin
+
+(arguments
+ ")" @indent.end)
+
+(object
+ "}" @indent.end)
+
+(statement_block
+ "}" @indent.end)
+
+[
+ (arguments
+ (object))
+ ")"
+ "}"
+ "]"
+] @indent.branch
+
+(statement_block
+ "{" @indent.branch)
+
+((parenthesized_expression
+ "("
+ (_)
+ ")" @indent.end) @_outer
+ (#not-has-parent? @_outer if_statement))
+
+[
+ "}"
+ "]"
+] @indent.end
+
+(template_string) @indent.ignore
+
+[
+ (comment)
+ (ERROR)
+] @indent.auto
+
+(if_statement
+ consequence: (_) @indent.dedent
+ (#not-kind-eq? @indent.dedent statement_block)) @indent.begin
diff --git a/runtime/queries/ecma/injections.scm b/runtime/queries/ecma/injections.scm
new file mode 100644
index 000000000..04abafcde
--- /dev/null
+++ b/runtime/queries/ecma/injections.scm
@@ -0,0 +1,203 @@
+(((comment) @_jsdoc_comment
+ (#lua-match? @_jsdoc_comment "^/[*][*][^*].*[*]/$")) @injection.content
+ (#set! injection.language "jsdoc"))
+
+((comment) @injection.content
+ (#set! injection.language "comment"))
+
+; html(`...`), html`...`, sql(`...`), etc.
+(call_expression
+ function: (identifier) @injection.language
+ arguments: [
+ (arguments
+ (template_string) @injection.content)
+ (template_string) @injection.content
+ ]
+ (#lua-match? @injection.language "^[a-zA-Z][a-zA-Z0-9]*$")
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ ; Languages excluded from auto-injection due to special rules
+ ; - svg uses the html parser
+ ; - css uses the styled parser
+ (#not-any-of? @injection.language "svg" "css"))
+
+; svg`...` or svg(`...`)
+(call_expression
+ function: (identifier) @_name
+ (#eq? @_name "svg")
+ arguments: [
+ (arguments
+ (template_string) @injection.content)
+ (template_string) @injection.content
+ ]
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "html"))
+
+; Vercel PostgreSQL
+; foo.sql`...` or foo.sql(`...`)
+(call_expression
+ function: (member_expression
+ property: (property_identifier) @injection.language)
+ arguments: [
+ (arguments
+ (template_string) @injection.content)
+ (template_string) @injection.content
+ ]
+ (#eq? @injection.language "sql")
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children))
+
+(call_expression
+ function: (identifier) @_name
+ (#eq? @_name "gql")
+ arguments: (template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "graphql"))
+
+(call_expression
+ function: (identifier) @_name
+ (#eq? @_name "hbs")
+ arguments: (template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "glimmer"))
+
+; css`<css>`, keyframes`<css>`
+(call_expression
+ function: (identifier) @_name
+ (#any-of? @_name "css" "keyframes")
+ arguments: (template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "styled"))
+
+; styled.div`<css>`
+(call_expression
+ function: (member_expression
+ object: (identifier) @_name
+ (#eq? @_name "styled"))
+ arguments: ((template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "styled")))
+
+; styled(Component)`<css>`
+(call_expression
+ function: (call_expression
+ function: (identifier) @_name
+ (#eq? @_name "styled"))
+ arguments: ((template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "styled")))
+
+; styled.div.attrs({ prop: "foo" })`<css>`
+(call_expression
+ function: (call_expression
+ function: (member_expression
+ object: (member_expression
+ object: (identifier) @_name
+ (#eq? @_name "styled"))))
+ arguments: ((template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "styled")))
+
+; styled(Component).attrs({ prop: "foo" })`<css>`
+(call_expression
+ function: (call_expression
+ function: (member_expression
+ object: (call_expression
+ function: (identifier) @_name
+ (#eq? @_name "styled"))))
+ arguments: ((template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "styled")))
+
+((regex_pattern) @injection.content
+ (#set! injection.language "regex"))
+
+; ((comment) @_gql_comment
+; (#eq? @_gql_comment "/* GraphQL */")
+; (template_string) @injection.content
+; (#set! injection.language "graphql"))
+((template_string) @injection.content
+ (#lua-match? @injection.content "^`#graphql")
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "graphql"))
+
+; el.innerHTML = `<html>`
+(assignment_expression
+ left: (member_expression
+ property: (property_identifier) @_prop
+ (#any-of? @_prop "outerHTML" "innerHTML"))
+ right: (template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "html"))
+
+; el.innerHTML = '<html>'
+(assignment_expression
+ left: (member_expression
+ property: (property_identifier) @_prop
+ (#any-of? @_prop "outerHTML" "innerHTML"))
+ right: (string
+ (string_fragment) @injection.content)
+ (#set! injection.language "html"))
+
+;---- Angular injections -----
+; @Component({
+; template: `<html>`
+; })
+(decorator
+ (call_expression
+ function: ((identifier) @_name
+ (#eq? @_name "Component"))
+ arguments: (arguments
+ (object
+ (pair
+ key: ((property_identifier) @_prop
+ (#eq? @_prop "template"))
+ value: ((template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "angular")))))))
+
+; @Component({
+; styles: [`<css>`]
+; })
+(decorator
+ (call_expression
+ function: ((identifier) @_name
+ (#eq? @_name "Component"))
+ arguments: (arguments
+ (object
+ (pair
+ key: ((property_identifier) @_prop
+ (#eq? @_prop "styles"))
+ value: (array
+ ((template_string) @injection.content
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.include-children)
+ (#set! injection.language "css"))))))))
+
+; @Component({
+; styles: `<css>`
+; })
+(decorator
+ (call_expression
+ function: ((identifier) @_name
+ (#eq? @_name "Component"))
+ arguments: (arguments
+ (object
+ (pair
+ key: ((property_identifier) @_prop
+ (#eq? @_prop "styles"))
+ value: ((template_string) @injection.content
+ (#set! injection.include-children)
+ (#offset! @injection.content 0 1 0 -1)
+ (#set! injection.language "css")))))))
diff --git a/runtime/queries/ecma/locals.scm b/runtime/queries/ecma/locals.scm
new file mode 100644
index 000000000..24ea7c0a8
--- /dev/null
+++ b/runtime/queries/ecma/locals.scm
@@ -0,0 +1,42 @@
+; Scopes
+;-------
+(statement_block) @local.scope
+
+(function_expression) @local.scope
+
+(arrow_function) @local.scope
+
+(function_declaration) @local.scope
+
+(method_definition) @local.scope
+
+(for_statement) @local.scope
+
+(for_in_statement) @local.scope
+
+(catch_clause) @local.scope
+
+; Definitions
+;------------
+(variable_declarator
+ name: (identifier) @local.definition.var)
+
+(import_specifier
+ (identifier) @local.definition.import)
+
+(namespace_import
+ (identifier) @local.definition.import)
+
+(function_declaration
+ (identifier) @local.definition.function
+ (#set! definition.var.scope parent))
+
+(method_definition
+ (property_identifier) @local.definition.function
+ (#set! definition.var.scope parent))
+
+; References
+;------------
+(identifier) @local.reference
+
+(shorthand_property_identifier) @local.reference