Skip to content

Commit d802f3e

Browse files
Support for trailing commas & skip Comments with concrete syntax (#596)
Co-authored-by: Ameya Ketkar <94497232+ketkarameya@users.noreply.github.com>
1 parent a699392 commit d802f3e

File tree

7 files changed

+67
-74
lines changed

7 files changed

+67
-74
lines changed

experimental/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tree-sitter
1+
tree-sitter==0.20.1
22
tree-sitter-languages
33
attrs
44
openai
@@ -8,4 +8,4 @@ pytest
88
flask
99
flask-socketio
1010
comby
11-
eventlet
11+
eventlet

src/models/concrete_syntax.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use regex::Regex;
1717

1818
use std::collections::HashMap;
1919
use tree_sitter::{Node, TreeCursor};
20+
use tree_sitter_traversal::Cursor;
2021

2122
use crate::models::capture_group_patterns::ConcreteSyntax;
2223
use crate::models::matches::Match;
@@ -128,7 +129,13 @@ pub(crate) fn get_matches_for_node(
128129
);
129130
}
130131

131-
let node = cursor.node();
132+
let mut node = cursor.node();
133+
134+
// Skip comment nodes always
135+
while node.kind().contains("comment") && cursor.goto_next_sibling() {
136+
node = cursor.node();
137+
}
138+
132139
// In case the template starts with :[var_name], we try match
133140
if let Some(caps) = RE_VAR.captures(match_template) {
134141
let var_name = &caps["var_name"];
@@ -148,6 +155,15 @@ pub(crate) fn get_matches_for_node(
148155
let current_node_code = current_node.utf8_text(source_code).unwrap();
149156
find_next_sibling(&mut tmp_cursor);
150157

158+
// Support for trailing commas
159+
// This skips trailing commas as we are parsing through the match template
160+
// Skips the comma node if the template doesn't contain it.
161+
let next_node = tmp_cursor.node();
162+
let next_node_text = next_node.utf8_text(source_code).unwrap();
163+
if next_node_text == "," && !meta_advanced.0.starts_with(',') {
164+
find_next_sibling(&mut tmp_cursor); // Skip comma
165+
}
166+
151167
if let (mut recursive_matches, true) =
152168
get_matches_for_node(&mut tmp_cursor, source_code, &meta_advanced)
153169
{

src/models/unit_tests/concrete_syntax_test.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
use crate::models::capture_group_patterns::ConcreteSyntax;
1515
use crate::models::concrete_syntax::get_all_matches_for_concrete_syntax;
16+
use crate::models::default_configs::GO;
1617
use crate::models::{default_configs::JAVA, language::PiranhaLanguage};
1718

1819
fn run_test(
1920
code: &str, pattern: &str, expected_matches: usize, expected_vars: Vec<Vec<(&str, &str)>>,
21+
language: &str,
2022
) {
21-
let java = PiranhaLanguage::from(JAVA);
23+
let java = PiranhaLanguage::from(language);
2224
let mut parser = java.parser();
2325
let tree = parser.parse(code.as_bytes(), None).unwrap();
2426
let meta = ConcreteSyntax(String::from(pattern));
@@ -49,6 +51,7 @@ fn test_single_match() {
4951
"public int :[name] = :[value];",
5052
1,
5153
vec![vec![("name", "a"), ("value", "10")]],
54+
JAVA,
5255
);
5356
}
5457

@@ -62,6 +65,7 @@ fn test_multiple_match() {
6265
vec![("name", "a"), ("value", "10")],
6366
vec![("name", "b"), ("value", "20")],
6467
],
68+
JAVA,
6569
);
6670
}
6771

@@ -72,5 +76,19 @@ fn test_no_match() {
7276
"public String :[name] = :[value];",
7377
0,
7478
vec![],
79+
JAVA,
80+
);
81+
}
82+
83+
#[test]
84+
fn test_trailing_comma() {
85+
run_test(
86+
"a.foo(x, // something about the first argument
87+
y, // something about the second argumet
88+
);",
89+
":[var].foo(:[arg1], :[arg2])",
90+
2,
91+
vec![vec![("var", "a"), ("arg1", "x"), ("arg2", "y")]],
92+
GO,
7593
);
7694
}

