Skip to content

Commit 74a2748

Browse files
authored
Have special semicolon rules for method definitions (#182)
* use special semicolon rules for method definitions * regenerate parser Co-authored-by: resolritter <[email protected]>
1 parent d08f57f commit 74a2748

File tree

6 files changed

+407693
-403310
lines changed

6 files changed

+407693
-403310
lines changed

common/corpus/declarations.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,3 +996,25 @@ export class A extends B<C, D> implements C {}
996996
(implements_clause
997997
(type_identifier)))
998998
(class_body))))
999+
1000+
==========================================================================
1001+
Semicolon is not automatically inserted for method definitions with a body
1002+
==========================================================================
1003+
1004+
class Foo {
1005+
public bar()
1006+
{
1007+
}
1008+
}
1009+
1010+
---
1011+
1012+
(program
1013+
(class_declaration
1014+
(type_identifier)
1015+
(class_body
1016+
(method_definition
1017+
(accessibility_modifier)
1018+
(property_identifier)
1019+
(formal_parameters)
1020+
(statement_block)))))

common/define-grammar.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ module.exports = function defineGrammar(dialect) {
325325
repeat(choice(
326326
$.decorator,
327327
seq($.method_definition, optional($._semicolon)),
328+
// As it happens for functions, the semicolon insertion should not
329+
// happen if a block follows the closing paren, because then it's a
330+
// *definition*, not a declaration. Example:
331+
// public foo()
332+
// { <--- this brace made the method signature become a definition
333+
// }
334+
// The same rule applies for functions and that's why we use
335+
// "_function_signature_automatic_semicolon".
336+
seq($.method_signature, choice($._function_signature_automatic_semicolon, ',')),
328337
seq(
329338
choice(
330339
$.abstract_method_signature,

tsx/src/grammar.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6263,6 +6263,28 @@
62636263
}
62646264
]
62656265
},
6266+
{
6267+
"type": "SEQ",
6268+
"members": [
6269+
{
6270+
"type": "SYMBOL",
6271+
"name": "method_signature"
6272+
},
6273+
{
6274+
"type": "CHOICE",
6275+
"members": [
6276+
{
6277+
"type": "SYMBOL",
6278+
"name": "_function_signature_automatic_semicolon"
6279+
},
6280+
{
6281+
"type": "STRING",
6282+
"value": ","
6283+
}
6284+
]
6285+
}
6286+
]
6287+
},
62666288
{
62676289
"type": "SEQ",
62686290
"members": [

0 commit comments

Comments
 (0)