From 857272e833e8af8fd336573ec86107f4ebddc8bb Mon Sep 17 00:00:00 2001 From: Emilio Srougo Date: Mon, 18 Mar 2024 10:06:56 -0600 Subject: [PATCH] Add separate named node for type spreads --- grammar.js | 27 ++++++++++++++++++--------- test/corpus/type_declarations.txt | 10 +++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/grammar.js b/grammar.js index 03107fd..e75fdc3 100644 --- a/grammar.js +++ b/grammar.js @@ -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: { @@ -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( diff --git a/test/corpus/type_declarations.txt b/test/corpus/type_declarations.txt index 78187f6..652e048 100644 --- a/test/corpus/type_declarations.txt +++ b/test/corpus/type_declarations.txt @@ -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))) @@ -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