Skip to content

Commit 1a89962

Browse files
committed
unified: Do not emit plain strings as interpolations
1 parent 6490cce commit 1a89962

16 files changed

Lines changed: 32 additions & 76 deletions

unified/extractor/src/languages/swift/swift.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
217217
// Plain string literals (no interpolation)
218218
coerce_to_pattern!(simpleStringLiteralExpr),
219219
rule!((simpleStringLiteralExpr) @@node => (string_literal #{node})),
220-
// String literals with interpolation
220+
// String literals, possibly with interpolation. Flatten to string_literal if no interpolation.
221221
coerce_to_pattern!(stringLiteralExpr),
222+
rule!(
223+
(stringLiteralExpr segments: (stringSegment) segments: _* @@rest) @@node
224+
where rest.is_empty()
225+
=>
226+
(string_literal #{node}) // Note: capture entire 'stringLiteralExpr' to preserve quotation marks
227+
),
222228
rule!(
223229
(stringLiteralExpr segments: _* @segs)
224230
=>

unified/extractor/tests/corpus/swift/collections/tuple-literal.output

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ top_level
6060
argument
6161
value: int_literal "1"
6262
argument
63-
value:
64-
string_interpolation_expr
65-
element: string_literal "two"
63+
value: string_literal "\"two\""
6664
argument
6765
value: float_literal "3.0"

unified/extractor/tests/corpus/swift/control-flow/binding-modifier-does-not-leak-to-sibling.output

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ top_level
114114
identifier: identifier "print"
115115
argument:
116116
argument
117-
value:
118-
string_interpolation_expr
119-
element: string_literal "matched"
117+
value: string_literal "\"matched\""
120118
switch_case
121119
body:
122120
block

unified/extractor/tests/corpus/swift/control-flow/defer-statement.output

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,4 @@ top_level
9191
identifier: identifier "print"
9292
argument:
9393
argument
94-
value:
95-
string_interpolation_expr
96-
element: string_literal "work"
94+
value: string_literal "\"work\""

unified/extractor/tests/corpus/swift/control-flow/switch-case-item-where-clauses.output

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ top_level
181181
identifier: identifier "print"
182182
argument:
183183
argument
184-
value:
185-
string_interpolation_expr
186-
element: string_literal "positive"
184+
value: string_literal "\"positive\""
187185
switch_case
188186
pattern:
189187
or_pattern
@@ -210,9 +208,7 @@ top_level
210208
identifier: identifier "print"
211209
argument:
212210
argument
213-
value:
214-
string_interpolation_expr
215-
element: string_literal "non-positive"
211+
value: string_literal "\"non-positive\""
216212
switch_case
217213
body:
218214
block
@@ -223,6 +219,4 @@ top_level
223219
identifier: identifier "print"
224220
argument:
225221
argument
226-
value:
227-
string_interpolation_expr
228-
element: string_literal "other"
222+
value: string_literal "\"other\""

unified/extractor/tests/corpus/swift/control-flow/switch-expression-pattern.output

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ top_level
356356
name_expr
357357
identifier: identifier "z"
358358
expr_equality_pattern
359-
expr:
360-
string_interpolation_expr
361-
element: string_literal "foo"
359+
expr: string_literal "\"foo\""
362360
expr_equality_pattern
363361
expr: int_literal "4"
364362
expr_equality_pattern
@@ -436,9 +434,7 @@ top_level
436434
identifier: identifier "print"
437435
argument:
438436
argument
439-
value:
440-
string_interpolation_expr
441-
element: string_literal "expr"
437+
value: string_literal "\"expr\""
442438
switch_case
443439
pattern:
444440
expr_equality_pattern
@@ -460,9 +456,7 @@ top_level
460456
identifier: identifier "print"
461457
argument:
462458
argument
463-
value:
464-
string_interpolation_expr
465-
element: string_literal "ternary is also valid"
459+
value: string_literal "\"ternary is also valid\""
466460
switch_case
467461
body:
468462
block

unified/extractor/tests/corpus/swift/control-flow/switch-statement.output

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ top_level
142142
identifier: identifier "print"
143143
argument:
144144
argument
145-
value:
146-
string_interpolation_expr
147-
element: string_literal "one"
145+
value: string_literal "\"one\""
148146
switch_case
149147
pattern:
150148
or_pattern
@@ -162,9 +160,7 @@ top_level
162160
identifier: identifier "print"
163161
argument:
164162
argument
165-
value:
166-
string_interpolation_expr
167-
element: string_literal "two or three"
163+
value: string_literal "\"two or three\""
168164
switch_case
169165
body:
170166
block
@@ -175,6 +171,4 @@ top_level
175171
identifier: identifier "print"
176172
argument:
177173
argument
178-
value:
179-
string_interpolation_expr
180-
element: string_literal "other"
174+
value: string_literal "\"other\""

unified/extractor/tests/corpus/swift/control-flow/switch-tuple-pattern.output

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,4 @@ top_level
178178
identifier: identifier "print"
179179
argument:
180180
argument
181-
value:
182-
string_interpolation_expr
183-
element: string_literal "origin"
181+
value: string_literal "\"origin\""

unified/extractor/tests/corpus/swift/control-flow/switch-with-binding-pattern.output

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,7 @@ top_level
401401
identifier: identifier "print"
402402
argument:
403403
argument
404-
value:
405-
string_interpolation_expr
406-
element: string_literal "foo"
404+
value: string_literal "\"foo\""
407405
switch_case
408406
pattern:
409407
expr_equality_pattern
@@ -422,9 +420,7 @@ top_level
422420
identifier: identifier "print"
423421
argument:
424422
argument
425-
value:
426-
string_interpolation_expr
427-
element: string_literal "foo"
423+
value: string_literal "\"foo\""
428424
switch_case
429425
pattern:
430426
expr_equality_pattern
@@ -445,9 +441,7 @@ top_level
445441
identifier: identifier "print"
446442
argument:
447443
argument
448-
value:
449-
string_interpolation_expr
450-
element: string_literal "foo"
444+
value: string_literal "\"foo\""
451445
switch_case
452446
pattern:
453447
constructor_pattern
@@ -492,6 +486,4 @@ top_level
492486
identifier: identifier "print"
493487
argument:
494488
argument
495-
value:
496-
string_interpolation_expr
497-
element: string_literal "is T"
489+
value: string_literal "\"is T\""

unified/extractor/tests/corpus/swift/control-flow/switch-with-labeled-case-pattern-arguments.output

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ top_level
154154
identifier: identifier "print"
155155
argument:
156156
argument
157-
value:
158-
string_interpolation_expr
159-
element: string_literal "yes"
157+
value: string_literal "\"yes\""
160158
switch_case
161159
pattern:
162160
constructor_pattern

0 commit comments

Comments
 (0)