Skip to content

Commit e24575c

Browse files
authored
fix: W-8095488 (ternary without space gets treated as optional chaining) (#80)
1 parent 5ebbc46 commit e24575c

File tree

6 files changed

+99
-4
lines changed

6 files changed

+99
-4
lines changed

grammars/apex.tmLanguage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3798,7 +3798,7 @@
37983798
<key>conditional-operator</key>
37993799
<dict>
38003800
<key>begin</key>
3801-
<string>(?&lt;!\?)\?(?!\?|\.|\[)</string>
3801+
<string>(?&lt;!\?)\?(?!\?|\.(?!\d)|\[)</string>
38023802
<key>beginCaptures</key>
38033803
<dict>
38043804
<key>0</key>

grammars/apex.tmLanguage.cson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,7 @@ repository:
22422242
}
22432243
]
22442244
'conditional-operator':
2245-
begin: '(?<!\\?)\\?(?!\\?|\\.|\\[)'
2245+
begin: '(?<!\\?)\\?(?!\\?|\\.(?!\\d)|\\[)'
22462246
beginCaptures:
22472247
'0':
22482248
name: 'keyword.operator.conditional.question-mark.apex'

grammars/soql.tmLanguage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3780,7 +3780,7 @@
37803780
<key>conditional-operator</key>
37813781
<dict>
37823782
<key>begin</key>
3783-
<string>(?&lt;!\?)\?(?!\?|\.|\[)</string>
3783+
<string>(?&lt;!\?)\?(?!\?|\.(?!\d)|\[)</string>
37843784
<key>beginCaptures</key>
37853785
<dict>
37863786
<key>0</key>

src/apex.tmLanguage.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,8 @@ repository:
13421342
# Only match ? if:
13431343
# 1. There isn't a preceding or trailing ? (null-coalescing operator)
13441344
# 2. There isn't a trailing . or [ (null-conditional operator)
1345-
begin: (?<!\?)\?(?!\?|\.|\[)
1345+
# 3. UNLESS the trailing . is followed by a digit (decimal literal like ?.34)
1346+
begin: (?<!\?)\?(?!\?|\.(?!\d)|\[)
13461347
beginCaptures:
13471348
'0': { name: keyword.operator.conditional.question-mark.apex }
13481349
end: ':'
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* W-8095488: Ternary expression with decimals syntax highlighting
3+
*
4+
* When a ternary expression contains a decimal literal and spaces are excluded,
5+
* the question mark may be incorrectly tokenized as a safe navigation operator
6+
* instead of a ternary operator.
7+
*/
8+
public class TernaryWithDecimals_W8095488 {
9+
10+
// With spaces - should work correctly
11+
public void ternaryWithSpaces() {
12+
Boolean b = true;
13+
Decimal d = b ? .34 : 0;
14+
Decimal i = b?.123 : 0;
15+
}
16+
17+
// Without spaces - potential tokenization issue
18+
public void ternaryWithoutSpaces() {
19+
Boolean b = true;
20+
Decimal d = b?.34:0;
21+
Decimal i = b?.123:0;
22+
}
23+
24+
// Mixed scenarios
25+
public void mixedScenarios() {
26+
Boolean b = true;
27+
28+
// Space before question mark, no space after
29+
Decimal d1 = b ?.34:0;
30+
31+
// No space before, space after
32+
Decimal d2 = b? .34:0;
33+
34+
// Space after question mark, no space around colon
35+
Decimal d3 = b? .34:0;
36+
37+
// All spaces
38+
Decimal d4 = b ? .34 : 0;
39+
40+
// Integer values (for comparison)
41+
Integer i1 = b?1:0;
42+
Integer i2 = b ? 1 : 0;
43+
}
44+
45+
// Safe navigation operator (for comparison)
46+
public void safeNavigationOperator() {
47+
String s = 'test';
48+
Integer len = s?.length();
49+
50+
Account acc;
51+
String name = acc?.Name;
52+
}
53+
54+
// Complex expressions
55+
public void complexExpressions() {
56+
Boolean b = true;
57+
58+
// Ternary in switch
59+
switch on (b) {
60+
when true {
61+
Decimal d = b?.34:0;
62+
String s = 'SUCCESS';
63+
}
64+
}
65+
66+
// Nested ternary with decimals
67+
Decimal nested = b ? (b?.5:.25) : .0;
68+
69+
// Ternary with decimal in variable declaration
70+
Decimal inline = b?.99:0;
71+
}
72+
73+
// Property access vs ternary
74+
public void propertyVsTernary() {
75+
Boolean b = true;
76+
77+
// These should be ternary operators, not safe navigation
78+
Decimal d = b?.34:0;
79+
Decimal i = b?.123:0;
80+
81+
// This is safe navigation
82+
String type = this?.type;
83+
}
84+
85+
String type = 'test';
86+
}
87+
88+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>58.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
6+

0 commit comments

Comments
 (0)