Skip to content

Commit b81e8f3

Browse files
committed
fixup! Properly parse nested ternary expressions
1 parent ee9900a commit b81e8f3

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

syntaxes/expression.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@
563563
"match": "\\\\(x\\h{2}|[0-2][0-7]{0,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.|$)"
564564
},
565565
"ternaryExpression": {
566-
"begin": "(?!\\?\\.\\s*[^[:digit:]])(\\?)(?!\\?)",
566+
"begin": "(?!\\?\\.\\s*[^[:digit:]])(\\?)",
567567
"beginCaptures": {
568568
"1": {
569569
"name": "keyword.operator.ternary.ts"

syntaxes/src/expression.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,12 @@ export const Expression: GrammarDefinition = {
593593
match: /\\(x\h{2}|[0-2][0-7]{0,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.|$)/,
594594
},
595595

596+
// Derived from
597+
// https://github.com/sheetalkamat/TypeScript-TmLanguage-VsCode/blob/04219109f3e2090f787c0fa8743e73a4bd1ad876/syntaxes/TypeScript.tmLanguage.json#L2830-L2848
596598
ternaryExpression: {
597-
begin: /(?!\?\.\s*[^[:digit:]])(\?)(?!\?)/,
599+
// 1. negative lookahead (?!\?\.\s*[^[:digit:]]) ensures safe navigation does not match
600+
// 2. match the ternary question mark with (\?)
601+
begin: /(?!\?\.\s*[^[:digit:]])(\?)/,
598602
beginCaptures: {
599603
1: {
600604
name: 'keyword.operator.ternary.ts',

syntaxes/test/data/expression.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
{{ condition ? 'test' : "test" }}
7272
{{ condition ? [function(), variable] : {} }}
7373
{{ condition ? condition2 ? condition3 ? 1 : 2 : 3 : 4 }}
74+
<!-- Don't match safe navigations -->
75+
{{ condition?.a : 1 }}
7476

7577
<!-- Microsyntax -->
7678
<!-- Let Expression -->

syntaxes/test/data/expression.html.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,18 @@
825825
# ^ template.ng expression.ng constant.numeric.decimal.ts
826826
# ^ template.ng expression.ng
827827
# ^^ template.ng punctuation.definition.block.ts
828+
><!-- Don't match safe navigations -->
829+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.ng
830+
>{{ condition?.a : 1 }}
831+
#^^ template.ng punctuation.definition.block.ts
832+
# ^ template.ng expression.ng
833+
# ^^^^^^^^^ template.ng expression.ng variable.other.readwrite.ts
834+
# ^^ template.ng expression.ng punctuation.accessor.ts
835+
# ^ template.ng expression.ng variable.other.property.ts
836+
# ^^^ template.ng expression.ng
837+
# ^ template.ng expression.ng constant.numeric.decimal.ts
838+
# ^ template.ng expression.ng
839+
# ^^ template.ng punctuation.definition.block.ts
828840
>
829841
><!-- Microsyntax -->
830842
#^^^^^^^^^^^^^^^^^^^^^ template.ng

0 commit comments

Comments
 (0)