Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kotlin): allow newlines in class declarations (#10015)
Fixes: SAF-899 Fixes: SAF-923 Requires: semgrep/ocaml-tree-sitter-semgrep#477 The current version of Kotlin's tree-sitter doesn't support newlines when declaring classes. It has to do with automatic semicolons. For example, the following won't parse: ``` class C constructor(arg:Int){} ``` but the following parses fine: ``` class C constructor(arg:Int){} ``` The fix requires a hack in the grammar. (Ideally the scanner seems to be the right place for a fix, but modifying the scanner is complicated.) The hack works as follows. When the parser sees the code: ``` class C constructor(arg:Int){} ``` The first line is parsed as a class declaration node, and the second line is parsed as a partial class declaration node. These need to be consolidated during CST -> Generic AST. This PR implements this merge. Test plan: * `make core-test` with new tests * ran `semgrep-core -lang kotlin -dump_ast` with some examples that have new lines in class declarations, including the one reported on Linear. The output AST shows that there's a single class definition that is correctly populated.
- Loading branch information
Showing
8 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Fixed a parsing error in Kotlin when there's a newline between the class name and the primary constructor. | ||
|
||
This could not parse before | ||
|
||
``` | ||
class C | ||
constructor(arg:Int){} | ||
``` | ||
|
||
because of the newline between the class name and the constructor. | ||
|
||
Now it's fixed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule semgrep-kotlin
updated
from b5c600 to 6011fc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class C | ||
constructor(arg: Int) | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ERROR: | ||
@Annotation1 class BiggerClass< X, Int > | ||
|
||
@Annotation2 constructor(arg: Int): BaseClass where X: Int { | ||
@Annotation3 constructor(arg1: Int, arg2: Int) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class $X <$T1, $T2> | ||
constructor (arg:Int): $BASE_CLASS { | ||
... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// ERROR: | ||
class MyClass constructor(arg: Int) {} | ||
|
||
// ERROR: | ||
class MyClass | ||
constructor(arg: Int) | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class $X | ||
|
||
constructor | ||
|
||
(arg: Int) | ||
|
||
{ | ||
|
||
} |