Skip to content

Fix: correct ASI for arrow function followed by parenthesized expressions #354

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
- name: Run tests
uses: tree-sitter/parser-test-action@v2
with:
generate: false # TODO: upgrade ABI, while on ABI 14 it is impossible to pass this check
test-rust: true
test-node: true
test-python: true
Expand Down
52 changes: 32 additions & 20 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ module.exports = grammar({
$._automatic_semicolon,
$._template_chars,
$._ternary_qmark,
$._shorthand_arrow,
$.html_comment,
// we use these just as signaling to the ASI scanner
'||',
'(',
'[',
// We use escape sequence and regex pattern to tell the scanner if we're currently inside a string or template string, in which case
// it should NOT parse html comments.
$.escape_sequence,
Expand Down Expand Up @@ -58,7 +62,6 @@ module.exports = grammar({
precedences: $ => [
[
'member',
'template_call',
'call',
$.update_expression,
'unary_void',
Expand All @@ -78,8 +81,9 @@ module.exports = grammar({
$.sequence_expression,
$.arrow_function,
],
['new', $.primary_expression],
['assign', $.primary_expression],
['member', 'template_call', 'new', 'call', $.expression],
['member', 'new_args', 'call', 'new_no_args', $.expression],
['declaration', 'literal'],
[$.primary_expression, $.statement_block, 'object'],
[$.meta_property, $.import],
Expand Down Expand Up @@ -488,12 +492,14 @@ module.exports = grammar({
$.binary_expression,
$.ternary_expression,
$.update_expression,
$.new_expression,
$.yield_expression,
$.arrow_function,
),

// Note: this is similar to MemberExpression from the ecmascript spec
primary_expression: $ => choice(
$.subscript_expression,
$.new_expression,
$.member_expression,
$.parenthesized_expression,
$._identifier,
Expand All @@ -510,7 +516,6 @@ module.exports = grammar({
$.object,
$.array,
$.function_expression,
$.arrow_function,
$.generator_function,
$.class,
$.meta_property,
Expand Down Expand Up @@ -768,11 +773,16 @@ module.exports = grammar({
)),
$._call_signature,
),
'=>',
field('body', choice(
$.expression,
$.statement_block,
)),
choice(
seq(
alias($._shorthand_arrow, '=>'),
field('body', $.expression),
),
seq(
choice('=>', alias($._shorthand_arrow, '=>')),
field('body', $.statement_block),
),
),
),

// Override
Expand All @@ -783,12 +793,8 @@ module.exports = grammar({

call_expression: $ => choice(
prec('call', seq(
field('function', choice($.expression, $.import)),
field('arguments', $.arguments),
)),
prec('template_call', seq(
field('function', choice($.primary_expression, $.new_expression)),
field('arguments', $.template_string),
field('function', choice($.primary_expression, $.import)),
field('arguments', choice($.arguments, $.template_string)),
)),
prec('member', seq(
field('function', $.primary_expression),
Expand All @@ -797,11 +803,17 @@ module.exports = grammar({
)),
),

new_expression: $ => prec.right('new', seq(
'new',
field('constructor', choice($.primary_expression, $.new_expression)),
field('arguments', optional(prec.dynamic(1, $.arguments))),
)),
new_expression: $ => choice(
prec('new_args', seq(
'new',
field('constructor', $.primary_expression),
field('arguments', $.arguments),
)),
prec('new_no_args', seq(
'new',
field('constructor', $.primary_expression),
)),
),

await_expression: $ => prec('unary_void', seq(
'await',
Expand Down
Loading
Loading