Skip to content

Commit fa8a986

Browse files
committed
fix: support nullish coalesce for syntax highlighting (#1376)
* refactor: format syntax test html * fix: support nullish coalesce for syntax highlighting References: microsoft/TypeScript-TmLanguage@3890a41#diff-9d173be38aa6ed456838ecca050f4bed363f34c0c01d5c1a079070bf00563b99 https://github.com/microsoft/vscode-typescript-next/blob/main/syntaxes/TypeScript.tmLanguage.json Fixes #1372 (cherry picked from commit fa36c29)
1 parent 2cab265 commit fa8a986

File tree

6 files changed

+76
-35
lines changed

6 files changed

+76
-35
lines changed

syntaxes/expression.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
},
168168
{
169169
"name": "keyword.operator.logical.ts",
170-
"match": "\\!|&&|\\|\\|"
170+
"match": "\\!|&&|\\?\\?|\\|\\|"
171171
},
172172
{
173173
"name": "keyword.operator.bitwise.ts",
@@ -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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export const Expression: GrammarDefinition = {
176176
},
177177
{
178178
name: 'keyword.operator.logical.ts',
179-
match: /\!|&&|\|\|/,
179+
match: /\!|&&|\?\?|\|\|/,
180180
},
181181
{
182182
name: 'keyword.operator.bitwise.ts',
@@ -594,11 +594,12 @@ export const Expression: GrammarDefinition = {
594594
},
595595

596596
// Derived from
597-
// https://github.com/sheetalkamat/TypeScript-TmLanguage-VsCode/blob/04219109f3e2090f787c0fa8743e73a4bd1ad876/syntaxes/TypeScript.tmLanguage.json#L2830-L2848
597+
// https://github.com/microsoft/vscode-typescript-next/blob/849bce9ce68c4b87e6e1de0c31573ff376c7078d/syntaxes/TypeScript.tmLanguage.json#L2855-L2873
598598
ternaryExpression: {
599599
// 1. negative lookahead (?!\?\.\s*[^[:digit:]]) ensures safe navigation does not match
600600
// 2. match the ternary question mark with (\?)
601-
begin: /(?!\?\.\s*[^[:digit:]])(\?)/,
601+
// 3. negative lookahead (?!\?) ensures nullish coalesce does not match
602+
begin: /(?!\?\.\s*[^[:digit:]])(\?)(?!\?)/,
602603
beginCaptures: {
603604
1: {
604605
name: 'keyword.operator.ternary.ts',

syntaxes/test/data/expression.html

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,23 @@
6161
{{ call({ test: 123 }) }}
6262
{{ call({ test: [123, 321, { value: ['string', false, true] }] }) }}
6363

64+
<!-- Nullish coalesce -->
65+
{{a ?? 'empty'}}
66+
6467
<!-- Ternary expression -->
6568
{{ condition ? true : false }}
6669
{{ condition ? true : false; }}
67-
{{ condition() ? call(1 + 2 + 3) : call() }}
68-
{{ condition() ? call(1 + 2 + 3) : call(); }}
70+
{{ condition() ? call(1 + 2 + 3) : call() }}
71+
{{ condition() ? call(1 + 2 + 3) : call(); }}
6972
{{ condition()?.object ? call()!.test() : false }}
7073
{{ condition ? ['123'] : { test: 'a' } }}
7174
{{ condition ? 'test' : "test" }}
7275
{{ condition ? [function(), variable] : {} }}
7376
{{ condition ? condition2 ? condition3 ? 1 : 2 : 3 : 4 }}
7477
<!-- Don't match safe navigations -->
7578
{{ condition?.a : 1 }}
79+
<!-- Don't match nullish coalesce -->
80+
{{ condition ?? (a ? 1 : 2) }}
7681

7782
<!-- Microsyntax -->
7883
<!-- Let Expression -->
@@ -99,13 +104,15 @@
99104
{{ ((let param of params) | async) | translate }}
100105

101106
<!-- Mixed -->
102-
{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; index as i }}
107+
{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; index
108+
as i }}
103109
{{ (let param of params?.anotherParam!.param().param | async) as p; let i = index; let first = first }}
104-
{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; let i = index; }}
110+
{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; let i
111+
= index; }}
105112

106113
<!-- Regression -->
107114
<!-- #575 -->
108115
<div *ngFor="let n of nums; index as i"></div>
109116
<div *matHeaderCellDef></div>
110117
<!-- #613 -->
111-
<div *ngIf="x$ | async as a"></div>
118+
<div *ngIf="x$ | async as a"></div>

syntaxes/test/data/expression.html.snap

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,19 @@
623623
# ^ template.ng expression.ng
624624
# ^^ template.ng punctuation.definition.block.ts
625625
>
626+
><!-- Nullish coalesce -->
627+
#^^^^^^^^^^^^^^^^^^^^^^^^^^ template.ng
628+
>{{a ?? 'empty'}}
629+
#^^ template.ng punctuation.definition.block.ts
630+
# ^ template.ng expression.ng variable.other.readwrite.ts
631+
# ^ template.ng expression.ng
632+
# ^^ template.ng expression.ng keyword.operator.logical.ts
633+
# ^ template.ng expression.ng
634+
# ^ template.ng expression.ng string.quoted.single.ts punctuation.definition.string.begin.ts
635+
# ^^^^^ template.ng expression.ng string.quoted.single.ts
636+
# ^ template.ng expression.ng string.quoted.single.ts punctuation.definition.string.end.ts
637+
# ^^ template.ng punctuation.definition.block.ts
638+
>
626639
><!-- Ternary expression -->
627640
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.ng
628641
>{{ condition ? true : false }}
@@ -653,7 +666,7 @@
653666
# ^^^^^ template.ng expression.ng constant.language.boolean.false.ts
654667
# ^^ template.ng expression.ng
655668
# ^^ template.ng punctuation.definition.block.ts
656-
>{{ condition() ? call(1 + 2 + 3) : call() }}
669+
>{{ condition() ? call(1 + 2 + 3) : call() }}
657670
#^^ template.ng punctuation.definition.block.ts
658671
# ^ template.ng expression.ng
659672
# ^^^^^^^^^ template.ng expression.ng entity.name.function.ts
@@ -680,9 +693,9 @@
680693
# ^^^^ template.ng expression.ng entity.name.function.ts
681694
# ^ template.ng expression.ng meta.brace.round.ts
682695
# ^ template.ng expression.ng meta.brace.round.ts
683-
# ^^ template.ng expression.ng
684-
# ^^ template.ng punctuation.definition.block.ts
685-
>{{ condition() ? call(1 + 2 + 3) : call(); }}
696+
# ^ template.ng expression.ng
697+
# ^^ template.ng punctuation.definition.block.ts
698+
>{{ condition() ? call(1 + 2 + 3) : call(); }}
686699
#^^ template.ng punctuation.definition.block.ts
687700
# ^ template.ng expression.ng
688701
# ^^^^^^^^^ template.ng expression.ng entity.name.function.ts
@@ -709,8 +722,8 @@
709722
# ^^^^ template.ng expression.ng entity.name.function.ts
710723
# ^ template.ng expression.ng meta.brace.round.ts
711724
# ^ template.ng expression.ng meta.brace.round.ts
712-
# ^^^ template.ng expression.ng
713-
# ^^ template.ng punctuation.definition.block.ts
725+
# ^^ template.ng expression.ng
726+
# ^^ template.ng punctuation.definition.block.ts
714727
>{{ condition()?.object ? call()!.test() : false }}
715728
#^^ template.ng punctuation.definition.block.ts
716729
# ^ template.ng expression.ng
@@ -837,6 +850,28 @@
837850
# ^ template.ng expression.ng constant.numeric.decimal.ts
838851
# ^ template.ng expression.ng
839852
# ^^ template.ng punctuation.definition.block.ts
853+
><!-- Don't match nullish coalesce -->
854+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.ng
855+
>{{ condition ?? (a ? 1 : 2) }}
856+
#^^ template.ng punctuation.definition.block.ts
857+
# ^ template.ng expression.ng
858+
# ^^^^^^^^^ template.ng expression.ng variable.other.readwrite.ts
859+
# ^ template.ng expression.ng
860+
# ^^ template.ng expression.ng keyword.operator.logical.ts
861+
# ^ template.ng expression.ng
862+
# ^ template.ng expression.ng meta.brace.round.ts
863+
# ^ template.ng expression.ng variable.other.readwrite.ts
864+
# ^ template.ng expression.ng
865+
# ^ template.ng expression.ng keyword.operator.ternary.ts
866+
# ^ template.ng expression.ng
867+
# ^ template.ng expression.ng constant.numeric.decimal.ts
868+
# ^ template.ng expression.ng
869+
# ^ template.ng expression.ng keyword.operator.ternary.ts
870+
# ^ template.ng expression.ng
871+
# ^ template.ng expression.ng constant.numeric.decimal.ts
872+
# ^ template.ng expression.ng meta.brace.round.ts
873+
# ^ template.ng expression.ng
874+
# ^^ template.ng punctuation.definition.block.ts
840875
>
841876
><!-- Microsyntax -->
842877
#^^^^^^^^^^^^^^^^^^^^^ template.ng
@@ -1219,7 +1254,7 @@
12191254
>
12201255
><!-- Mixed -->
12211256
#^^^^^^^^^^^^^^^ template.ng
1222-
>{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; index as i }}
1257+
>{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; index
12231258
#^^ template.ng punctuation.definition.block.ts
12241259
# ^ template.ng expression.ng
12251260
# ^ template.ng expression.ng meta.brace.round.ts
@@ -1265,12 +1300,12 @@
12651300
# ^ template.ng expression.ng meta.array.literal.ts meta.brace.square.ts
12661301
# ^^ template.ng expression.ng
12671302
# ^^^^^ template.ng expression.ng variable.other.readwrite.ts
1268-
# ^ template.ng expression.ng
1269-
# ^^ template.ng expression.ng storage.type.as.ts
1270-
# ^ template.ng expression.ng
1271-
# ^ template.ng expression.ng entity.name.type.ts
1272-
# ^ template.ng expression.ng
1273-
# ^^ template.ng punctuation.definition.block.ts
1303+
>as i }}
1304+
#^^ template.ng expression.ng storage.type.as.ts
1305+
# ^ template.ng expression.ng
1306+
# ^ template.ng expression.ng entity.name.type.ts
1307+
# ^ template.ng expression.ng
1308+
# ^^ template.ng punctuation.definition.block.ts
12741309
>{{ (let param of params?.anotherParam!.param().param | async) as p; let i = index; let first = first }}
12751310
#^^ template.ng punctuation.definition.block.ts
12761311
# ^ template.ng expression.ng
@@ -1318,7 +1353,7 @@
13181353
# ^^^^^ template.ng expression.ng variable.other.readwrite.ts
13191354
# ^ template.ng expression.ng
13201355
# ^^ template.ng punctuation.definition.block.ts
1321-
>{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; let i = index; }}
1356+
>{{ (let param of params?.get('value')!.property | async).anotherProperty | translate: ['language1', 'language2']; let i
13221357
#^^ template.ng punctuation.definition.block.ts
13231358
# ^ template.ng expression.ng
13241359
# ^ template.ng expression.ng meta.brace.round.ts
@@ -1366,12 +1401,12 @@
13661401
# ^^^ template.ng expression.ng storage.type.ts
13671402
# ^ template.ng expression.ng
13681403
# ^ template.ng expression.ng variable.other.readwrite.ts
1369-
# ^ template.ng expression.ng
1370-
# ^ template.ng expression.ng keyword.operator.assignment.ts
1371-
# ^ template.ng expression.ng
1372-
# ^^^^^ template.ng expression.ng variable.other.readwrite.ts
1373-
# ^^ template.ng expression.ng
1374-
# ^^ template.ng punctuation.definition.block.ts
1404+
>= index; }}
1405+
#^ template.ng expression.ng keyword.operator.assignment.ts
1406+
# ^ template.ng expression.ng
1407+
# ^^^^^ template.ng expression.ng variable.other.readwrite.ts
1408+
# ^^ template.ng expression.ng
1409+
# ^^ template.ng punctuation.definition.block.ts
13751410
>
13761411
><!-- Regression -->
13771412
#^^^^^^^^^^^^^^^^^^^^ template.ng
@@ -1418,5 +1453,4 @@
14181453
# ^ template.ng meta.ng-binding.template.html expression.ng
14191454
# ^ template.ng meta.ng-binding.template.html expression.ng entity.name.type.ts
14201455
# ^ template.ng meta.ng-binding.template.html string.quoted.html punctuation.definition.string.end.html
1421-
# ^^^^^^^^ template.ng
1422-
>
1456+
# ^^^^^^^^ template.ng

syntaxes/test/data/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
<my-component *custom-if="true != false"></my-component>
5252
<my-component *custom_if="true != false"></my-component>
5353
<my-component *custom_$if="true != false"></my-component>
54-
<my-component *%invalid="expr"></my-component>
54+
<my-component *%invalid="expr"></my-component>

syntaxes/test/data/template.html.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,5 +426,4 @@
426426
# ^ template.ng meta.ng-binding.template.html string.quoted.html punctuation.definition.string.end.html
427427
# ^^^^^^^^^^^^^^^^^ template.ng
428428
><my-component *%invalid="expr"></my-component>
429-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.ng
430-
>
429+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.ng

0 commit comments

Comments
 (0)