Skip to content

Commit

Permalink
Add separate named node for type spreads
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilios1995 committed Mar 20, 2024
1 parent e39df74 commit 857272e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
27 changes: 18 additions & 9 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module.exports = grammar({
[$.type_binding, $._inline_type],
[$._module_structure, $.parenthesized_module_expression],
[$.record_type_field, $.object_type_field],
[$._record_type_member, $._object_type_member],
],

rules: {
Expand Down Expand Up @@ -382,40 +383,48 @@ module.exports = grammar({

record_type: $ => seq(
'{',
commaSept($.record_type_field),
commaSept($._record_type_member),
'}',
),

record_type_field: $ => choice(
seq('...', choice($.type_identifier, $.generic_type, $.type_identifier_path)),
record_type_field: $ =>
seq(
optional('mutable'),
alias($.value_identifier, $.property_identifier),
optional('?'),
$.type_annotation,
),

type_spread: $ =>
seq('...', choice($.type_identifier, $.generic_type, $.type_identifier_path)),

_record_type_member: $ => choice(
$.record_type_field,
$.type_spread
),

object_type: $ => prec.left(seq(
'{',
choice(
commaSep1t($._object_type_field),
seq('.', commaSept($._object_type_field)),
seq('..', commaSept($._object_type_field)),
commaSep1t($._object_type_member),
seq('.', commaSept($._object_type_member)),
seq('..', commaSept($._object_type_member)),
),
'}',
)),

_object_type_field: $ => alias($.object_type_field, $.field),
_object_type_member: $ =>
choice(
alias($.object_type_field, $.field),
$.type_spread
),

object_type_field: $ => choice(
seq('...', choice($.type_identifier, $.type_identifier_path)),
seq(
alias($.string, $.property_identifier),
':',
$._type,
),

),

generic_type: $ => prec.left(seq(
Expand Down
10 changes: 5 additions & 5 deletions test/corpus/type_declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,21 @@ type t = {
(type_binding
(type_identifier)
(record_type
(record_type_field
(type_spread
(type_identifier))
(record_type_field
(type_spread
(generic_type
(type_identifier)
(type_arguments
(type_identifier))))
(record_type_field
(type_spread
(generic_type
(type_identifier_path
(module_identifier)
(type_identifier))
(type_arguments
(type_identifier))))
(record_type_field
(type_spread
(type_identifier_path
(module_identifier)
(type_identifier)))
Expand Down Expand Up @@ -478,7 +478,7 @@ type t<'a> = {.."name": string} as 'a
(property_identifier
(string_fragment))
(type_identifier))
(field
(type_spread
(type_identifier)))))
(type_declaration
(type_binding
Expand Down

0 comments on commit 857272e

Please sign in to comment.