Skip to content

Commit ee9900a

Browse files
committed
Properly parse nested ternary expressions
The previous implementation of the template grammar for ternary expressions would not match nested ternary expressions. In this commit we update the matcher to use the TypeScript TM grammar's implementation to do this correctly. Screenshots attached to the PR for this commit demonstrate the change in a "real" template. Closes #957
1 parent d291a15 commit ee9900a

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

syntaxes/expression.json

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -563,27 +563,15 @@
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": "(?=\\?)(?!\\?\\.)",
567-
"end": "(?=$|\"|[;,})\\]])",
568-
"patterns": [
569-
{
570-
"include": "#ternaryOperator"
571-
},
572-
{
573-
"include": "#ngExpression"
574-
}
575-
]
576-
},
577-
"ternaryOperator": {
578-
"begin": "(\\?)(?!\\.)",
566+
"begin": "(?!\\?\\.\\s*[^[:digit:]])(\\?)(?!\\?)",
579567
"beginCaptures": {
580-
"0": {
568+
"1": {
581569
"name": "keyword.operator.ternary.ts"
582570
}
583571
},
584-
"end": "(:)",
572+
"end": "\\s*(:)",
585573
"endCaptures": {
586-
"0": {
574+
"1": {
587575
"name": "keyword.operator.ternary.ts"
588576
}
589577
},

syntaxes/src/expression.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -594,28 +594,15 @@ export const Expression: GrammarDefinition = {
594594
},
595595

596596
ternaryExpression: {
597-
begin: /(?=\?)(?!\?\.)/,
598-
end: /(?=$|"|[;,})\]])/,
599-
patterns: [
600-
{
601-
include: '#ternaryOperator',
602-
},
603-
{
604-
include: '#ngExpression',
605-
},
606-
],
607-
},
608-
609-
ternaryOperator: {
610-
begin: /(\?)(?!\.)/,
597+
begin: /(?!\?\.\s*[^[:digit:]])(\?)(?!\?)/,
611598
beginCaptures: {
612-
0: {
599+
1: {
613600
name: 'keyword.operator.ternary.ts',
614601
},
615602
},
616-
end: /(:)/,
603+
end: /\s*(:)/,
617604
endCaptures: {
618-
0: {
605+
1: {
619606
name: 'keyword.operator.ternary.ts',
620607
},
621608
},

syntaxes/test/data/expression.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
{{ condition ? ['123'] : { test: 'a' } }}
7171
{{ condition ? 'test' : "test" }}
7272
{{ condition ? [function(), variable] : {} }}
73+
{{ condition ? condition2 ? condition3 ? 1 : 2 : 3 : 4 }}
7374

7475
<!-- Microsyntax -->
7576
<!-- Let Expression -->

syntaxes/test/data/expression.html.snap

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,7 @@
756756
# ^ template.ng expression.ng string.quoted.single.ts punctuation.definition.string.begin.ts
757757
# ^ template.ng expression.ng string.quoted.single.ts
758758
# ^ template.ng expression.ng string.quoted.single.ts punctuation.definition.string.end.ts
759-
# ^ template.ng expression.ng
760-
# ^^ template.ng expression.ng
759+
# ^^^ template.ng expression.ng
761760
# ^^ template.ng punctuation.definition.block.ts
762761
>{{ condition ? 'test' : "test" }}
763762
#^^ template.ng punctuation.definition.block.ts
@@ -794,9 +793,38 @@
794793
# ^ template.ng expression.ng meta.array.literal.ts meta.brace.square.ts
795794
# ^ template.ng expression.ng
796795
# ^ template.ng expression.ng keyword.operator.ternary.ts
797-
# ^^ template.ng expression.ng
798-
# ^^ template.ng expression.ng
796+
# ^^^^ template.ng expression.ng
799797
# ^^ template.ng punctuation.definition.block.ts
798+
>{{ condition ? condition2 ? condition3 ? 1 : 2 : 3 : 4 }}
799+
#^^ template.ng punctuation.definition.block.ts
800+
# ^ template.ng expression.ng
801+
# ^^^^^^^^^ template.ng expression.ng variable.other.readwrite.ts
802+
# ^ template.ng expression.ng
803+
# ^ template.ng expression.ng keyword.operator.ternary.ts
804+
# ^ template.ng expression.ng
805+
# ^^^^^^^^^^ template.ng expression.ng variable.other.readwrite.ts
806+
# ^ template.ng expression.ng
807+
# ^ template.ng expression.ng keyword.operator.ternary.ts
808+
# ^ template.ng expression.ng
809+
# ^^^^^^^^^^ template.ng expression.ng variable.other.readwrite.ts
810+
# ^ template.ng expression.ng
811+
# ^ template.ng expression.ng keyword.operator.ternary.ts
812+
# ^ template.ng expression.ng
813+
# ^ template.ng expression.ng constant.numeric.decimal.ts
814+
# ^ template.ng expression.ng
815+
# ^ template.ng expression.ng keyword.operator.ternary.ts
816+
# ^ template.ng expression.ng
817+
# ^ template.ng expression.ng constant.numeric.decimal.ts
818+
# ^ template.ng expression.ng
819+
# ^ template.ng expression.ng keyword.operator.ternary.ts
820+
# ^ template.ng expression.ng
821+
# ^ template.ng expression.ng constant.numeric.decimal.ts
822+
# ^ template.ng expression.ng
823+
# ^ template.ng expression.ng keyword.operator.ternary.ts
824+
# ^ template.ng expression.ng
825+
# ^ template.ng expression.ng constant.numeric.decimal.ts
826+
# ^ template.ng expression.ng
827+
# ^^ template.ng punctuation.definition.block.ts
800828
>
801829
><!-- Microsyntax -->
802830
#^^^^^^^^^^^^^^^^^^^^^ template.ng

0 commit comments

Comments
 (0)