Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tree-sitter-javascript"
description = "JavaScript grammar for tree-sitter"
version = "0.20.2"
version = "0.20.3"
authors = [
"Max Brunsfeld <[email protected]>",
"Douglas Creager <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ way.)
```toml
[dependencies]
tree-sitter = "~0.20.10"
tree-sitter-javascript = "~0.20.2"
tree-sitter-javascript = "~0.20.3"
```

Typically, you will use the [language][language func] function to add this
Expand Down
56 changes: 44 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ module.exports = grammar({
seq($.import_clause, $._from_clause),
field('source', $.string),
),
optional($.import_attribute),
$._semicolon,
),

Expand Down Expand Up @@ -234,6 +235,8 @@ module.exports = grammar({
),
),

import_attribute: $ => seq('with', $.object),

//
// Statements
//
Expand Down Expand Up @@ -607,10 +610,15 @@ module.exports = grammar({

// Should not contain new lines and should not start or end with a space
jsx_text: _ => choice(
/[^{}<>\n ]([^{}<>\n]*[^{}<>\n ])?/,
/[^{}<>\n& ]([^{}<>\n&]*[^{}<>\n& ])?/,
/\/\/[^\n]*/,
),

// An entity can be named, numeric (decimal), or numeric (hexadecimal). The
// longest entity name is 29 characters long, and the HTML spec says that
// no more will ever be added.
html_character_reference: _ => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});/,

jsx_expression: $ => seq(
'{',
optional(choice(
Expand All @@ -623,6 +631,7 @@ module.exports = grammar({

_jsx_child: $ => choice(
$.jsx_text,
$.html_character_reference,
$._jsx_element,
$.jsx_expression,
),
Expand All @@ -644,9 +653,9 @@ module.exports = grammar({
),

nested_identifier: $ => prec('member', seq(
choice($.identifier, alias($.nested_identifier, $.member_expression)),
field('object', choice($.identifier, alias($.nested_identifier, $.member_expression))),
'.',
alias($.identifier, $.property_identifier),
field('property', alias($.identifier, $.property_identifier)),
)),

jsx_namespace_name: $ => seq($._jsx_identifier, ':', $._jsx_identifier),
Expand Down Expand Up @@ -682,8 +691,36 @@ module.exports = grammar({
)),
),

_jsx_string: $ => choice(
seq(
'"',
repeat(choice(
alias($.unescaped_double_jsx_string_fragment, $.string_fragment),
$.html_character_reference,
)),
'"',
),
seq(
'\'',
repeat(choice(
alias($.unescaped_single_jsx_string_fragment, $.string_fragment),
$.html_character_reference,
)),
'\'',
),
),

// Workaround to https://github.com/tree-sitter/tree-sitter/issues/1156
// We give names to the token() constructs containing a regexp
// so as to obtain a node in the CST.
//
unescaped_double_jsx_string_fragment: _ => token.immediate(prec(1, /[^"&]+/)),

// same here
unescaped_single_jsx_string_fragment: _ => token.immediate(prec(1, /[^'&]+/)),

_jsx_attribute_value: $ => choice(
$.string,
alias($._jsx_string, $.string),
$.jsx_expression,
$._jsx_element,
),
Expand Down Expand Up @@ -909,12 +946,6 @@ module.exports = grammar({
// Primitives
//

// Here we tolerate unescaped newlines in double-quoted and
// single-quoted string literals.
// This is legal in typescript as jsx/tsx attribute values (as of
// 2020), and perhaps will be valid in javascript as well in the
// future.
//
string: $ => choice(
seq(
'"',
Expand All @@ -938,10 +969,10 @@ module.exports = grammar({
// We give names to the token() constructs containing a regexp
// so as to obtain a node in the CST.
//
unescaped_double_string_fragment: _ => token.immediate(prec(1, /[^"\\]+/)),
unescaped_double_string_fragment: _ => token.immediate(prec(1, /[^"\\\r\n]+/)),

// same here
unescaped_single_string_fragment: _ => token.immediate(prec(1, /[^'\\]+/)),
unescaped_single_string_fragment: _ => token.immediate(prec(1, /[^'\\\r\n]+/)),

escape_sequence: _ => token.immediate(seq(
'\\',
Expand All @@ -951,6 +982,7 @@ module.exports = grammar({
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u{[0-9a-fA-F]+}/,
/[\r?][\n\u2028\u2029]/,
),
)),

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-sitter-javascript",
"version": "0.20.2",
"version": "0.20.3",
"description": "JavaScript grammar for tree-sitter",
"main": "bindings/node",
"keywords": [
Expand Down
Loading