test-resources/java/feature_flag_system_2/control/input/ExperimentInterface.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
interface SomeParameter {
1515

16-
@BoolParam(key = "STALE_FLAG", namespace = "some_long_name")
17-
BoolParameter isStaleFeature();
16+
1817

1918
@BoolParam(key = "other_flag", namespace = "some_long_name")
2019
BoolParameter isOtherFlag();

test-resources/java/feature_flag_system_2/control/input/ExperimentInterfaceAnnotatedInterface.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
@ParameterDefinition(namespace = "some_long_name")
1515
interface SomeParameter {
1616

17-
@BoolParam(key = "STALE_FLAG")
18-
BoolParameter isStaleFeature();
17+
1918

2019
@BoolParam(key = "other_flag", namespace = "some_long_name")
2120
BoolParameter isOtherFlag();

test-resources/java/feature_flag_system_2/control/input/XPFlagCleanerInterfaceMethod.java

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,39 @@ class XPFlagCleanerPositiveCases {
1717

1818
private ExperimentInterface experimentation;
1919

20-
private boolean ftBool = experimentation.isStaleFeature().getCachedValue();
20+
2121

2222
public void conditional_contains_stale_flag() {
2323

24-
if (experimentation.isStaleFeature().getCachedValue()) {
25-
System.out.println("Hello World");
26-
}
24+
System.out.println("Hello World");
2725
}
2826

2927
public void conditional_with_else_contains_stale_flag() {
3028

31-
if (experimentation.isStaleFeature().getCachedValue()) {
32-
System.out.println("Hello World");
33-
} else {
34-
System.out.println("Hi world");
35-
}
29+
System.out.println("Hello World");
3630
}
3731

3832
public void conditional_with_else_contains_stale_flag_tbool() {
3933

40-
bool tBool = exp.isStaleFeature().getCachedValue();
41-
if (tBool && true) {
42-
System.out.println("Hello World");
43-
} else {
44-
System.out.println("Hi world");
45-
}
34+
35+
System.out.println("Hello World");
4636
}
4737

4838
public void conditional_with_else_contains_stale_flag_tbool(int a) {
4939

50-
bool tBool = exp.isStaleFeature().getCachedValue();
51-
if (tBool && true) {
52-
System.out.println("Hello World");
53-
} else {
54-
System.out.println("Hi world");
55-
}
40+
41+
System.out.println("Hello World");
5642
}
5743

5844
public void conditional_with_else_contains_stale_flag_tbool(int a, bool abc) {
5945

60-
bool tBool = exp.isStaleFeature().getCachedValue();
61-
if (!tBool && true) {
62-
System.out.println("Hello World");
63-
} else {
64-
System.out.println("Hi world");
65-
}
46+
47+
System.out.println("Hi world");
6648
}
6749

6850
public void conditional_with_else_contains_stale_flag_tbool_reassigned(int a, bool abc, int z) {
6951
// Currently if there is another assignment, variable will not be inlined.
70-
bool tBool = exp.isStaleFeature().getCachedValue();
52+
bool tBool = true;
7153
tBool = abc() && tBool;
7254
if (!tBool && true) {
7355
System.out.println("Hello World");
@@ -79,40 +61,28 @@ public void conditional_with_else_contains_stale_flag_tbool_reassigned(int a, bo
7961
public void conditional_with_else_contains_stale_flag_tbool_reassigned_to_same_val(
8062
int a, bool abc, int z) {
8163

82-
bool tBool = exp.isStaleFeature().getCachedValue();
83-
tBool = true;
84-
if (!tBool && true) {
85-
System.out.println("Hello World");
86-
} else {
87-
System.out.println("Hi world");
88-
}
64+
65+
66+
System.out.println("Hi world");
8967
}
9068

9169
public void conditional_with_else_contains_stale_flag_ftbool(int a) {
9270

93-
if (ftBool && true) {
94-
System.out.println("Hello World");
95-
} else {
96-
System.out.println("Hi world");
97-
}
71+
System.out.println("Hello World");
9872
}
9973

10074
public void conditional_with_else_contains_stale_flag_tbool_reassigned_ftbool(
10175
int a, bool abc, int z) {
10276
// Currently if there is another assignment, variable will not be inlined.
103-
ftBool = exp.isStaleFeature().getCachedValue();
104-
if (!ftBool && true) {
105-
System.out.println("Hello World");
106-
} else {
107-
System.out.println("Hi world");
108-
}
77+
78+
System.out.println("Hi world");
10979
}
11080

11181
public void conditional_with_else_contains_stale_flag_tbool_reassigned_ftbool_1(
11282
int a, bool abc, int z) {
11383
// Currently if there is another assignment, variable will not be inlined.
11484
bool ftBool = abc();
115-
ftBool = exp.isStaleFeature().getCachedValue();
85+
ftBool = true;
11686
if (!ftBool && true) {
11787
System.out.println("Hello World");
11888
} else {

test-resources/java/feature_flag_system_2/control/input/XPMethodChainCases.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,23 @@ public void testDontMatchNonInstanceNested() {
2626
public static void foobar(Parameter cp) {
2727
SomeParam sp = SomeParam.create(cp);
2828
// Matches API
29-
if (sp.isStaleFeature().getCachedValue()) {
30-
System.out.println("!");
31-
}
29+
System.out.println("!");
3230
// Matches API
33-
if (!sp.isStaleFeature().getCachedValue()) {
34-
System.out.println("!!!");
35-
}
31+
3632
// Does not match API
3733
if (sp.otherFlag().getCachedValue()) {
3834
System.out.println("!!!");
3935
}
40-
if (sp.otherFlag().getCachedValue() && sp.isStaleFeature().getCachedValue()) {
41-
System.out.println("!!!");
42-
}
43-
if (sp.otherFlag().getCachedValue() || sp.isStaleFeature().getCachedValue()) {
36+
if (sp.otherFlag().getCachedValue()) {
4437
System.out.println("!!!");
4538
}
39+
System.out.println("!!!");
4640
// test for identifier && false
47-
if (a && sp.isStaleFeature().getCachedValue()){
41+
if (a){
4842
System.out.println("!!! Testing identifier conjunction false");
4943
}
5044
// test for identifier || true
51-
if (a || !sp.isStaleFeature().getCachedValue()){
45+
if (a){
5246
System.out.println("!!!! Testing identifier disjunction true");
5347
}
5448
SomeParamRev spr = SomeParamRev.create(cp);
@@ -72,8 +66,8 @@ public static void foobar(Parameter cp) {
7266

7367
System.out.println("done!");
7468
// Matches API
75-
cp.put(sp.isStaleFeature(), true);
76-
cp.put(sp.isStaleFeature(), false);
69+
70+
7771

7872
// Do not match API
7973
cp.put(sp.otherFlag(), true);
@@ -82,16 +76,13 @@ public static void foobar(Parameter cp) {
8276

8377
class TestMethodChainTest {
8478
// Matches annotation
85-
@ParameterValue(ns = "some_long_name", key = "STALE_FLAG", val = "true")
79+
8680
public void testSomethingTreated() {
8781
System.out.println();
8882
}
8983

9084
// Matches annotation
91-
@ParameterValue(ns = "some_long_name", key = "STALE_FLAG", val = "false")
92-
public void testSomethingControl() {
93-
System.out.println();
94-
}
85+
9586

9687
// Does not match annotation
9788
@ParameterValue(ns = "some_long_name", key = "other_flag", val = "false")

0 commit comments

Comments
 (0)