Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): handle string line continuation #1610

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type: module
id: 0
range: 1:1 - 12:1
range: 1:1 - 14:3
dataflow_sources:
- 1
- 5
Expand All @@ -12,6 +12,7 @@ dataflow_sources:
- 37
- 42
- 49
- 58
children:
- type: expression_statement
id: 1
Expand Down Expand Up @@ -284,6 +285,31 @@ children:
- type: '"""'
id: 57
range: 11:14 - 11:15
- type: expression_statement
id: 58
range: 13:1 - 14:3
dataflow_sources:
- 59
children:
- type: string
id: 59
range: 13:1 - 14:3
dataflow_sources:
- 60
- 61
- 62
children:
- type: '"""'
id: 60
range: 13:1 - 13:2
- type: escape_sequence
id: 61
range: 13:3 - 14:1
content: |
\
- type: '"""'
id: 62
range: 14:2 - 14:3

- node: 2
content: '''a'''
Expand Down Expand Up @@ -342,6 +368,13 @@ children:
data:
value: foo
isliteral: true
- node: 59
content: |-
"a\
b"
data:
value: ab
isliteral: true
- node: 11
content: '"a"'
data:
Expand Down
14 changes: 10 additions & 4 deletions internal/languages/python/detectors/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ func handleTemplateString(node *tree.Node, detectorContext types.Context) ([]int

switch {
case child.Type() == "escape_sequence":
value, err := stringutil.Unescape(child.Content())
if err != nil {
return fmt.Errorf("failed to decode escape sequence: %w", err)
// tree sitter parser doesn't handle line continuation inside a string
if child.Content() == "\\\n" || child.Content() == "\\\r\n" {
childValue = ""
} else {
value, err := stringutil.Unescape(child.Content())
if err != nil {
return fmt.Errorf("failed to decode escape sequence '%s': %w", child.Content(), err)
}

childValue = value
}

childValue = value
childIsLiteral = true
case len(namedChildren) == 0:
childValue = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

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

"a\
b"
Loading