From 607b426e269b17f4ad32dca9c612c4a9f8541766 Mon Sep 17 00:00:00 2001 From: Gammut Date: Sun, 9 Jul 2023 11:35:32 -0500 Subject: Refactor queries for ecma based languages (#7207) --- runtime/queries/ecma/README.md | 14 ++++++++++++++ runtime/queries/ecma/highlights.scm | 9 ++++++++- runtime/queries/ecma/locals.scm | 20 +++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 runtime/queries/ecma/README.md (limited to 'runtime/queries/ecma') diff --git a/runtime/queries/ecma/README.md b/runtime/queries/ecma/README.md new file mode 100644 index 00000000..b891f24b --- /dev/null +++ b/runtime/queries/ecma/README.md @@ -0,0 +1,14 @@ +# Inheritance model for ecma-based languages + +Ecma-based languages share many traits. Because of this we want to share as many queries as possible while avoiding nested inheritance that can make query behaviour unpredictable due to unexpected precedence. + +To achieve that, there are "public" and "private" versions for javascript, jsx, and typescript query files, that share the same name, but the "private" version name starts with an underscore (with the exception of ecma, that already exists as a sort of base "private" language). This allows the "private" versions to host the specific queries of the language excluding any `; inherits` statement, in order to make them safe to be inherited by the "public" version of the same language and other languages as well. The tsx language doesn't have a "private" version given that currently it doesn't need to be inherited by other languages. + +| Language | Inherits from | +| ---------- | ----------------------- | +| javascript | _javascript, ecma | +| jsx | _jsx, _javascript, ecma | +| typescript | _typescript, ecma | +| tsx | _jsx, _typescript, ecma | + +If you intend to add queries to any of the ecma-based languages above, make sure you add them to the correct private language they belong to, so that other languages down the line can benefit from them. diff --git a/runtime/queries/ecma/highlights.scm b/runtime/queries/ecma/highlights.scm index 7285ab96..ddbe938f 100644 --- a/runtime/queries/ecma/highlights.scm +++ b/runtime/queries/ecma/highlights.scm @@ -46,8 +46,15 @@ (assignment_expression left: (identifier) @function right: [(function) (arrow_function)]) - +; Function and method parameters +;------------------------------- + +; Arrow function parameters in the form `p => ...` are supported by both +; javascript and typescript grammars without conflicts. +(arrow_function + parameter: (identifier) @variable.parameter) + ; Function and method calls ;-------------------------- diff --git a/runtime/queries/ecma/locals.scm b/runtime/queries/ecma/locals.scm index cc5f2e14..df8eb0d3 100644 --- a/runtime/queries/ecma/locals.scm +++ b/runtime/queries/ecma/locals.scm @@ -12,14 +12,28 @@ ; Definitions ;------------ -(pattern/identifier) @local.definition +; ...i +(rest_pattern + (identifier) @local.definition) + +; { i } +(object_pattern + (shorthand_property_identifier_pattern) @local.definition) -(pattern/rest_pattern +; { a: i } +(object_pattern + (pair_pattern + value: (identifier) @local.definition)) + +; [ i ] +(array_pattern (identifier) @local.definition) - + +; i => ... (arrow_function parameter: (identifier) @local.definition) +; const/let/var i = ... (variable_declarator name: (identifier) @local.definition) -- cgit v1.2.3-70-g09d2