Skip to content

Commit

Permalink
fix: support binary OR for strings (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe authored Jan 9, 2024
1 parent 90b9c30 commit e142a5d
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
type: program
id: 0
range: 1:1 - 9:1
range: 1:1 - 12:1
dataflow_sources:
- 1
- 6
- 17
- 25
- 33
- 41
- 49
children:
- type: expression_statement
id: 1
Expand Down Expand Up @@ -193,6 +195,94 @@ children:
- type: '"""'
id: 40
range: 8:18 - 8:19
- type: expression_statement
id: 41
range: 10:1 - 10:15
children:
- type: binary_expression
id: 42
range: 10:1 - 10:15
dataflow_sources:
- 43
- 44
- 45
children:
- type: "false"
id: 43
range: 10:1 - 10:6
content: "false"
- type: '"||"'
id: 44
range: 10:7 - 10:9
- type: string
id: 45
range: 10:10 - 10:15
dataflow_sources:
- 46
- 47
- 48
children:
- type: '"""'
id: 46
range: 10:10 - 10:11
- type: string_fragment
id: 47
range: 10:11 - 10:14
content: foo
- type: '"""'
id: 48
range: 10:14 - 10:15
- type: expression_statement
id: 49
range: 11:1 - 11:15
children:
- type: binary_expression
id: 50
range: 11:1 - 11:15
dataflow_sources:
- 51
- 55
- 56
children:
- type: string
id: 51
range: 11:1 - 11:6
dataflow_sources:
- 52
- 53
- 54
children:
- type: '"""'
id: 52
range: 11:1 - 11:2
- type: string_fragment
id: 53
range: 11:2 - 11:5
content: hey
- type: '"""'
id: 54
range: 11:5 - 11:6
- type: '"||"'
id: 55
range: 11:7 - 11:9
- type: string
id: 56
range: 11:10 - 11:15
dataflow_sources:
- 57
- 58
- 59
children:
- type: '"""'
id: 57
range: 11:10 - 11:11
- type: string_fragment
id: 58
range: 11:11 - 11:14
content: foo
- type: '"""'
id: 59
range: 11:14 - 11:15

- node: 2
content: '"ab"'
Expand All @@ -214,6 +304,21 @@ children:
data:
value: hello world\
isliteral: true
- node: 42
content: false || "foo"
data:
value: foo
isliteral: true
- node: 50
content: '"hey" || "foo"'
data:
value: hey
isliteral: true
- node: 50
content: '"hey" || "foo"'
data:
value: foo
isliteral: true
- node: 4
content: ab
data:
Expand Down Expand Up @@ -254,6 +359,21 @@ children:
data:
value: \
isliteral: true
- node: 45
content: '"foo"'
data:
value: foo
isliteral: true
- node: 51
content: '"hey"'
data:
value: hey
isliteral: true
- node: 56
content: '"foo"'
data:
value: foo
isliteral: true
- node: 10
content: a
data:
Expand All @@ -269,4 +389,19 @@ children:
data:
value: a
isliteral: true
- node: 47
content: foo
data:
value: foo
isliteral: true
- node: 53
content: hey
data:
value: hey
isliteral: true
- node: 58
content: foo
data:
value: foo
isliteral: true

15 changes: 14 additions & 1 deletion internal/languages/javascript/detectors/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,21 @@ func (detector *stringDetector) DetectAt(
case "template_string":
return handleTemplateString(node, detectorContext)
case "binary_expression":
if node.Children()[1].Content() == "+" {
switch node.Children()[1].Content() {
case "+":
return common.ConcatenateChildStrings(node, detectorContext)
case "||":
leftData, err := common.GetStringData(node.ChildByFieldName("left"), detectorContext)
if err != nil {
return nil, err
}

rightData, err := common.GetStringData(node.ChildByFieldName("right"), detectorContext)
if err != nil {
return nil, err
}

return append(leftData, rightData...), nil
}
case "augmented_assignment_expression":
if node.Children()[1].Content() == "+=" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ x = "a"
`${x} b`

"hello\x20world\\"

false || "foo"
"hey" || "foo"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type: module
id: 0
range: 1:1 - 12:31
range: 1:1 - 13:1
dataflow_sources:
- 1
children:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type: module
id: 0
range: 1:1 - 7:7
range: 1:1 - 11:1
dataflow_sources:
- 1
- 5
Expand All @@ -9,6 +9,8 @@ dataflow_sources:
- 21
- 25
- 33
- 37
- 44
children:
- type: expression_statement
id: 1
Expand Down Expand Up @@ -182,6 +184,81 @@ children:
- type: '"""'
id: 36
range: 7:6 - 7:7
- type: expression_statement
id: 37
range: 9:1 - 9:15
dataflow_sources:
- 38
children:
- type: boolean_operator
id: 38
range: 9:1 - 9:15
alias_of:
- 39
- 41
children:
- type: "false"
id: 39
range: 9:1 - 9:6
content: "False"
- type: '"or"'
id: 40
range: 9:7 - 9:9
- type: string
id: 41
range: 9:10 - 9:15
dataflow_sources:
- 42
- 43
children:
- type: '"""'
id: 42
range: 9:10 - 9:11
- type: '"""'
id: 43
range: 9:14 - 9:15
- type: expression_statement
id: 44
range: 10:1 - 10:15
dataflow_sources:
- 45
children:
- type: boolean_operator
id: 45
range: 10:1 - 10:15
alias_of:
- 46
- 50
children:
- type: string
id: 46
range: 10:1 - 10:6
dataflow_sources:
- 47
- 48
children:
- type: '"""'
id: 47
range: 10:1 - 10:2
- type: '"""'
id: 48
range: 10:5 - 10:6
- type: '"or"'
id: 49
range: 10:7 - 10:9
- type: string
id: 50
range: 10:10 - 10:15
dataflow_sources:
- 51
- 52
children:
- type: '"""'
id: 51
range: 10:10 - 10:11
- type: '"""'
id: 52
range: 10:14 - 10:15

- node: 2
content: '''a'''
Expand Down Expand Up @@ -218,6 +295,21 @@ children:
data:
value: a\n
isliteral: true
- node: 38
content: False or "foo"
data:
value: foo
isliteral: true
- node: 45
content: '"hey" or "foo"'
data:
value: hey
isliteral: true
- node: 45
content: '"hey" or "foo"'
data:
value: foo
isliteral: true
- node: 11
content: '"a"'
data:
Expand All @@ -228,4 +320,19 @@ children:
data:
value: b
isliteral: true
- node: 41
content: '"foo"'
data:
value: foo
isliteral: true
- node: 46
content: '"hey"'
data:
value: hey
isliteral: true
- node: 50
content: '"foo"'
data:
value: foo
isliteral: true

14 changes: 14 additions & 0 deletions internal/languages/python/detectors/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ func (detector *stringDetector) DetectAt(
if node.Children()[1].Content() == "+" {
return common.ConcatenateChildStrings(node, detectorContext)
}
case "boolean_operator":
if node.Children()[1].Content() == "or" {
leftData, err := common.GetStringData(node.ChildByFieldName("left"), detectorContext)
if err != nil {
return nil, err
}

rightData, err := common.GetStringData(node.ChildByFieldName("right"), detectorContext)
if err != nil {
return nil, err
}

return append(leftData, rightData...), nil
}
case "augmented_assignment":
if node.Children()[1].Content() == "+=" {
return common.ConcatenateAssignEquals(node, detectorContext)
Expand Down
2 changes: 1 addition & 1 deletion internal/languages/python/detectors/testdata/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def main(args):
s2 += args[0]
s2 += " there"

s3 = f"foo '{s2}' bar"
s3 = f"foo '{s2}' bar"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
"""a"""
'''a'''
f'{foo} a'
r'a\n'
r'a\n'

False or "foo"
"hey" or "foo"
Loading

0 comments on commit e142a5d

Please sign in to comment.