Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose template string chars as nodes #276

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = grammar({

externals: $ => [
$._automatic_semicolon,
$._template_chars,
$.template_chars,
$._ternary_qmark,
],

Expand Down Expand Up @@ -976,7 +976,7 @@ module.exports = grammar({
template_string: $ => seq(
'`',
repeat(choice(
$._template_chars,
$.template_chars,
$.escape_sequence,
$.template_substitution,
)),
Expand Down
4 changes: 3 additions & 1 deletion queries/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
(member_expression
property: (property_identifier) @injection.language)
]
arguments: (template_string) @injection.content)
arguments: (template_string
(template_chars) @injection.content
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test/highlight/injection.js was failing, which tests that for the source text:

eval(js `var foo`)

the var foo gets highlighted as JS content (eg var gets highlighted as a keyword)

My understanding is that the reason that test started failing is because the way that injections work, by default this @injection.content doesn't include ("re-parse") any child nodes' content of this (template_string) node. And now there is a (template_chars) child node, so the re-parsing of the (template_string) node doesn't include the var foo content

So this gets that test passing by specifying that the new (template_chars) node should be used as the thing to be re-parsed instead

I have no idea if this is hacky or not, eg not sure how the old version vs this version would behave when highlighting eg a template string with multiple text sections (eval(`is this text parsed as JS ${foo} and then this text is also parsed as JS`))

))

; Parse regex syntax within regex literals

Expand Down
4 changes: 2 additions & 2 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 48 additions & 48 deletions src/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 28 additions & 10 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,49 @@ Template strings
----

(program
(expression_statement (template_string))
(expression_statement (template_string))
(expression_statement (template_string
(template_chars)))
(expression_statement (template_string
(template_chars)))
(expression_statement (template_string
(template_chars)
(template_substitution
(binary_expression (number) (number)))
(template_chars)
(template_substitution (sequence_expression
(binary_expression (number) (number))
(binary_expression (number) (number))))))
(expression_statement (template_string))
(binary_expression (number) (number))))
(template_chars)))
(expression_statement (template_string
(template_chars)))
(expression_statement (template_string
(template_chars)
(template_substitution (number))))
(expression_statement (template_string))
(expression_statement (template_string))
(expression_statement (template_string
(template_chars)))
(expression_statement (template_string
(template_chars)))
(expression_statement (template_string
(template_chars)
(template_substitution (string (string_fragment)))
(template_chars)
(template_substitution (string (string_fragment)))
(template_substitution (string (string_fragment)))))
(template_chars)))
(expression_statement (template_string (escape_sequence)))
(expression_statement (template_string
(template_chars)
(escape_sequence)
(template_chars)
(template_substitution (call_expression
(member_expression (identifier) (property_identifier))
(arguments (string (string_fragment)))))
(escape_sequence)
(template_substitution (identifier))))
(template_chars)
(template_substitution (identifier))
(template_chars)))
(expression_statement (template_string (escape_sequence)))
(expression_statement (template_string)))
(expression_statement (template_string
(template_chars))))

============================================
Function calls with template strings
Expand All @@ -96,7 +113,8 @@ f `hello`;
---

(program
(expression_statement (call_expression (identifier) (template_string))))
(expression_statement (call_expression (identifier) (template_string
(template_chars)))))

============================================
Numbers
Expand Down
Loading