Skip to content

Commit caffe2d

Browse files
committed
fix: allow decorators before type and declarations
See #133
1 parent 652c0af commit caffe2d

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

grammar.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,19 @@ module.exports = grammar({
241241
choice('=', '+='),
242242
optional('private'),
243243
$._type,
244-
optional(seq(
245-
'and',
246-
$._type_declaration
247-
)),
244+
repeat(alias($._type_declaration_and, $.type_declaration)),
248245
)),
249246
),
250247

248+
_type_declaration_and: $ => seq(
249+
// New line here not necessary terminates the statement,
250+
// show this doubt to the parser
251+
repeat($._newline),
252+
repeat($.decorator),
253+
'and',
254+
$._type_declaration
255+
),
256+
251257
type_parameters: $ => seq(
252258
'<',
253259
commaSep1t($.type_identifier),

test/corpus/decorators.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,34 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
6767
(array)
6868
(number))))))
6969

70+
============================================
71+
Decorator before type `and`
72+
============================================
73+
74+
@deriving
75+
type foo = {
76+
foo: string
77+
}
78+
@deriving
79+
and bar = {bar: int}
80+
81+
82+
@deriving @hello and baz = {baz: float}
83+
84+
---
85+
86+
(source_file
87+
(decorated
88+
(decorator (decorator_identifier))
89+
(type_declaration
90+
(type_identifier)
91+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))
92+
(type_declaration
93+
(decorator (decorator_identifier))
94+
(type_identifier)
95+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))
96+
(type_declaration
97+
(decorator (decorator_identifier))
98+
(decorator (decorator_identifier))
99+
(type_identifier)
100+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))))))

test/corpus/type_declarations.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,16 @@ and teacher = {
385385
(property_identifier)
386386
(type_annotation
387387
(type_identifier))))
388-
(type_identifier)
389-
(record_type
390-
(record_type_field
391-
(property_identifier)
392-
(type_annotation
393-
(generic_type
394-
(type_identifier)
395-
(type_arguments
396-
(type_identifier))))))))
388+
(type_declaration
389+
(type_identifier)
390+
(record_type
391+
(record_type_field
392+
(property_identifier)
393+
(type_annotation
394+
(generic_type
395+
(type_identifier)
396+
(type_arguments
397+
(type_identifier)))))))))
397398

398399

399400
===========================================

0 commit comments

Comments
 (0)