Skip to content

Commit 3a4935c

Browse files
authored
Merge pull request #11 from flagship-io/fix/regex
Fix/regex
2 parents d3b9a5d + 57777e6 commit 3a4935c

File tree

4 files changed

+48
-71
lines changed

4 files changed

+48
-71
lines changed

internal/files/search.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ func GetFlagType(defaultValue string) (string, string) {
1919
var flagType string = "string"
2020
var flagTypeInterface interface{}
2121

22-
r, _ := regexp.Compile(`[^\w#]`)
22+
r, _ := regexp.Compile(`[\{\}\[\]]`)
2323

2424
json.Unmarshal([]byte(defaultValue), &flagTypeInterface)
2525

2626
if match := r.MatchString(defaultValue); match {
2727
flagType = "unknown"
2828
}
2929

30-
if defaultValue[0:1] == "\"" {
31-
defaultValue = strings.Trim(defaultValue, "\"")
30+
if (defaultValue[0:1] == "\"" || defaultValue[0:1] == "'") && (defaultValue[len(defaultValue)-1:] == "\"" || defaultValue[len(defaultValue)-1:] == "'") {
3231
flagType = "string"
3332
}
3433

@@ -86,30 +85,34 @@ func SearchFiles(cfg *config.Config, path string, resultChannel chan model.FileS
8685

8786
// Add default regex for flags in commentaries
8887
flagRegexes = append(flagRegexes, model.FlagRegex{
89-
FunctionRegex: `(?s)fs:flag:(\w+)`,
90-
FieldRegex: `fs:flag:(.+)`,
88+
FieldRegex: `fs:flag:(.+)`,
9189
})
9290

9391
results := []model.SearchResult{}
9492

9593
flagIndexes := [][]int{}
9694
for _, flagRegex := range flagRegexes {
97-
regxp := regexp.MustCompile(flagRegex.FunctionRegex)
95+
regxp := regexp.MustCompile(flagRegex.FieldRegex)
9896
flagLineIndexes := regxp.FindAllStringIndex(fileContentStr, -1)
9997

10098
for _, flagLineIndex := range flagLineIndexes {
10199
submatch := fileContentStr[flagLineIndex[0]:flagLineIndex[1]]
102-
regxp := regexp.MustCompile(flagRegex.FieldRegex)
103100

104101
submatchIndexes := regxp.FindAllStringSubmatchIndex(submatch, -1)
105102

106-
for k, submatchIndex := range submatchIndexes {
107-
if len(submatchIndex) < 6 {
103+
for _, submatchIndex := range submatchIndexes {
104+
if len(submatchIndex) < 3 {
108105
log.Printf("Did not find the flag key in file %s. Code : %s", path, submatch)
109106
continue
110107
}
111-
if !flagRegex.HasMultipleKeys && k > 0 {
112-
break
108+
109+
if len(submatchIndex) < 6 {
110+
log.Printf("Did not find the flag default value in file %s. Code : %s", path, submatch)
111+
flagIndexes = append(flagIndexes, []int{
112+
flagLineIndex[0] + submatchIndex[2],
113+
flagLineIndex[0] + submatchIndex[3],
114+
})
115+
continue
113116
}
114117

115118
flagIndexes = append(flagIndexes, []int{
@@ -124,11 +127,18 @@ func SearchFiles(cfg *config.Config, path string, resultChannel chan model.FileS
124127

125128
for _, flagIndex := range flagIndexes {
126129
// Extract the code with a certain number of lines
130+
defaultValue_ := ""
131+
flagType := "unknown"
127132
firstLineIndex := getSurroundingLineIndex(fileContentStr, flagIndex[0], true, cfg.NbLineCodeEdges)
128133
lastLineIndex := getSurroundingLineIndex(fileContentStr, flagIndex[1], false, cfg.NbLineCodeEdges)
129134
code := fileContentStr[firstLineIndex:lastLineIndex]
130135
key := fileContentStr[flagIndex[0]:flagIndex[1]]
131-
defaultValue := fileContentStr[flagIndex[2]:flagIndex[3]]
136+
137+
if len(flagIndex) >= 3 {
138+
defaultValue := fileContentStr[flagIndex[2]:flagIndex[3]]
139+
flagType, defaultValue_ = GetFlagType(defaultValue)
140+
}
141+
132142
// Better value wrapper for code highlighting (5 chars wrapping)
133143
keyWrapper := key
134144
nbCharsWrapping := 5
@@ -139,8 +149,6 @@ func SearchFiles(cfg *config.Config, path string, resultChannel chan model.FileS
139149
lineNumber := getLineFromPos(fileContentStr, flagIndex[0])
140150
codeLineHighlight := getLineFromPos(code, strings.Index(code, keyWrapper))
141151

142-
flagType, defaultValue_ := GetFlagType(defaultValue)
143-
144152
results = append(results, model.SearchResult{
145153
FlagKey: key,
146154
FlagDefaultValue: defaultValue_,

internal/files/search_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ func TestSearchFiles(t *testing.T) {
6565
{name: "btnSizeInt", lineNumber: 25, codeLineHighlight: 6},
6666
},
6767
},
68-
/* {
68+
{
6969
filePath: "../../example/src/ios/SDK_V3/sample.swift",
7070
flags: []flag{
71-
{name: "btnColor", lineNumber: 9, codeLineHighlight: 5},
72-
{name: "displayVipFeature", lineNumber: 10, codeLineHighlight: 5},
73-
{name: "vipFeature", lineNumber: 11, codeLineHighlight: 5},
71+
{name: "btnColor", lineNumber: 9, codeLineHighlight: 6},
72+
{name: "displayVipFeature", lineNumber: 10, codeLineHighlight: 6},
73+
{name: "vipFeature", lineNumber: 11, codeLineHighlight: 6},
7474
},
75-
}, */
75+
},
7676
{
7777
filePath: "../../example/src/java/SDK_V2/sample.java",
7878
flags: []flag{

internal/model/language_regexes.go

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,143 +11,114 @@ type LanguageRegex struct {
1111
}
1212

1313
type FlagRegex struct {
14-
FunctionRegex string `json:"function_regex"`
15-
FieldRegex string `json:"field_regex"`
16-
HasMultipleKeys bool `json:"has_multiple_keys"`
14+
FieldRegex string `json:"field_regex"`
1715
}
1816

1917
var LanguageRegexes = []LanguageRegex{
2018
{
2119
ExtensionRegex: `\.[jt]sx?$`,
2220
FlagRegexes: []FlagRegex{
2321
{
24-
FunctionRegex: `(?s)useFsModifications\(.+?\)`, // SDK React V2
25-
FieldRegex: `['"]?key['"]?\s*\:\s*['"](.+?)['"](?:.*\s*)['"]?defaultValue['"]?\s*\:\s*['"]?(.+?)['"]?\s*[\"\,]`,
26-
HasMultipleKeys: true,
22+
FieldRegex: `useFsFlag[(](?:\s*['"](.*)['"]\s*,\s*(["'].*\s*[^"]*["']|[^)]*))\s*[)]`, // SDK React V3
2723
},
2824
{
29-
FunctionRegex: `(?s)useFsFlag\(.+?\)`, // SDK React V3
30-
FieldRegex: `useFsFlag\(['"]?\s*(.+?)['"](?:.\s*)['"]?(.+?)['"]?\s*[\"\)]`,
31-
HasMultipleKeys: true,
25+
FieldRegex: `['"]?key['"]?\s*\:\s*['"](.+?)['"](?:.*\s*)['"]?defaultValue['"]?\s*\:\s*(['"].*['"]|[^\r\n\t\f\v,}]+).*[},]?`, // SDK JS V2 && SDK React V2
3226
},
3327
{
34-
FunctionRegex: `(?s)\.getModifications\(.+?\].+?\)`, // SDK JS V2
35-
FieldRegex: `['"]?key['"]?\s*\:\s*['"](.+?)['"](?:.*\s*)['"]?defaultValue['"]?\s*\:\s*['"]?(.+?)['"]?\s*[\"\,]`,
36-
HasMultipleKeys: true,
37-
},
38-
{
39-
FunctionRegex: `(?s)getFlag\(.+?\)`, // SDK JS V3
40-
FieldRegex: `getFlag\(['"]?\s*(.+?)['"](?:.\s*)['"]?(.+?)['"]?\s*[\"\)\,]`,
41-
HasMultipleKeys: true,
28+
FieldRegex: `getFlag[(](?:\s*["'](.*)["']\s*,\s*(["'].*\s*[^"]*["']|[^)]*))\s*[)]`, // SDK JS V3
4229
},
4330
},
4431
},
4532
{
4633
ExtensionRegex: `\.go$`,
4734
FlagRegexes: []FlagRegex{
4835
{
49-
FunctionRegex: `(?s)\.GetModification(String|Number|Bool|Object|Array)\(.+?\)`, // SDK GO V2
50-
FieldRegex: `\s*['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\,]`,
36+
FieldRegex: `\.GetModification(?:String|Number|Bool|Object|Array)\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK GO V2
5137
},
5238
},
5339
},
5440
{
5541
ExtensionRegex: `\.py$`,
5642
FlagRegexes: []FlagRegex{
5743
{
58-
FunctionRegex: `(?s)\.get_modification\(.+?\)`, // SDK PYTHON V2
59-
FieldRegex: `\s*['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
44+
FieldRegex: `\.get_modification\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|True|False|\d+|"[^"]*"))?\s*\)`, // SDK PYTHON V2
6045
},
6146
},
6247
},
6348
{
6449
ExtensionRegex: `\.java$`,
6550
FlagRegexes: []FlagRegex{
6651
{
67-
FunctionRegex: `(?s)\.getModification\(.+?\)`, // SDK JAVA V2
68-
FieldRegex: `\s*['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
52+
FieldRegex: `\.getModification\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK JAVA V2
6953
},
7054
{
71-
FunctionRegex: `(?s)\.getFlag\(.+?\)`, // SDK JAVA V3
72-
FieldRegex: `(?s)\.getFlag\(['"](.+?)['"](?:.\s*)['"]?(.+?)['"]?\s*[\"\)\,]`,
55+
FieldRegex: `\.getFlag[(](?:\s*["'](.*)["']\s*,\s*(["'].*\s*[^"]*["']|[^)]*))\s*[)]`, // SDK JAVA V3
7356
},
7457
},
7558
},
7659
{
7760
ExtensionRegex: `\.php$`,
7861
FlagRegexes: []FlagRegex{
7962
{
80-
FunctionRegex: `(?s)\-\>getModification\(.+?\)`, // SDK PHP V2
81-
FieldRegex: `\s*['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
63+
FieldRegex: `\-\>getModification\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK PHP V1 && SDK PHP V2
8264
},
8365
{
84-
FunctionRegex: `(?s)\-\>getFlag\(.+?\)`, // SDK PHP V3
85-
FieldRegex: `(?s)\-\>getFlag\(['"](.+?)['"](?:.\s*)['"]?(.+?)['"]?\s*[\"\)\,]`,
66+
FieldRegex: `\-\>getFlag[(](?:\s*["'](.*)["']\s*,\s*(["'].*\s*[^"]*["']|[^)]*))\s*[)]`, // SDK PHP V3
8667
},
8768
},
8869
},
8970
{
9071
ExtensionRegex: `\.kt$`,
9172
FlagRegexes: []FlagRegex{
9273
{
93-
FunctionRegex: `(?s)\.getModification\(.+?\)`, // SDK ANDROID V2
94-
FieldRegex: `\s*['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
74+
FieldRegex: `\.getModification\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK ANDROID V2
9575
},
9676
{
97-
FunctionRegex: `(?s)\.getFlag\(.+?\)`, // SDK ANDROID V3
98-
FieldRegex: `(?s)\.getFlag\(['"](.+?)['"](?:.\s*)['"]?(.+?)['"]?\s*[\"\)\,]`,
77+
FieldRegex: `\.getFlag[(](?:\s*["'](.*)["']\s*,\s*(["'].*\s*[^"]*["']|[^)]*))\s*[)]`, // SDK ANDROID V3
9978
},
10079
},
10180
},
10281
{
10382
ExtensionRegex: `\.swift$`,
10483
FlagRegexes: []FlagRegex{
10584
{
106-
FunctionRegex: `(?s)\.getModification\(.+?\)`, // SDK iOS V2
107-
FieldRegex: `\s*['"](.+?)['"](?:,\s*)['"]?default(?:String|Double|Bool|Float|Int|Json|Array)['"]?\s*\:\s*['"]?(.+?)['"]?\s*[\"\,]`,
85+
FieldRegex: `\.getModification\(\s*["'](\w+)['"]\s*,\s*default(?:String|Double|Float|Int|Bool|Json|Array)\s*:\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)\s*(?:,\s*activate\s*:\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK iOS V2
10886
},
10987
{
110-
FunctionRegex: `(?s)\.getFlag\(key: ['"](.+?)['"]`, // SDK iOS V3
111-
FieldRegex: `['"]?key['"]?\s*\:\s*['"](.+?)['"](?:.*\s*)['"]?defaultValue['"]?\s*\:\s*['"]?(.+?)['"]?\s*[\)]`,
88+
FieldRegex: `\.getFlag[(]\s*key\s*:\s*(?:\s*["'](.*)["']\s*,\s*defaultValue\s*:\s*(["'].*\s*[^"]*["']|[^)]*))\s*[)]`, // SDK iOS V3
11289
},
11390
},
11491
},
11592
{
11693
ExtensionRegex: `\.m$`,
11794
FlagRegexes: []FlagRegex{
11895
{
119-
FunctionRegex: `(?s)\]\s*getModification:@.+?\]`, // SDK iOS V2
120-
FieldRegex: `\s*['"](.+?)['"](?:\s*)default(?:String|Double|Bool|Float|Int|Json|Array):\@?\s*(['"](.+?)['"]|YES|NO|TRUE|FALSE|true|false|\d*\.?\d+)?`,
96+
FieldRegex: `getModification\s*:\s*@\s*['"](.+?)['"](?:\s*)default(?:String|Double|Bool|Float|Int|Json|Array):\@?\s*(['"].+?['"]|YES|NO|TRUE|FALSE|true|false|[+-]?(?:\d*[.])?\d+)?`, // SDK iOS V2
12197
},
12298
{
123-
FunctionRegex: `(?s)\s*getFlagWithKey:@.+?\]`, // SDK iOS V3
124-
FieldRegex: `\s*getFlagWithKey:@['"](.+?)['"](?:\s*)['"]?defaultValue['"]?\s*\:\@?\s*['"]?(.+?)['"]?\s*[\]]`,
99+
FieldRegex: `getFlagWithKey\s*:\s*\@['"](.+?)['"](?:\s*)['"]?defaultValue['"]?\s*\:\s*\@?\s*(.+?)\s*[\]]`, // SDK iOS V3
125100
},
126101
},
127102
},
128103
{
129104
ExtensionRegex: `\.[fc]s$`,
130105
FlagRegexes: []FlagRegex{
131106
{
132-
FunctionRegex: `(?s)\.GetModification\(.+?\)`, // SDK .NET V1
133-
FieldRegex: `(?s)\.GetModification\(['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
107+
FieldRegex: `\.GetModification\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK .NET V1
134108
},
135109
{
136-
FunctionRegex: `(?s)\.GetFlag\(.+?\)`, // SDK .NET V3
137-
FieldRegex: `(?s)\.GetFlag\(['"](.+?)['"](?:.\s*)['"]?(.+?)['"]?\s*[\"\)\,]`,
110+
FieldRegex: `\.GetFlag\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK .NET V3
138111
},
139112
},
140113
},
141114
{
142115
ExtensionRegex: `\.vb$`,
143116
FlagRegexes: []FlagRegex{
144117
{
145-
FunctionRegex: `(?s)\.GetModification\(.+?\)`, // SDK .NET V1
146-
FieldRegex: `(?s)\.GetModification\(['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
118+
FieldRegex: `\.GetModification\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|True|false|False|\d+|"[^"]*"))?\s*\)`, // SDK .NET V1
147119
},
148120
{
149-
FunctionRegex: `(?s)\.GetFlag\(.+?\)`, // SDK .NET V3
150-
FieldRegex: `(?s)\.GetFlag\(['"](.+?)['"](?:,\s*)['"]?(.+?)['"]?\s*[\)\,]`,
121+
FieldRegex: `\.GetFlag\(\s*["']([\w\-]+)['"]\s*,\s*(["'][^"]*['"]|[+-]?(?:\d*[.])?\d+|true|false|False|True)(?:\s*,\s*(?:true|false|\d+|"[^"]*"))?\s*\)`, // SDK .NET V3
151122
},
152123
},
153124
},

internal/model/language_regexes_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestCustomRegex(t *testing.T) {
11-
AddCustomRegexes(`[{"extension_regex":".tsx?","flag_regexes":[{"function_regex":"(?s)useFlagShipActivation\\(.+?\\)","field_regex":"\\s*['\"](.+?)['\"]","has_multiple_keys":true}]}]`)
11+
AddCustomRegexes(`[{"extension_regex":".tsx?","flag_regexes":[{"field_regex":"\\s*['\"](.+?)['\"]"}]}]`)
1212

1313
found := funk.Find(LanguageRegexes, func(languageRegex LanguageRegex) bool {
1414
return languageRegex.ExtensionRegex == ".tsx?"
@@ -22,7 +22,5 @@ func TestCustomRegex(t *testing.T) {
2222

2323
assert.Equal(t, ".tsx?", foundLanguageRegex.ExtensionRegex)
2424
assert.Equal(t, 1, len(foundLanguageRegex.FlagRegexes))
25-
assert.Equal(t, "(?s)useFlagShipActivation\\(.+?\\)", foundLanguageRegex.FlagRegexes[0].FunctionRegex)
2625
assert.Equal(t, "\\s*['\"](.+?)['\"]", foundLanguageRegex.FlagRegexes[0].FieldRegex)
27-
assert.Equal(t, true, foundLanguageRegex.FlagRegexes[0].HasMultipleKeys)
2826
}

0 commit comments

Comments
 (0)