Skip to content

Commit

Permalink
Add <template> support
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jul 4, 2022
1 parent 7858313 commit ef8d87b
Show file tree
Hide file tree
Showing 9 changed files with 46,174 additions and 43,502 deletions.
22 changes: 21 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ module.exports = grammar({

expression: $ => choice(
$.primary_expression,
$.glimmer_template,
$._jsx_element,
$.jsx_fragment,
$.assignment_expression,
Expand Down Expand Up @@ -543,6 +544,24 @@ module.exports = grammar({
']'
),

glimmer_template: $ => choice(
seq(
field('open_tag', $.glimmer_opening_tag),
field('content', repeat($._glimmer_template_content)),
field('close_tag', $.glimmer_closing_tag),
),
// empty template has no content
// <template></template>
seq(
field('open_tag', $.glimmer_opening_tag),
field('close_tag', $.glimmer_closing_tag),
),
),

_glimmer_template_content: $ => /.{1,}/,
glimmer_opening_tag: $ => seq('<template>'),
glimmer_closing_tag: $ => seq('</template>'),

_jsx_element: $ => choice($.jsx_element, $.jsx_self_closing_element),

jsx_element: $ => seq(
Expand Down Expand Up @@ -1062,7 +1081,8 @@ module.exports = grammar({
'{',
repeat(choice(
seq(field('member', $.method_definition), optional(';')),
seq(field('member', $.field_definition), $._semicolon)
seq(field('member', $.field_definition), $._semicolon),
field('template', $.glimmer_template)
)),
'}'
),
Expand Down
27 changes: 27 additions & 0 deletions queries/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@

((comment) @injection.content
(#set! injection.language "jsdoc"))

; Parse Ember/Glimmer/Handlebars/HTMLBars/etc. template literals
; e.g.: await render(hbs`<SomeComponent />`)
(call_expression
function: ((identifier) @_name
(#eq? @_name "hbs"))
arguments: ((template_string) @glimmer
(#offset! @glimmer 0 1 0 -1)))

; Ember Unified <template> syntax
; e.g.: <template><SomeComponent @arg={{double @value}} /></template>
(
(jsx_element
open_tag: (jsx_opening_element
name: ((identifier) @_name
(#eq? @_name "template")
)
)
) @glimmer
(#offset! @glimmer 0 10 0 -11)
; ) @injection.content
; 10 - length of <template>
; 11 - lengeth of </template>
; (#offset! @injection.content 0 10 0 -11)
; (#set! injection.language "glimmer")
)

92 changes: 92 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,10 @@
"type": "SYMBOL",
"name": "primary_expression"
},
{
"type": "SYMBOL",
"name": "glimmer_template"
},
{
"type": "SYMBOL",
"name": "_jsx_element"
Expand Down Expand Up @@ -2304,6 +2308,86 @@
}
]
},
"glimmer_template": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "open_tag",
"content": {
"type": "SYMBOL",
"name": "glimmer_opening_tag"
}
},
{
"type": "FIELD",
"name": "content",
"content": {
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_glimmer_template_content"
}
}
},
{
"type": "FIELD",
"name": "close_tag",
"content": {
"type": "SYMBOL",
"name": "glimmer_closing_tag"
}
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "open_tag",
"content": {
"type": "SYMBOL",
"name": "glimmer_opening_tag"
}
},
{
"type": "FIELD",
"name": "close_tag",
"content": {
"type": "SYMBOL",
"name": "glimmer_closing_tag"
}
}
]
}
]
},
"_glimmer_template_content": {
"type": "PATTERN",
"value": ".{1,}"
},
"glimmer_opening_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<template>"
}
]
},
"glimmer_closing_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "</template>"
}
]
},
"_jsx_element": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -5856,6 +5940,14 @@
"name": "_semicolon"
}
]
},
{
"type": "FIELD",
"name": "template",
"content": {
"type": "SYMBOL",
"name": "glimmer_template"
}
}
]
}
Expand Down
58 changes: 58 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
"type": "binary_expression",
"named": true
},
{
"type": "glimmer_template",
"named": true
},
{
"type": "jsx_element",
"named": true
Expand Down Expand Up @@ -868,6 +872,16 @@
"named": true
}
]
},
"template": {
"multiple": true,
"required": false,
"types": [
{
"type": "glimmer_template",
"named": true
}
]
}
}
},
Expand Down Expand Up @@ -1560,6 +1574,42 @@
}
}
},
{
"type": "glimmer_closing_tag",
"named": true,
"fields": {}
},
{
"type": "glimmer_opening_tag",
"named": true,
"fields": {}
},
{
"type": "glimmer_template",
"named": true,
"fields": {
"close_tag": {
"multiple": false,
"required": true,
"types": [
{
"type": "glimmer_closing_tag",
"named": true
}
]
},
"open_tag": {
"multiple": false,
"required": true,
"types": [
{
"type": "glimmer_opening_tag",
"named": true
}
]
}
}
},
{
"type": "if_statement",
"named": true,
Expand Down Expand Up @@ -3130,6 +3180,10 @@
"type": "<",
"named": false
},
{
"type": "</template>",
"named": false
},
{
"type": "<<",
"named": false
Expand All @@ -3142,6 +3196,10 @@
"type": "<=",
"named": false
},
{
"type": "<template>",
"named": false
},
{
"type": "=",
"named": false
Expand Down
Loading

0 comments on commit ef8d87b

Please sign in to comment.