Skip to content

Commit

Permalink
Add optional_chain named field
Browse files Browse the repository at this point in the history
Optional chaining property/array access and optional function
calls now produce an `optional_chain` named field.

When traversing a parsed program, once can retrieve the operator by
looking at the source code, indeed, but it seems _hacky_ (in a bad
sense).
  • Loading branch information
stackmystack committed Aug 18, 2022
1 parent 3dac6b0 commit 186f2ad
Show file tree
Hide file tree
Showing 7 changed files with 34,600 additions and 34,280 deletions.
8 changes: 5 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,16 @@ module.exports = grammar({
_call_signature: $ => field('parameters', $.formal_parameters),
_formal_parameter: $ => choice($.pattern, $.assignment_pattern),

optional_chain: $ => '?.',

call_expression: $ => choice(
prec('call', seq(
field('function', $.expression),
field('arguments', choice($.arguments, $.template_string))
)),
prec('member', seq(
field('function', $.primary_expression),
'?.',
field('optional_chain', $.optional_chain),
field('arguments', $.arguments)
))
),
Expand All @@ -753,15 +755,15 @@ module.exports = grammar({

member_expression: $ => prec('member', seq(
field('object', choice($.expression, $.primary_expression)),
choice('.', '?.'),
choice('.', field('optional_chain', $.optional_chain)),
field('property', choice(
$.private_property_identifier,
alias($.identifier, $.property_identifier)))
)),

subscript_expression: $ => prec.right('member', seq(
field('object', choice($.expression, $.primary_expression)),
optional('?.'),
optional(field('optional_chain', $.optional_chain)),
'[', field('index', $._expressions), ']'
)),

Expand Down
2 changes: 1 addition & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@

[
";"
"?."
(optional_chain)
"."
","
] @punctuation.delimiter
Expand Down
28 changes: 22 additions & 6 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -3244,6 +3244,10 @@
}
]
},
"optional_chain": {
"type": "STRING",
"value": "?."
},
"call_expression": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -3296,8 +3300,12 @@
}
},
{
"type": "STRING",
"value": "?."
"type": "FIELD",
"name": "optional_chain",
"content": {
"type": "SYMBOL",
"name": "optional_chain"
}
},
{
"type": "FIELD",
Expand Down Expand Up @@ -3410,8 +3418,12 @@
"value": "."
},
{
"type": "STRING",
"value": "?."
"type": "FIELD",
"name": "optional_chain",
"content": {
"type": "SYMBOL",
"name": "optional_chain"
}
}
]
},
Expand Down Expand Up @@ -3467,8 +3479,12 @@
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "?."
"type": "FIELD",
"name": "optional_chain",
"content": {
"type": "SYMBOL",
"name": "optional_chain"
}
},
{
"type": "BLANK"
Expand Down
38 changes: 34 additions & 4 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,16 @@
"named": true
}
]
},
"optional_chain": {
"multiple": false,
"required": false,
"types": [
{
"type": "optional_chain",
"named": true
}
]
}
}
},
Expand Down Expand Up @@ -2056,6 +2066,16 @@
}
]
},
"optional_chain": {
"multiple": false,
"required": false,
"types": [
{
"type": "optional_chain",
"named": true
}
]
},
"property": {
"multiple": false,
"required": true,
Expand Down Expand Up @@ -2633,6 +2653,16 @@
"named": true
}
]
},
"optional_chain": {
"multiple": false,
"required": false,
"types": [
{
"type": "optional_chain",
"named": true
}
]
}
}
},
Expand Down Expand Up @@ -3244,10 +3274,6 @@
"type": "?",
"named": false
},
{
"type": "?.",
"named": false
},
{
"type": "??",
"named": false
Expand Down Expand Up @@ -3424,6 +3450,10 @@
"type": "of",
"named": false
},
{
"type": "optional_chain",
"named": true
},
{
"type": "private_property_identifier",
"named": true
Expand Down
Loading

0 comments on commit 186f2ad

Please sign in to comment.