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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ tree-sitter-language = "0.1"
cc = "1.2"

[dev-dependencies]
tree-sitter = { package = "willbooster-tree-sitter", version = "1" }
tree-sitter = { package = "willbooster-tree-sitter", version = "1.2.2" }
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This fork fixes parsing bugs and raises conformance with [ECMAScript (ECMA-262)]

The npm package ships `tree-sitter-javascript.wasm` for
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
browsers, and Cloudflare Workers. In Node.js and Bun, load it from the package:
browsers, and Cloudflare Workers. Use runtime 1.2.2 or newer. In Node.js and Bun, load it from the package:

```js
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -62,14 +62,13 @@ The package also ships the node types in `src/node-types.json`, the highlight, i
`require('@willbooster/tree-sitter-javascript/grammar')`). The crate ships `tree-sitter.json` and `queries/` too.

In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-javascript) and on
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
fuzzed with (the grammar also loads in the upstream `tree-sitter` crate 0.27, whose error recovery never ends on some
malformed input):
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) 1.2.2 or newer. The canonical constructor
supertype query requires this runtime version. This is the runtime this package is tested and fuzzed with:

```toml
[dependencies]
tree-sitter = { package = "willbooster-tree-sitter", version = "1" }
tree-sitter-javascript = { package = "willbooster-tree-sitter-javascript", version = "1" }
tree-sitter = { package = "willbooster-tree-sitter", version = "1.2.2" }
tree-sitter-javascript = { package = "willbooster-tree-sitter-javascript", version = "3" }
```

```rust
Expand Down
6 changes: 3 additions & 3 deletions bun.lock

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

127 changes: 119 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ module.exports = grammar({
$._export_default,
$._default_declaration_start,
$._regex_flags_start,
$._argumentless_new_end,
$._constructor_async_function_start,
],

extras: ($) => [$.comment, $.html_comment, /[\s\p{Zs}\uFEFF\u2028\u2029\u2060\u200B]/u],
Expand All @@ -116,7 +118,15 @@ module.exports = grammar({
properties: () => [],
},

supertypes: ($) => [$.statement, $.declaration, $.expression, $.primary_expression, $.pattern],
supertypes: ($) => [
$.statement,
$.declaration,
$.expression,
$.primary_expression,
$.pattern,
$._constructor,
$._constructor_chain_base,
],

inline: ($) => [
$._call_signature,
Expand Down Expand Up @@ -864,15 +874,116 @@ module.exports = grammar({
)
),

// As in ECMAScript, a member access or index extends the nearest `new` with arguments, while an argument list fills
// the nearest `new` that still lacks one: `new new A().b` constructs `new A().b`, `new new A().b(2)` constructs
// `new A().b` with `2`, and `new new A()(2)` constructs `new A()`. The 'member' and 'new' precedences make the parser
// shift a `.`, `[`, or argument list after a constructor rather than end a `new` without arguments, so that form
// never takes one. Both forms are primary expressions, so a nested `new` matches the constructor field's type.
new_expression: ($) =>
prec('new', seq('new', field('constructor', $.primary_expression), field('arguments', $.arguments))),
prec(
'new',
seq('new', field('constructor', alias($._constructor, $.primary_expression)), field('arguments', $.arguments))
),

_constructor_function_expression: ($) =>
prec(
'literal',
seq(
optional($._constructor_async_function_start),
optional('async'),
'function',
field('name', optional($.identifier)),
$._call_signature,
field('body', $.statement_block)
)
),

_constructor_generator_function: ($) =>
prec(
'literal',
seq(
optional($._constructor_async_function_start),
optional('async'),
'function',
'*',
field('name', optional($.identifier)),
$._call_signature,
field('body', $.statement_block)
)
),

_constructor_atom: ($) =>
choice(
$._jsx_element,
$.parenthesized_expression,
$._identifier,
alias($._await_yield_identifier, $.identifier),
alias($._reserved_identifier, $.identifier),
$.this,
$.super,
$.number,
$.string,
$.template_string,
$.regex,
$.true,
$.false,
$.null,
$.object,
$.array,
alias($._constructor_function_expression, $.function_expression),
alias($._constructor_generator_function, $.generator_function),
$.class,
$.meta_property
),

_constructor: ($) =>
choice(
$._constructor_atom,
$.new_expression,
alias($._argumentless_new_expression, $.new_expression),
alias($._constructor_member, $.member_expression),
alias($._constructor_subscript, $.subscript_expression),
alias($._constructor_tag, $.call_expression)
),

_constructor_chain_base: ($) =>
choice(alias($._constructor, $.primary_expression), alias($._constructor_import, $.call_expression)),

_constructor_import: ($) => prec('call', seq(field('function', $.import), field('arguments', $.arguments))),

_constructor_member: ($) =>
prec(
'member',
seq(
field('object', alias($._constructor_chain_base, $.primary_expression)),
'.',
field(
'property',
choice($.private_property_identifier, reserved('properties', alias($.identifier, $.property_identifier)))
)
)
),

_constructor_subscript: ($) =>
prec.right(
'member',
seq(
field('object', alias($._constructor_chain_base, $.primary_expression)),
'[',
field('index', $._expressions),
']'
)
),

_constructor_tag: ($) =>
prec(
'template_call',
seq(
field('function', alias($._constructor_chain_base, $.primary_expression)),
field('arguments', $.template_string)
)
),

_argumentless_new_expression: ($) => prec.right('new', seq('new', field('constructor', $.primary_expression))),
_argumentless_new_expression: ($) =>
prec.right(
'new',
seq('new', field('constructor', alias($._constructor, $.primary_expression)), $._argumentless_new_end)
),

await_expression: ($) =>
prec.dynamic(
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@willbooster/oxfmt-config": "1.2.2",
"@willbooster/oxlint-config": "1.4.8",
"@willbooster/wb": "22.13.1",
"@willbooster/web-tree-sitter": "1.0.16",
"@willbooster/web-tree-sitter": "1.2.2",
"conventional-changelog-conventionalcommits": "10.4.0",
"lefthook": "2.1.14",
"oxfmt": "0.70.0",
Expand All @@ -62,7 +62,7 @@
"wrangler": "4.136.2"
},
"peerDependencies": {
"@willbooster/web-tree-sitter": "^1.0.0"
"@willbooster/web-tree-sitter": "^1.2.2"
},
"peerDependenciesMeta": {
"@willbooster/web-tree-sitter": {
Expand Down
Loading
Loading